Friday, February 17, 2012

C++ Tutorial: Interfaces and virtual classes, Singletons and Exclusive ownership classes

Today I will try to help some people understand a little better about object oriented programming(OOP).

I won't do really in-detail explanations of the code, but actually try to explain the concept of taking these approaches from a software design point of view.

Let me just warn you that the names I may call to these concepts may not be the official recognized names, but that is not the point of the tutorial.

To begin, I would like to explain the common knowledge concept of an Interface.

An interface is when you create a class with some functionality, but leave the implementation abstract, without being defined.

A perfect example would be an interface class IRenderer, that would have methods to initialize the graphics library, to render shapes, models and everything else a renderer needs to do.

Then, you would inherit from IRenderer and implement all the methods, to actually make it work with a specific graphics library. Making a COpenGLRenderer and a CDirectXRenderer would be logical, for example. Each would implement all methods in IRenderer, both using different API's.

A purely virtual interface is when the base class, in this case IRenderer doesn't do anything on its own and can't even be instanced. This happens if the methods are declared as "virtual <type> <name>(<params>) = 0;" and you don't define its body at all.

Otherwise, you can have a virtual class that already works and does something, but can be extended by reimplementing those methods.

Singletons


This concept is also widely known, but often applied erroneously. Sometimes you have a class that you want to ensure that it is only initialized once. There are actually different approaches to do this, but i will recommend one that I find specially effective.

You could make all methods and variables static, which some may consider a dirty solution, since it is not the most safe solution because the unpredictable global initialization of a program. It can be avoided but normally demands extra checks that impact performance without necessity.

So, the first thing I recommend is a private constructor. A private constructor is very very useful because it will ensure that only a member function or a friend class can instantiate this class.

That means immediately you cannot do something like "MyClass object;", you will get compiler errors.
Though, you can create a pointer to it "MyClass* object". Now you need to make it valid, so you request its creation "MyClass* object = MyClass::create()".

MyClass::create is really a static method of MyClass. That is why you can call it without having an instance before. Besides that, it belongs to MyClass so it may instance it.

Here is a sample class of this lesson applied:

class MyClass{
public:
      int getNumber();

      ~MyClass(){
             myInstance = NULL;
       }

       static MyClass* instance(){
              // Could instantiate on demand..
              return myInstance;
       }

       static MyClass* create(){
              if(!myInstance) MyClass(); // create an object so it becomes the static instance
              return myInstance;
       }

private:
      int number;
      static MyClass* myInstance;

     MyClass(){
           myInstance = this;
    }

};
MyClass* MyClass::myInstance = NULL;

Hope you understand and make profit of using this!

To finish, in behalf of this understanding, you can easily create a class that can ONLY be created by specific classes. Just make the constructor protected or private so it cannot be instanced at all.

Then, any class that you want to be able to instance your exclusive creation class, just declare it as a friend class.

...
private:
     friend class ThisClassMayInstanceMe;
...

If you know what i mean!

Hope it is clear for you! Any doubts, suggestions just contact me!

Good coding.

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. very informative post indeed .being enrolled in http://www.wiziq.com/course/5776-object-oriented-programming-with-c
    i was looking for such articles online to assist me and your article helped me a lot. i really like that you are providing such information.

    ReplyDelete