Saturday, 16 June 2018

Adapter Design Pattern in C++

Adapter Design pattern converts the interface of a class into another interface that clients expect.


#include
 
using namespace std;
 
class Circle
{
 public:
    virtual void draw() = 0;
};
 
class StandardCircle
{
public:
    StandardCircle(double radius)
    {
      radius_ = radius;
        cout << "StandardCircle:  create. radius = "<< radius_ << endl;
    }
    
private:
    double radius_ ;
};
 
class CAdapter : public Circle, private StandardCircle  //Adapter Class
{
public:
    CAdapter( double diameter)
        : StandardCircle(diameter/2)
    {
        cout << "CAdapter: create. diameter = " << diameter << endl;
    }
    virtual void draw()
    {
        cout << "CAdapter: draw." << endl;
        oldDraw();
    }
};
 
int main()
{
    Circle*  c = new CAdapter(14);
    c->draw();
}

No comments:

Post a Comment