设计模式 - Prototype模式(原型模式)
作用:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
UML结构图:
抽象基类:
1)Prototype:虚拟基类,所有原型的基类,提供Clone接口函数接口函数:
1)Prototype::Clone函数:纯虚函数,根据不同的派生类来实例化创建对象.
解析:
Prototype模式其实就是常说的"虚拟构造函数"一个实现,C++的实现机制中并没有支持这个特性,但是通过不同派生类实现的Clone接口函数可以完成与"虚拟构造函数"同样的效果.举一个例子来解释这个模式的作用,假设有一家店铺是配钥匙的,他对外提供配制钥匙的服务(提供Clone接口函数),你需要配什么钥匙它不知道只是提供这种服务,具体需要配什么钥匙只有到了真正看到钥匙的原型才能配好.也就是说,需要一个提供这个服务的对象,同时还需要一个原型(Prototype),不然不知道该配什么样的钥匙.
代码实现:
Prototype.h
1
2 #ifndef PROTOTYPE_H
3 #define PROTOTYPE_H
4
5 // 虚拟基类,所有原型的基类,提供Clone接口函数
6 class Prototype
7 {
8 public:
9 Prototype(){}
10 virtual ~Prototype(){}
11
12 virtual Prototype* Clone() = 0;
13 };
14
15 // 派生自Prototype,实现Clone方法
16 class ConcreatePrototype1
17 : public Prototype
18 {
19 public:
20 ConcreatePrototype1();
21 ConcreatePrototype1(const ConcreatePrototype1&);
22 virtual ~ConcreatePrototype1();
23
24 virtual Prototype* Clone();
25 };
26
27 // 派生自Prototype,实现Clone方法
28 class ConcreatePrototype2
29 : public Prototype
30 {
31 public:
32 ConcreatePrototype2();
33 ConcreatePrototype2(const ConcreatePrototype2&);
34 virtual ~ConcreatePrototype2();
35
36 virtual Prototype* Clone();
37 };
38
39 #endif
2 #ifndef PROTOTYPE_H
3 #define PROTOTYPE_H
4
5 // 虚拟基类,所有原型的基类,提供Clone接口函数
6 class Prototype
7 {
8 public:
9 Prototype(){}
10 virtual ~Prototype(){}
11
12 virtual Prototype* Clone() = 0;
13 };
14
15 // 派生自Prototype,实现Clone方法
16 class ConcreatePrototype1
17 : public Prototype
18 {
19 public:
20 ConcreatePrototype1();
21 ConcreatePrototype1(const ConcreatePrototype1&);
22 virtual ~ConcreatePrototype1();
23
24 virtual Prototype* Clone();
25 };
26
27 // 派生自Prototype,实现Clone方法
28 class ConcreatePrototype2
29 : public Prototype
30 {
31 public:
32 ConcreatePrototype2();
33 ConcreatePrototype2(const ConcreatePrototype2&);
34 virtual ~ConcreatePrototype2();
35
36 virtual Prototype* Clone();
37 };
38
39 #endif
Prototype.cpp
1 #include "Prototype.h"
2 #include <iostream>
3
4 ConcreatePrototype1::ConcreatePrototype1()
5 {
6 std::cout << "construction of ConcreatePrototype1\n";
7 }
8
9 ConcreatePrototype1::~ConcreatePrototype1()
10 {
11 std::cout << "destruction of ConcreatePrototype1\n";
12 }
13
14 ConcreatePrototype1::ConcreatePrototype1(const ConcreatePrototype1&)
15 {
16 std::cout << "copy construction of ConcreatePrototype1\n";
17 }
18
19 Prototype* ConcreatePrototype1::Clone()
20 {
21 return new ConcreatePrototype1(*this);
22 }
23
24 ConcreatePrototype2::ConcreatePrototype2()
25 {
26 std::cout << "construction of ConcreatePrototype2\n";
27 }
28
29 ConcreatePrototype2::~ConcreatePrototype2()
30 {
31 std::cout << "destruction of ConcreatePrototype2\n";
32 }
33
34 ConcreatePrototype2::ConcreatePrototype2(const ConcreatePrototype2&)
35 {
36 std::cout << "copy construction of ConcreatePrototype2\n";
37 }
38
39 Prototype* ConcreatePrototype2::Clone()
40 {
41 return new ConcreatePrototype2(*this);
42 }
2 #include <iostream>
3
4 ConcreatePrototype1::ConcreatePrototype1()
5 {
6 std::cout << "construction of ConcreatePrototype1\n";
7 }
8
9 ConcreatePrototype1::~ConcreatePrototype1()
10 {
11 std::cout << "destruction of ConcreatePrototype1\n";
12 }
13
14 ConcreatePrototype1::ConcreatePrototype1(const ConcreatePrototype1&)
15 {
16 std::cout << "copy construction of ConcreatePrototype1\n";
17 }
18
19 Prototype* ConcreatePrototype1::Clone()
20 {
21 return new ConcreatePrototype1(*this);
22 }
23
24 ConcreatePrototype2::ConcreatePrototype2()
25 {
26 std::cout << "construction of ConcreatePrototype2\n";
27 }
28
29 ConcreatePrototype2::~ConcreatePrototype2()
30 {
31 std::cout << "destruction of ConcreatePrototype2\n";
32 }
33
34 ConcreatePrototype2::ConcreatePrototype2(const ConcreatePrototype2&)
35 {
36 std::cout << "copy construction of ConcreatePrototype2\n";
37 }
38
39 Prototype* ConcreatePrototype2::Clone()
40 {
41 return new ConcreatePrototype2(*this);
42 }
Main.cpp
1 #include "Prototype.h"
2 #include <stdlib.h>
3
4 int main()
5 {
6 Prototype* pPrototype1 = new ConcreatePrototype1();
7 Prototype* pPrototype2 = pPrototype1->Clone();
8
9 Prototype* pPrototype3 = new ConcreatePrototype2();
10 Prototype* pPrototype4 = pPrototype3->Clone();
11
12 delete pPrototype1;
13 delete pPrototype2;
14 delete pPrototype3;
15 delete pPrototype4;
16
17 system("pause");
18
19 return 0;
20 }
2 #include <stdlib.h>
3
4 int main()
5 {
6 Prototype* pPrototype1 = new ConcreatePrototype1();
7 Prototype* pPrototype2 = pPrototype1->Clone();
8
9 Prototype* pPrototype3 = new ConcreatePrototype2();
10 Prototype* pPrototype4 = pPrototype3->Clone();
11
12 delete pPrototype1;
13 delete pPrototype2;
14 delete pPrototype3;
15 delete pPrototype4;
16
17 system("pause");
18
19 return 0;
20 }