Saturday, May 3, 2014

Abstract Factory Pattern

  1 #include <iostream>
  2
  3 using namespace std;
  4
  5 class Mobile{
  6         public:
  7                 virtual void getName() { }
  8                 virtual void getScreen() { }
  9                 virtual void getBattery() { }
 10 };
 11
 12 class NokiaMobile: public Mobile{
 13         public:
 14                 virtual void getName()
 15                 {
 16                         cout<<"This is Nokia Mobile.."<<endl;
 17                 }
 18                 virtual void getScreen()
 19                 {
 20                         cout<<"This is Nokia Mobile screen.."<<endl;
 21                 }
 22                 virtual void getBattery()
 23                 {
 24                         cout<<"This is Nokia Mobile battery.."<<endl;
 25                 }
 26 };
 27
 28
 29 class SamsungMobile: public Mobile{
 30         public:
 31                 void display()
 32                 {
 33                         cout<<"This is Samsung Mobile.."<<endl;
 34                 }
 35                 virtual void getScreen()
 36                 {
 37                         cout<<"This is Samsung Mobile screen.."<<endl;
 38                 }
 39                 virtual void getBattery()
 40                 {
 41                         cout<<"This is Samsung Mobile battery.."<<endl;
 42                 }
 43 };
 44
 45 class Charger{
 46         public:
 47                 virtual void getName(){}
 48
 49 };
 50 class NokiaCharger: public Charger{
 51         public:
 52                 virtual void getName()
 53                 {
 54                         cerr<<"This is Nokia Charger.."<<endl;
 55                 }
 56
 57 };
 58
 59 class SamsungCharger: public Charger{
 60         public:
 61                 virtual void getName()
 62                 {
 63                         cerr<<"This is Samsung Charger.."<<endl;
 64                 }
 65
 66 };
 67
 68
 69 class MobileFactory{
 70         public:
 71                 virtual Mobile* getMobile(){}
 72                 virtual Charger* getCharger(){}
 73 };
 74
 75 class NokiaMobileFactory: public MobileFactory{
 76         public:
 77                 virtual Mobile* getMobile()
 78                 {
 79                         return new NokiaMobile();
 80                 }
 81                 virtual Charger* getCharger()
 82                 {
 83                         return new NokiaCharger();
 84                 }
 85 };
 86
 87
 88 class SamsungMobileFactory : public MobileFactory{
 89         public:
 90                 virtual Mobile* getMobile()
 91                 {
 92                         return new SamsungMobile();
 93                 }
 94                 virtual Charger* getCharger()
 95                 {
 96                         return new SamsungCharger();
 97                 }
 98 };
 99
100
101 class FactoryCreator{
102         public:
103                 MobileFactory* createFactory(int order)
104                 {
105                         if(order == 1)
106                                 return new NokiaMobileFactory();
107                         else
108                                 return new SamsungMobileFactory();
109                 }
110 };
111
112
113 int main()
114 {
115         int orderMobile;
116         cout<<"1. Nokia "<<endl;
117         cout<<"2. Samsung "<<endl;
118         cin>>orderMobile;
119         FactoryCreator *fc = new FactoryCreator();
120         fc->createFactory(orderMobile)->getMobile()->getName();
121         fc->createFactory(orderMobile)->getMobile()->getScreen();
122         fc->createFactory(orderMobile)->getMobile()->getBattery();
123         fc->createFactory(orderMobile)->getCharger()->getName();
124         delete fc;
125
126 }

No comments:

Post a Comment