Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts

Saturday, October 24, 2009

An example of Interface pattern in use

Interface pattern is a interesting pattern and only recently I realized the value of using it and where people put it into use.

One real interesting use of this that I found was in using callback functions for components.

Say you want to use a component A1 in a class B. But this component A1 needs to get some information from its user, class B, then we need a callback. How does one make sure that the right callback is provided by the implementer. Well interface pattern provides a simple solution for this.

To make to use of interface pattern, we need to implement an abstract class A2. This class should have the function which we want to use as callback as a pure virtual function. Now each class which needs to use component A1, should inherit A2 and implement the desired callback function in A2.

Example, implementation of this is as follows

class A2
{

public:

        void CallBackFunc() = 0;

}

class A1
{

public:

        A2 *user;

         A1(A2 *userClass)
        {

               user = userClass;

        }

        void action()
        {

              user->CallBackFunc();

        }

}

class B : public A2
{

       B()
       {

             A1 *newObj = new A1(this);

       }

        void CallBackFunc()
        {

             //Do something here

              return;

        }

}

Thursday, July 9, 2009

The importance of Interface classes

I am sure you might have heard about interfaces. But then why do you need them. You anyways are going to implement those classes. Why take the unnecessary overhead of writing interfaces for them as well.

Actually its not necessary to write a interface class for all classes that you write. But there are certain cases when its much much better if you have them in place.

First what are interface classes. Well this is a base class which has pure declaration of all the public methods need in the target class. All these methods are make pure virtual functions. No implementation is added to this class. For example if you write a class Time, you would want to declare an interface named, ITime. So the class declarations would be as follows.

class ITime 
{
      public : int getHour() = 0;
                   int getMinute()= 0;
                   int getSecond()= 0;
                   void setHour(int hour)= 0;
                   void setMinute(int min)= 0;
                   void setSecond(int sec)= 0;
};
class Time: public ITime
{
      public : int getHour() = 0;
                   int getMinute()= 0;
                   int getSecond()= 0;
                   void setHour(int hour)= 0;
                   void setMinute(int min)= 0;
                   void setSecond(int sec)= 0;
      private: int m_hour, m_min, m_sec;
};

The interface class has no implementation but the concrete class does. Concrete class contains all additional private member variables needed as well as constructors and destructors, as needed.

When do you need it? For classes which are instantiated at a few locations but used at a lot of other places, it makes sense to create interfaces for them.

But why do all this circus. Well the reason is pretty straight forward, to allow users of this class not care about whatever is there in the actual implementation. All internally used methods and variables are not known to the user of this class. Also since the concrete class headers are not included at all places where this class is being used, changes in internal implementation need not lead to compilation of each of the other classes. Only if something is changed in Interface class, recompilation would take place in all places else its a faster process.

This is more or less what I understand as a need for Interfaces. Please do let me know if you think otherwise.

Saturday, May 2, 2009

Why you should use forward declarations

Well I am sure you have always heard people telling you to use forward class declaration. I am not sure if you too have thought that what’s the point of it.

Well one best use case is in case of circular dependency. When you have a class A depended on class B and class B also references class A, you will be in a mess including header files in each other. So forward declaration would be a good way out of the mess, provided it meets certain conditions.

Another reason for using forward class declaration is that you will actually speed up the compilation time. Say you include class A as a pointer in class B and for that you have included header file of A in B. Then for any change in A, if it affects B in any manner or not, would lead to compilation of class B as well. This can be a serious pain point in case of large projects. If A has a forward declaration, then A would be compiled(obviously) and then at link time, compiler would make class B aware of A. 

But now the big question is, when can u not use forward declaration. Well answer to that question is very simple. Think like a compiler, in what situations would you be missing an actual declaration.

One of the reason can be when you are using the class as a base class. In that case u cannot determine the base class function  and the compiler cannot check for rules until it finds the declaration.

Another case is when you declare it as a member. In this case compiler has no clue about the size of memory to be allocated for this member. So forward declaration does not work here as well.

At times you want to define functions in the header file itself. In that case, if your function uses a forward declared class, it would throw a compile time error, because it has no clue about its properties.

Also its pretty clear that if you try and create an dynamically allocated object of class, forward declaration would not work if you try and call some function on that object. Again simply because the compiler would have no clue about that.

So friends its important that we use, forward declarations wherever possible. It does make life a lot more simpler, but then one size does not fit all Happy

Saturday, March 28, 2009

When Multiple Inheritance is bad

We all see and know that Multiple inheritance is one of the most classy features of C++. Something that was sorely missed in Java which did not allow. Well recently I realized that its actually a good thing if we just stick with single inheritance. Why, well the reason is simple.

 Diagram1

So now say you are the creator of all the classes, A,B,C, X and Y.  In that case your best approach would be if it was such that there was single inheritance. In that case you would have several benefits. For example if there was such a case that at some point of time, you need to use some function in B from class Y, you would not be able to do it. A single inheritance would provide you the extra flexibility to do operations on the object C in the base classes.

Well this is debatable that you should always stick to single inheritance or not. My take is that one hat does not fit all. So based on the situation, we need to take a call and do what best solves the problem at hand.