You have a library provided by the vendor. All you have is header files and
library files.
Library contains the class Shape and there is whole hierarchy tree (i mean
classes which derive from this base class).
Now you want to add some function "getArea" (not originally present in the
class or any of its derived class) in the class "Shape" , you dont have the
source code.

Using this library, you have written a lot of code. Now you have to make
some changes so that, any object of Shape class (or its derived class) will
be able to call this function.
With your strategy, you should be able to override the definition of this
function in the derived class.

I think it is related to design patterns, not sure.
I gave the solution that : we will use MyShape , and use composition. i.e.
will create the object of Shape class inside this "NewShape" class and
interface can be made. Internally, we can call the functions of "Shape"
object. That will work fine. But we have to do this for the whole design
hierarchy. Secondly, if there is some changes by the vendor in future in the
library, i have to change my new design tree.
e.g.
class Shape {
   public : void draw();
              void clear();
}
class Circle : public Shape{
   public : void draw();
              void clear();
}
class Rectangle: public Shape{
   public : void draw();
              void clear();
}


class NewShape{
private Shape * obj;
   public : void draw(){ obj->draw(); }
              void clear() { obj->clear(); }
              int getArear(){
                     //implementtation of getArea function
              }
}

class NewCircle : NewShape{
private Circle * obj;
   public : void draw(){ obj->draw(); }
              void clear() { obj->clear(); }
              int getArear(){
                     //implementtation of getArea function specific to
circle
              }
}
class NewRectangle : NewShape{
private Rectangle * obj;
   public : void draw(){ obj->draw(); }
              void clear() { obj->clear(); }
              int getArear(){
                     //implementtation of getArea function
              }
}

I know this solution is not good.. please provide a better approach for
this.
Please let me knw if anything is not clear.




Thanks in advance.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to