抽象工廠模式(Abstract Factory Pattern)
阿新 • • 發佈:2020-08-14
抽象工廠模式是圍繞一個超級工廠建立其他工廠。
在抽象工廠模式中,介面是負責建立一個相關物件的工程,不需要顯示指定它們的類。每個生成的工廠都能按照工廠模式提供物件。
意圖:提供一個建立一系列相關或者相互依賴物件的介面,而無需指定它們具體的類。
解決:主要解決介面選擇的問題。
如何解決:在一個產品族裡,定義多個產品。
程式碼:在一個工廠裡聚合多個同類產品。
例項:
優點:當一個產品族中的多個物件被設計成一起工作時,它能保證客戶端始終只使用同一個產品族中的物件。
缺點:產品族擴充套件非常困難,要增加一個系列的某一產品,既要在抽象的Creator里加程式碼,又要在具體的裡面加程式碼。
使用場景:1、QQ換面板,一整套一起換。
注意事項:產品族難擴充套件,產品等級易擴充套件。
C++實現:
1、為形狀建立一個介面
Shape.h
#pragma once class Shape { public: virtual void draw() {} };
2、建立實現介面的實體類
Rectangle.h
#pragma once #include<iostream> #include "Shape.h" class Rectangle :public Shape { public: void draw() { std::cout << "Rectangle::draw() method." << std::endl; } };
Square.h
#pragma once #include<iostream> #include "Shape.h" class Square :public Shape { public: void draw() { std::cout << "Square::draw() method." << std::endl; } };
Circle.h
#pragma once #include<iostream> #include "Shape.h" class Circle :public Shape { public: void draw() { std::cout << "Circle::draw() method." << std::endl; } };
3、為顏色建立一個介面
Color.h
#pragma once class Color { public: virtual void fill(){} };
4、建立實現介面的實體類
Blue.h
#pragma once #include<iostream> #include"Color.h" class Blue :public Color { public: void fill() { std::cout << "Blue::fill() method." << std::endl; } };
Red.h
#pragma once #include<iostream> #include"Color.h" class Red :public Color { public: void fill() { std::cout << "Red::fill() method." << std::endl; } };
Green.h
#pragma once #include<iostream> #include"Color.h" class Green :public Color { public: void fill() { std::cout << "Green::fill() method." << std::endl; } };
5、為Color和Shape物件建立抽象類來獲取工廠
AbstractFactory.h
#pragma once #include"Color.h" #include"Shape.h" #include<string> #include<iostream> class AbstractFactory { public: virtual Color* getColor(std::string color) = 0; virtual Shape* getShape(std::string shape) = 0; };
6、建立擴張了AbstractFactory的工廠類,基於給定的資訊生成實體類的物件
ColorFactory.h
#pragma once #include<iostream> #include<string> #include"Blue.h" #include"Green.h" #include"Red.h" #include"AbstractFactory.h" class ColorFactory :public AbstractFactory { Shape* getShape(std::string shapeType) { return NULL; } Color* getColor(std::string color) { if (color == " ") { return NULL; } if (color == "RED") { return new Red(); } else if (color == "GREEN") { return new Green(); } else if (color == "BLUE") { return new Blue(); } return NULL; } };
ShapeFactory.h
#pragma once #include<iostream> #include<string> #include"Circle.h" #include"Rectangle.h" #include"Square.h" #include"AbstractFactory.h" class ShapeFactory :public AbstractFactory { Shape* getShape(std::string shapeType) { if (shapeType == " ") { return NULL; } if (shapeType == "CIRCLE") { return new Circle(); } else if (shapeType == "RECTANGLE") { return new Rectangle(); } else if (shapeType == "SQUARE") { return new Square(); } return NULL; } Color* getColor(std::string color) { return NULL; } };
7、建立一個工廠創造器/生成器,通過傳遞形狀或顏色資訊來獲取工廠
FactoryProducer.h
#pragma once #include<iostream> #include<string> #include"AbstractFactory.h" #include"ShapeFactory.h" #include"ColorFactory.h" class FactoryProducer { public: static AbstractFactory* getFactory(std::string choice) { if (choice == "SHAPE") { return new ShapeFactory(); } else if (choice == "COLOR") { return new ColorFactory(); } return NULL; } };
8、使用FactoryProducer來獲取AbatractFactory,通過傳遞型別資訊來獲取實體類的物件
AbstractFactoryPatternDemo.cpp
#include<iostream> #include"AbstractFactory.h" #include"FactoryProducer.h" using namespace std; int main() { //獲取形狀工廠 AbstractFactory* shapeFactory = FactoryProducer::getFactory("SHAPE"); //獲取形狀為 Circle 的物件 Shape* shape1 = shapeFactory->getShape("CIRCLE"); //呼叫 Circle 的 draw 方法 shape1->draw(); //獲取顏色工廠 AbstractFactory* colorFactory = FactoryProducer::getFactory("COLOR"); //獲取顏色為 Red 的物件 Color* color1 = colorFactory->getColor("RED"); //呼叫 Red 的 fill 方法 color1->fill(); system("pause"); return 0; }
9、實驗結果
參考:https://www.runoob.com/design-pattern/abstract-factory-pattern.html