设计模式之一
使用场景
细分
简单工厂模式(Simple Factory Pattern) :
工厂方法模式(Factory Method Pattern) :
抽象工厂模式(Abstract Factory Pattern) :
1. 简单工厂模式(Simple Factory) 简单工厂模式通过一个工厂类来创建不同类的对象,根据传入的参数决定实例化哪一个类。
示例: 假设我们有一个产品接口 Product
,以及两个具体产品类 ConcreteProductA
和 ConcreteProductB
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include <iostream> #include <memory> #include <string> class Product { public : virtual ~Product () = default ; virtual void use () = 0 ; }; class ConcreteProductA : public Product { public : void use () override { std::cout << "Using ConcreteProductA" << std::endl; } }; class ConcreteProductB : public Product { public : void use () override { std::cout << "Using ConcreteProductB" << std::endl; } }; class SimpleFactory { public : static std::unique_ptr<Product> createProduct (const std::string& type) { if (type == "A" ) { return std::make_unique <ConcreteProductA>(); } else if (type == "B" ) { return std::make_unique <ConcreteProductB>(); } return nullptr ; } }; int main () { auto productA = SimpleFactory::createProduct ("A" ); productA->use (); auto productB = SimpleFactory::createProduct ("B" ); productB->use (); return 0 ; }
2. 工厂方法模式(Factory Method) 工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类的实例化推迟到子类中进行。
示例: 假设我们有一个抽象产品 Product
和两个具体产品 ConcreteProductA
、ConcreteProductB
,以及对应的工厂接口 Factory
和其实现 ConcreteFactoryA
、ConcreteFactoryB
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include <iostream> #include <memory> class Product { public : virtual ~Product () = default ; virtual void use () = 0 ; }; class ConcreteProductA : public Product { public : void use () override { std::cout << "Using ConcreteProductA" << std::endl; } }; class ConcreteProductB : public Product { public : void use () override { std::cout << "Using ConcreteProductB" << std::endl; } }; class Factory { public : virtual ~Factory () = default ; virtual std::unique_ptr<Product> createProduct () = 0 ; }; class ConcreteFactoryA : public Factory { public : std::unique_ptr<Product> createProduct () override { return std::make_unique <ConcreteProductA>(); } }; class ConcreteFactoryB : public Factory { public : std::unique_ptr<Product> createProduct () override { return std::make_unique <ConcreteProductB>(); } }; int main () { auto factoryA = std::make_unique <ConcreteFactoryA>(); auto productA = factoryA->createProduct (); productA->use (); auto factoryB = std::make_unique <ConcreteFactoryB>(); auto productB = factoryB->createProduct (); productB->use (); return 0 ; }
3. 抽象工厂模式(Abstract Factory) 抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
示例: 假设我们有两套产品族,一套是 Button
和 Window
,每套产品族有两个具体实现:WindowsButton
、WindowsWindow
和 MacButton
、MacWindow
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #include <iostream> #include <memory> class Button { public : virtual ~Button () = default ; virtual void paint () = 0 ; }; class Window { public : virtual ~Window () = default ; virtual void display () = 0 ; }; class WindowsButton : public Button { public : void paint () override { std::cout << "Painting a Windows button" << std::endl; } }; class WindowsWindow : public Window { public : void display () override { std::cout << "Displaying a Windows window" << std::endl; } }; class MacButton : public Button { public : void paint () override { std::cout << "Painting a Mac button" << std::endl; } }; class MacWindow : public Window { public : void display () override { std::cout << "Displaying a Mac window" << std::endl; } }; class AbstractFactory { public : virtual ~AbstractFactory () = default ; virtual std::unique_ptr<Button> createButton () = 0 ; virtual std::unique_ptr<Window> createWindow () = 0 ; }; class WindowsFactory : public AbstractFactory { public : std::unique_ptr<Button> createButton () override { return std::make_unique <WindowsButton>(); } std::unique_ptr<Window> createWindow () override { return std::make_unique <WindowsWindow>(); } }; class MacFactory : public AbstractFactory { public : std::unique_ptr<Button> createButton () override { return std::make_unique <MacButton>(); } std::unique_ptr<Window> createWindow () override { return std::make_unique <MacWindow>(); } }; int main () { auto windowsFactory = std::make_unique <WindowsFactory>(); auto windowsButton = windowsFactory->createButton (); auto windowsWindow = windowsFactory->createWindow (); windowsButton->paint (); windowsWindow->display (); auto macFactory = std::make_unique <MacFactory>(); auto macButton = macFactory->createButton (); auto macWindow = macFactory->createWindow (); macButton->paint (); macWindow->display (); return 0 ; }