Saturday, May 5, 2012

Design Pattern Simplified using C++

C++ code for implementing Strategy Design Pattern:


"The Strategy design pattern defines a family of algorithms,encapsulates each one,and make then interchangeable.Strategy lets the algorithm vary independently from clients that use it."

Problem Statement: Making Ducks Fly. :)

1:  #include<iostream>  
2:  using namespace std;  
3:  class FlyBehaviour  
4:  {  
5:    public:  
6:    virtual void fly()=0;  
7:  };  
8:  class FlyWithWings:public FlyBehaviour  
9:  {  
10:    public:  
11:    void fly()  
12:    {  
13:      cout<<"I'm flying"<<endl;  
14:    }  
15:  };  
16:  class FlyNoWay:public FlyBehaviour  
17:  {  
18:    public:  
19:    void fly()  
20:    {  
21:      cout<<"I can't fly..."<<endl;  
22:    }  
23:  };  
24:  class QuackBehaviour  
25:  {  
26:    public:  
27:    virtual void quack()=0;  
28:  };  
29:  class Quack:public QuackBehaviour  
30:  {  
31:    public:  
32:    void quack()  
33:    {  
34:      cout<<"Quack"<<endl;  
35:    }  
36:  };  
37:  class MuteQuack:public QuackBehaviour  
38:  {  
39:    public:  
40:    void quack()  
41:    {  
42:      cout<<"Silence"<<endl;  
43:    }  
44:  };  
45:  class Squeak:public QuackBehaviour  
46:  {  
47:    public:  
48:    void quack()  
49:    {  
50:      cout<<"Squeak"<<endl;  
51:    }  
52:  };  
53:  class Duck  
54:  {  
55:    protected:  
56:    FlyBehaviour *flyBehav;  
57:    QuackBehaviour *quackBehav;  
58:    public:  
59:    Duck(){}  
60:    void swim()  
61:    {  
62:      cout<<"All ducks float/swim"<<endl;  
63:    }  
64:    virtual void display()=0;  
65:    void performFly()  
66:    {  
67:      flyBehav->fly();  
68:    }  
69:    void performQuack()  
70:    {  
71:      quackBehav->quack();  
72:    }  
73:    void setFlyingBehav(FlyBehaviour *pflyBehav)  
74:    {  
75:      flyBehav = pflyBehav;  
76:    }  
77:    void setQuackingBehav(QuackBehaviour *pquackBehav)  
78:    {  
79:      quackBehav = pquackBehav;  
80:    }  
81:  };  
82:  class MallardDuck:public Duck  
83:  {  
84:    public:  
85:    MallardDuck()  
86:    {  
87:      quackBehav = new Quack();  
88:      flyBehav=new FlyWithWings();  
89:    }  
90:    void display()  
91:    {  
92:      cout<<"I am real Mallard Duck"<<endl;  
93:    }  
94:  };  
95:  class ModelDuck:public Duck  
96:  {  
97:    public:  
98:    ModelDuck()  
99:    {  
100:      quackBehav = new Quack();  
101:      flyBehav=new FlyNoWay();  
102:    }  
103:    void display()  
104:    {  
105:      cout<<"I am Model Duck"<<endl;  
106:    }  
107:  };  
108:  class FlyRocketPowered:public FlyBehaviour  
109:  {  
110:    void fly()  
111:    {  
112:      cout<<"Fly Rocket Powered ==> Zoooom"<<endl;  
113:    }  
114:  };  
115:  class DuckHunter  
116:  {  
117:    private:  
118:    Duck * duckType;  
119:    public:  
120:    void SetDuckType(Duck *pDuck)  
121:    {  
122:      duckType = pDuck;  
123:    }  
124:    void StartHunting()  
125:    {  
126:     for(int i=0;i<3;i++)  
127:      duckType->performQuack();  
128:    }  
129:  };  
130:  int main()  
131:  {  
132:   cout<<"*********Hello Duck*********"<<endl;  
133:   Duck *mallard = new MallardDuck();  
134:   mallard->display();  
135:   mallard->performQuack();  
136:   mallard->performFly();  
137:   Duck *model = new ModelDuck();  
138:   model->performFly();  
139:   model->setFlyingBehav(new FlyRocketPowered());  
140:   model->performFly();  
141:   DuckHunter *huntingToy = new DuckHunter();  
142:   huntingToy->SetDuckType(new MallardDuck());  
143:   huntingToy->StartHunting();  
144:   return 0;  
145:  }  

No comments:

Post a Comment