一、有map,无unique_ptr版本

(1)代码

Factory.h

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
95
96
#ifndef FACTORY_H
#define FACTORY_H
#include <iostream>
#include <string>
#include <memory>
#include <map>
using namespace std;

//有map

//抽象产品类
class Products
{
public:
//产品方法
virtual void productMethod() = 0;
};
//car产品类
class car : public Products
{
public:
void productMethod()
{
cout << " car " << endl;
}
};

//bus产品类
class bus : public Products
{
public:
void productMethod()
{
cout << " bus " << endl;
}
};


//抽象工厂类
class BaseFactory
{
public:
virtual Products* create() = 0;
public:
static map<string, BaseFactory*> fac; // key value
static Products* create_product(const string& name)
{
if (fac.find(name) != fac.end())
{
//fac[name]是name工厂地址
return fac[name]->create();
}
else
{
return NULL;
}
}
};
//类外初始化
map<string, BaseFactory*> BaseFactory::fac;

//car工厂类
class carFactory : public BaseFactory
{
public:
Products* create()
{
return new car();
}
};

//bus工厂类
class busFactory : public BaseFactory
{
public:
Products* create()
{
return new bus();
}
};

//使用一个初始化类对BaseFactory::map<string, BaseFactory*>fac进行初始化
class Init_BaseFac
{
static Init_BaseFac m_init;
public:
Init_BaseFac()
{
BaseFactory::fac["car"] = new carFactory();
BaseFactory::fac["bus"] = new busFactory();
}
};

//避免使用Init_BaseFac类手动初始化
Init_BaseFac Init_BaseFac::m_init;


client.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//有map,无unique_ptr
#include "Factory.h"

void fun()
{
Products* p = BaseFactory::create_product("car");
p->productMethod();

p = BaseFactory::create_product("bus");
p->productMethod();
delete p;
}


int main()
{
fun();
return 0;
}

(2)结果

在这里插入图片描述


二、有map,有unqiue_ptr版本

(1)代码

Factory.h

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
95
96
#ifndef FACTORY_H
#define FACTORY_H
#include <iostream>
#include <string>
#include <memory>
#include <map>
using namespace std;

//有map,有unique_ptr

//抽象产品类
class Products
{
public:
//产品方法
virtual void productMethod() = 0;
};
//car产品类
class car : public Products
{
public:
void productMethod()
{
cout << " car " << endl;
}
};

//bus产品类
class bus : public Products
{
public:
void productMethod()
{
cout << " bus " << endl;
}
};


//抽象工厂类
class BaseFactory
{
public:
virtual unique_ptr<Products> create() = 0;
public:
static map<string, unique_ptr<BaseFactory>> fac; // key value
static unique_ptr<Products> create_product(const string& name)
{
if (fac.find(name) != fac.end())
{
//fac[name]是name工厂地址
return fac[name]->create();
}
else
{
return NULL;
}
}
};
//类外初始化
map<string, unique_ptr<BaseFactory>> BaseFactory::fac;

//car工厂类
class carFactory : public BaseFactory
{
public:
unique_ptr<Products>create()
{
return unique_ptr<Products>(new car());
}
};

//bus工厂类
class busFactory : public BaseFactory
{
public:
unique_ptr<Products>create()
{
return unique_ptr<Products>(new bus());
}
};

//使用一个初始化类对BaseFactory::map<string, BaseFactory*>fac进行初始化
class Init_BaseFac
{
static Init_BaseFac m_init;
public:
Init_BaseFac()
{
BaseFactory::fac["car"] = unique_ptr<BaseFactory>(new carFactory());
BaseFactory::fac["bus"] = unique_ptr<BaseFactory>(new busFactory());
}
};

//避免使用Init_BaseFac类手动初始化
Init_BaseFac Init_BaseFac::m_init;

client.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Factory.h"

void fun()
{
unique_ptr<Products> p = BaseFactory::create_product("car");
p->productMethod();

p = BaseFactory::create_product("bus");
p->productMethod();
}

int main()
{
fun();
return 0;
}

(2)结果

在这里插入图片描述