设计模式
目录:
设计模式
工厂模式
工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式,而无需指定将要创建的对象的确切类。以下是一个简单的工厂模式示例,包括了工厂类的定义和使用步骤。
python代码:
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
# 定义产品接口
class Product:
def operation(self):
pass
# 具体产品A
class ConcreteProductA(Product):
def operation(self):
return "Result of ConcreteProductA"
# 具体产品B
class ConcreteProductB(Product):
def operation(self):
return "Result of ConcreteProductB"
# 工厂类
class Creator:
def factory_method(self):
pass
def some_operation(self):
product = self.factory_method()
return f"Creator: The same creator's code has just worked with {product.operation()}"
# 具体工厂A
class ConcreteCreatorA(Creator):
def factory_method(self):
return ConcreteProductA()
# 具体工厂B
class ConcreteCreatorB(Creator):
def factory_method(self):
return ConcreteProductB()
# 使用步骤
def client_code(creator):
print("Client: I'm not aware of the creator's class, but it still works.\n"
f"{creator.some_operation()}", end="")
# 客户端代码
if __name__ == "__main__":
print("App: Launched with the ConcreteCreatorA.")
client_code(ConcreteCreatorA())
print("\n")
print("App: Launched with the ConcreteCreatorB.")
client_code(ConcreteCreatorB())
在这个示例中,我们定义了一个Product接口和两个具体的产品类ConcreteProductA和ConcreteProductB。然后,我们定义了一个Creator工厂类和两个具体的工厂类ConcreteCreatorA和ConcreteCreatorB,每个具体工厂类都实现了factory_method来创建对应的具体产品。
客户端代码client_code接受一个Creator对象,并调用其some_operation方法,该方法内部会调用factory_method来创建产品并执行产品的操作。客户端不需要知道具体创建的是哪个产品,这使得客户端代码与产品类的具体实现解耦。
在if __name__ == "__main__":部分,我们创建了两个具体的工厂对象,并使用client_code函数来演示如何使用工厂模式。
C++代码:
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
#include <iostream>
#include <string>
// 定义产品接口
class Product {
public:
virtual ~Product() {}
virtual std::string Operation() const = 0;
};
// 具体产品A
class ConcreteProductA : public Product {
public:
std::string Operation() const override {
return "Result of ConcreteProductA";
}
};
// 具体产品B
class ConcreteProductB : public Product {
public:
std::string Operation() const override {
return "Result of ConcreteProductB";
}
};
// 工厂类
class Creator {
public:
virtual ~Creator() {}
virtual Product* FactoryMethod() const = 0;
std::string SomeOperation() const {
Product* product = this->FactoryMethod();
return "Creator: The same creator's code has just worked with " + product->Operation();
}
};
// 具体工厂A
class ConcreteCreatorA : public Creator {
public:
Product* FactoryMethod() const override {
return new ConcreteProductA();
}
};
// 具体工厂B
class ConcreteCreatorB : public Creator {
public:
Product* FactoryMethod() const override {
return new ConcreteProductB();
}
};
// 使用步骤
void ClientCode(const Creator& creator) {
std::cout << "Client: I'm not aware of the creator's class, but it still works.\n"
<< creator.SomeOperation() << std::endl;
}
// 客户端代码
int main() {
std::cout << "App: Launched with the ConcreteCreatorA." << std::endl;
Creator* creator = new ConcreteCreatorA();
ClientCode(*creator);
delete creator;
std::cout << std::endl;
std::cout << "App: Launched with the ConcreteCreatorB." << std::endl;
creator = new ConcreteCreatorB();
ClientCode(*creator);
delete creator;
return 0;
}
在这个C++示例中,我们定义了一个Product接口和两个具体的产品类ConcreteProductA和ConcreteProductB。然后,我们定义了一个Creator工厂类和两个具体的工厂类ConcreteCreatorA和ConcreteCreatorB,每个具体工厂类都实现了FactoryMethod来创建对应的具体产品。
客户端代码ClientCode接受一个Creator对象的引用,并调用其SomeOperation方法,该方法内部会调用FactoryMethod来创建产品并执行产品的操作。客户端不需要知道具体创建的是哪个产品,这使得客户端代码与产品类的具体实现解耦。
在main函数中,我们创建了两个具体的工厂对象,并使用ClientCode函数来演示如何使用工厂模式。请注意,在C++中,我们需要手动管理内存,因此在创建和删除对象时需要使用new和delete。
参考1234
-
来源:Chatgpt ↩︎
-
[设计模式 单例模式](https://mp.weixin.qq.com/s/0ggXLr5YBZbpb6oU_e0_0A)