设计模式之抽象工厂模式
抽象工厂模式针对产品族,不针对产品等级
1 #include<iostream> 2 using namespace std; 3 #include<string> 4 5 //简单工厂不符合开闭原则,所以这里不把创建每个水果对象放在一个工厂里,而是把创建工厂抽象出来,每次创建一个具体水果由每个对象工厂自己常见,达到开闭原则。 6 7 //但是工厂模式由弊端,每次新建一个对象就要新建一个工厂类,类多了就很麻烦 8 9 //抽象工厂模式针对的是产品族,满足开闭原则,不针对产品等级,不满足开闭原则 10 11 //抽象苹果 12 class abstractApple{ 13 public: 14 virtual void showName()=0; 15 }; 16 //中国苹果 17 class chinaApple:public abstractApple{ 18 public: 19 virtual void showName() 20 { 21 cout<<"china apple"<<endl; 22 } 23 }; 24 //美国苹果 25 class usaApple:public abstractApple{ 26 public: 27 virtual void showName() 28 { 29 cout<<"usa apple"<<endl; 30 } 31 }; 32 //日本苹果 33 class japanApple:public abstractApple{ 34 public: 35 virtual void showName() 36 { 37 cout<<"japan apple"<<endl; 38 } 39 }; 40 41 //抽象梨 42 class abstractPear{ 43 public: 44 virtual void showName()=0; 45 }; 46 //中国梨 47 class chinaPear:public abstractPear{ 48 public: 49 virtual void showName() 50 { 51 cout<<"china pear"<<endl; 52 } 53 }; 54 //美国梨 55 class usaPear:public abstractPear{ 56 public: 57 virtual void showName() 58 { 59 cout<<"usa pear"<<endl; 60 } 61 }; 62 //日本梨 63 class japanPear:public abstractPear{ 64 public: 65 virtual void showName() 66 { 67 cout<<"japan pear"<<endl; 68 } 69 }; 70 71 //抽象香蕉 72 class abstractBanana{ 73 public: 74 virtual void showName()=0; 75 }; 76 //中国香蕉 77 class chinaBanana:public abstractBanana{ 78 public: 79 virtual void showName() 80 { 81 cout<<"china Banana"<<endl; 82 } 83 }; 84 //美国香蕉 85 class usaBanana:public abstractBanana{ 86 public: 87 virtual void showName() 88 { 89 cout<<"usa Banana"<<endl; 90 } 91 }; 92 //日本香蕉 93 class japanBanana:public abstractBanana{ 94 public: 95 virtual void showName() 96 { 97 cout<<"japan Banana"<<endl; 98 } 99 }; 100 101 //抽象工厂 102 class abstractFactory{ 103 public: 104 virtual abstractApple* createApple()=0; 105 virtual abstractPear* createPear()=0; 106 virtual abstractBanana* createBanana()=0; 107 }; 108 109 //中国工厂 110 class chinaFactory:public abstractFactory{ 111 public: 112 virtual abstractApple* createApple(){ 113 return new chinaApple; 114 } 115 virtual abstractPear* createPear(){ 116 return new chinaPear; 117 } 118 virtual abstractBanana* createBanana(){ 119 return new chinaBanana; 120 } 121 }; 122 123 //美国工厂 124 class usaFactory:public abstractFactory{ 125 public: 126 virtual abstractApple* createApple(){ 127 return new usaApple; 128 } 129 virtual abstractPear* createPear(){ 130 return new usaPear; 131 } 132 virtual abstractBanana* createBanana(){ 133 return new usaBanana; 134 } 135 }; 136 //日本工厂 137 class japanFactory:public abstractFactory{ 138 public: 139 virtual abstractApple* createApple(){ 140 return new japanApple; 141 } 142 virtual abstractPear* createPear(){ 143 return new japanPear; 144 } 145 virtual abstractBanana* createBanana(){ 146 return new japanBanana; 147 } 148 }; 149 150 int main() 151 { 152 //创建一个抽象工厂 153 abstractFactory *factory=NULL; 154 //创建一个抽象苹果 155 abstractApple* apple=NULL; 156 //创建一个抽象梨 157 abstractPear* pear=NULL; 158 //创建一个抽象香蕉 159 abstractBanana* banana=NULL; 160 161 162 //创建一个中国苹果 163 factory=new chinaFactory; 164 apple=factory->createApple(); 165 apple->showName(); 166 //创建一个中国香蕉 167 banana=factory->createBanana(); 168 banana->showName(); 169 //创建一个中国梨 170 pear=factory->createPear(); 171 pear->showName(); 172 delete apple; 173 delete pear; 174 delete banana; 175 delete factory; 176 177 //创建一个日本苹果 178 factory=new japanFactory; 179 apple=factory->createApple(); 180 apple->showName(); 181 //创建一个日本香蕉 182 banana=factory->createBanana(); 183 banana->showName(); 184 //创建一个日本梨 185 pear=factory->createPear(); 186 pear->showName(); 187 delete apple; 188 delete pear; 189 delete banana; 190 delete factory; 191 192 193 194 return 0; 195 }