抽象工厂模式

 

 

 

  1 #include <iostream>
  2 using namespace std;
  3 
  4 class FrameApi {
  5 public:
  6     virtual void draw() = 0;
  7 protected:
  8     FrameApi() {};
  9 };
 10 
 11 class LayoutApi {//分辨率的布局器
 12 public:
 13     virtual void installFrame() = 0;
 14 protected:
 15     LayoutApi() {}
 16 };
 17 
 18 //pc上的Frame
 19 class ComputerFrame :public FrameApi {
 20 public:
 21     ComputerFrame(int pins) : m_pins(pins) {
 22 
 23     }
 24     void draw() {
 25         cout << "现在是pc机的Frame,我适用的分辨率是" << m_pins << endl;
 26     }
 27 private:
 28     int m_pins;
 29 };
 30 
 31 class MobileFrame :public FrameApi {
 32 public:
 33     MobileFrame(int pins) :m_pins(pins) {}
 34     void draw() {
 35         cout << "现在是Mobile的Frame,我使用的分辨率是" << m_pins << endl;
 36     }
 37 
 38 private:
 39     int m_pins;
 40 };
 41 
 42 //高分辨率的布局
 43 class HighLayout :public LayoutApi {
 44 public:
 45     HighLayout(int FrameAdpaterPins) :m_FrameAdpaterPins(FrameAdpaterPins) {}
 46     void installFrame() {
 47         cout << "现在是在PC环境下,我们使用的高分辨率布局" << m_FrameAdpaterPins << endl;
 48     }
 49 
 50 private:
 51     int m_FrameAdpaterPins;
 52 };
 53 
 54 //低分辨率的布局
 55 class LowLayout :public LayoutApi {
 56 public:
 57     LowLayout(int FrameAdpaterPins) :m_FrameAdpaterPins(FrameAdpaterPins) {}
 58     void installFrame() {
 59         cout << "现在是在Mobile环境下,我们使用的低分辨率布局" << m_FrameAdpaterPins << endl;
 60     }
 61 
 62 private:
 63     int m_FrameAdpaterPins;
 64 };
 65 //抽象工厂用来定义产品簇
 66 class AbstractFactory {
 67 public:
 68     virtual FrameApi* createFrameApi() = 0;
 69     virtual LayoutApi* createLayoutApi() = 0;
 70 protected:
 71     AbstractFactory(){}
 72 };
 73 
 74 class Schema1 : public AbstractFactory {
 75 public:
 76     FrameApi* createFrameApi() {
 77         return new ComputerFrame(1024);
 78 
 79     }
 80     LayoutApi* createLayoutApi() {
 81         return new HighLayout(1024);
 82     }
 83 };
 84 
 85 class Schema2 :public AbstractFactory {
 86 public:
 87     FrameApi* createFrameApi() {
 88         return new MobileFrame(800);
 89     }
 90     LayoutApi* createLayout() {
 91         return new LowLayout(800);
 92     }
 93 };
 94 
 95 class AdvancedGuiEngineer {
 96 public:
 97     void prepareMaterials(AbstractFactory* pSchema) {
 98         this->pFrame = pSchema->createFrameApi();
 99         this->pLayout = pSchema->createLayoutApi();
100         pFrame->draw();
101         pLayout->installFrame();
102     }
103 private:
104     FrameApi* pFrame;
105     LayoutApi* pLayout;
106 };
107 
108 //你去肯德基点餐,只要你是点的套餐,就一定不会不适配。
109 int main(void) {
110     /*GUIEngineer* pEng = new GUIEngineer;
111     pEng->prepareDraw(1, 2);*/
112     AdvancedGuiEngineer* pEng = new AdvancedGuiEngineer();
113     pEng->prepareMaterials(new Schema1());
114 
115     system("pause");
116     return 0;
117 }
代码

 

posted @ 2023-05-16 17:27  泽良_小涛  阅读(2)  评论(0编辑  收藏  举报