本文总阅读量

单例模式

简单的说就是(๑•ᴗ•๑):

  • 你点一下一个按钮(比如说:帮助->关于),你再点一次的话,它不会再弹出一个窗口。
  • 类比:
    计划生育,让你只能生一个孩子。

专业一点就是(¬_¬):

  • 保证一个类仅有一个实例,并提供一个访问它的全局访问点。

Singleton

做法

  • 构造函数私有化(类外不可以调用构造函数,便不可以创建对象)
  • 有一个私有的静态的成员变量(属性),可以是指针、引用或者对象(见“饿汉”)
  • 有一个公有的函数,可以返回一个该类对象(如果有的话直接返回,如果没有则创建)

懒汉

  • 顾名思义:需要创建时才创建对象
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
/*
* Singleton.cpp
* 单例模式
* Created on: 2016年3月11日 下午3:38:31
* Author: Wayne 13186259527@163.com
*/


#include <iostream>

using namespace std;
/**
* 懒汉:需要创建时才创建对象
*/

class Singleton {
private:
static Singleton * instance;
Singleton() {
}
public:
static Singleton * GetInstance() {
if (instance == NULL) {
//懒汉,进入函数后判断是否创建,如果未创建则创建对象
instance = new Singleton();
}
return instance;
}

};
//这里不创建
Singleton* Singleton::instance = NULL;

int main(void) {
//懒汉
Singleton * s1 = Singleton::GetInstance();
Singleton * s2 = Singleton::GetInstance();
if (s1 == s2) {
cout << "s1 == s2" << endl;
}
return 0;
}

饿汉

  • 顾名思义:从一开始加载的时候就创建对象
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
/*
* singleton2.cpp
* 单例模式 饿汉
* Created on: 2016年3月11日 下午4:24:03
* Author: Wayne 13186259527@163.com
*/


#include <iostream>
using namespace std;
class singleton {
private:
static singleton* instance;
singleton() {
}
public:
static singleton* GetInstace() {
//饿汉,直接返回已经创建好的对象
return instance;
}
};
//这里直接创建
singleton* singleton::instance = new singleton();

int main(void) {
singleton* s1 = singleton::GetInstace();
singleton* s2 = singleton::GetInstace();
if (s1 == s2) {
cout << "s1 == s2" << endl;
}
return 0;
}

本文总阅读量

适配器模式

简单的说就是(๑•ᴗ•๑):

电源适配器(把220V的交流电转换为电器能够使用的电压)

专业一点就是(⊙ˍ⊙):

想复用一些现成的类,但是接口又与复用环境要求不一致。那么使用适配器转换接口。


1、类适配器

  • [x] 适用于C++(因为它支持多继承)

ClassAdapter

适配器类继承了Target和Adaptee的方法,所以可以直接在方法中调用Adaptee的方法,实现接口的转换

2、对象适配器

ObjectAdapter

适配器类继承了Target的方法,并且含有Adaptee的对象(或者指针)。可以再方法中调用Adaptee的方法,实现接口转换

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
/*
* Adapter.cpp
*
* Created on: 2016年3月11日 下午4:48:49
* Author: Wayne 13186259527@163.com
*/


#include <iostream>
using namespace std;

class Target {
public:
virtual void Method() {
cout << "我要20V的电压" << endl;
}
};
class Adaptee {
public:
void SpecialMethod() {
cout << "我这里提供10V的电压" << endl;
}
};

//类适配器
class ClsAdapter: public Target, private Adaptee {
public:
void Method() {
cout << "类适配器" << endl;
cout << "你要20V,我给你20V" << endl;
SpecialMethod();
cout << "我给你转换为20V" << endl;
}
};

//对象适配器
class ObjAdapter: public Target {
private:
Adaptee ptr;
public:
/*ObjAdapter(Adaptee ptr){
this->ptr = ptr;
}*/

void Method() {
cout << "对象适配器" << endl;
cout << "你要20V,我给你20V" << endl;
ptr.SpecialMethod();
//ptr->SpecialMethod();
cout << "我给你转换为20V" << endl;
}
};

int main(void) {
//类适配器
ClsAdapter cls;
cls.Method();
cout << "==================" << endl;
//对象适配器
ObjAdapter obj;
obj.Method();
return 0;
}

本文总阅读量

抽象工厂模式

工厂方法模式UML类图

我感觉就是工厂方法的增强版,多了更多的操作子类,随之而来的就是更多的抽象工厂子类。每个工厂子类都可以操作对应的操作子类。并且它们使用的是统一的接口。

只需要在客户端更换抽象工厂子类,即可实现不同的操作。

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* AbstractFactory.cpp
* 抽象工厂模式
* Created on: 2016年3月11日
* Author: Administrator
*/


#include <iostream>
using namespace std;

class User {
private:
int id;
string name;
public:

int getId() const {
return id;
}

void setId(int id) {
this->id = id;
}

const string& getName() const {
return name;
}

void setName(const string& name) {
this->name = name;
}
};

class IUser {
public:

virtual void Insert(User *user) {
}
virtual void GetUser(int id) {

}
};

class SqlserverUser: public IUser {
public:

void Insert(User *user) {
cout << "在SQL server中给user表增加了一条数据" << endl;
}
void GetUser(int id) {
cout << "在SQL server中根据ID得到user表一条数据" << endl;

}
};
class AcessUser: public IUser {
public:

void Insert(User *user) {
cout << "在Acess中给user表增加了一条数据" << endl;
}
void GetUser(int id) {
cout << "在Acess中根据ID得到user表一条数据" << endl;
}
};

class Department {

};

class IDepartment {
public:

virtual void Insert(Department *deparment) {
}
virtual void GetDepartment(int id) {
}
};
class SqlserverDepartment: public IDepartment {
public:

void Insert(Department *deparment) {
cout << "在SQL server中给Department表增加了一条数据" << endl;
}
void GetDepartment(int id) {
cout << "在SQL server中根据ID得到Department表一条数据" << endl;
}
};

class AcessDepartment: public IDepartment {
public:

void Insert(Department *deparment) {
cout << "在Acess中给Department表增加了一条数据" << endl;
}
void GetDepartment(int id) {
cout << "在Acess中根据ID得到Department表一条数据" << endl;
}
};

class IFactory {
public:

virtual IUser * CreateUser() {
}
virtual IDepartment * CreateDepartment() {
}
};
class SqlserverFactory: public IFactory {
public:

IUser * CreateUser() {
return new SqlserverUser();
}
IDepartment * CreateDepartment() {
return new SqlserverDepartment();
}
};

class AcessFactory: public IFactory {
public:

IUser * CreateUser() {
return new AcessUser();
}
IDepartment * CreateDepartment() {
return new AcessDepartment();
}
};

int main(void) {
cout << "helloFactory" << endl;

User *user = new User();
Department *dep = new Department();
//在这里传入不同使用的数据库,来实现用不同数据库进行不同的操作。
IFactory *factory = new AcessFactory();
//IFactory *factory = new SqlserverFactory();

IUser *iuser = factory->CreateUser();

IDepartment *idep = factory->CreateDepartment();

iuser->Insert(user);
iuser->GetUser(1);
idep->Insert(dep);
idep->GetDepartment(1);

return 0;
}

本文总阅读量

工厂方法模式

工厂方法模式UML类图

和简单工厂的不同之处:

把逻辑判断从工厂类中移到客户端。
工厂类下多了几个子类用来创建对应的进行实际操作的
子类对象。

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* FactoryMethod.cpp
*
* 工厂方法模式
* Created on: 2016年3月13日 下午8:56:50
* Author: Wayne 13186259527@163.com
*/

#include <iostream>
#include <stdlib.h>

using namespace std;

class Operation {
private:
double x, y;
char c;
public:
Operation() {
}
Operation(double x, double y, char c) {
this->x = x;
this->y = y;
this->c = c;
}
virtual double Calc(double x, double y) {
return 0;
}
;
double GetX() {
return this->x;
}
double GetY() {
return this->y;
}
char GetC() {
return this->c;
}
};
class Add: public Operation {
public:
Add() {
}
;
double Calc(double x, double y) {
return x + y;
}
};
class Sub: public Operation {
public:
Sub() {
}
;
double Calc(double x, double y) {
return x - y;
}
};
class Mul: public Operation {
public:
Mul() {
}
;
;
double Calc(double x, double y) {
return x * y;
}
};
class Div: public Operation {
public:
Div() {
}
;
double Calc(double x, double y) {
if (y == 0) {
cout << "0不能作为被除数" << endl;
exit(0);

}
return x / y;
}
};

class Factory {
public:
virtual Operation* CreateOperation() {
return NULL;
}
;
};
class AddFactory: public Factory {
public:
Operation* CreateOperation() {
return new Add();
}
};
class SubFactory: public Factory {
public:
Operation* CreateOperation() {
return new Sub();
}
};
class MulFactory: public Factory {
public:
Operation* CreateOperation() {
return new Mul();
}
};
class DivFactory: public Factory {
public:
Operation* CreateOperation() {
return new Div();
}
};

int main(void) {
cout << "hello world" << endl;
cout << "eg:5+8" << endl;
double x, y;
char c;
cin >> x >> c >> y;
double result;

Factory *f = NULL;//指向对应的工厂
Operation *p = NULL;//指向对应的实现方法的子类
switch (c) {
case '+':
f = new AddFactory();
break;
case '-':
f = new SubFactory();
break;
case '*':
f = new MulFactory();
break;
case '/':
f = new DivFactory();
break;
}
p = f->CreateOperation();
result = p->Calc(x, y);

cout << "result = " << result << endl;

return 0;
}

本文总阅读量

这段时间准备着实习招聘,把以前懂一丢丢的设计模式又拿出来看看,这次看的书,主要是《大话设计模式》,偶尔翻一翻经典的《设计模式》。这几天看的是简单工厂模式、工厂方法模式、抽象工厂模式、策略模式。觉得它们有相似之处,所以在这里写下感想。如有错误,敬请指正(如果有人看的话✧(≖ ◡ ≖✿))

简单工厂模式
简单工厂模式UML类图

①几个子类继承基类的方法,并且各自实现。

②工厂类根据客户端传入的参数,生成对应的子类并且返回客户端。

③客户端定义的基类的指针指向生成的子类,由这个指针去调用具体方法,实现功能

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
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* EasyFactory.cpp
*
* Created on: 2016年3月11日 下午10:55:57
* Author: Wayne 13186259527@163.com
*/


/*
简单工厂模式
实现简单的加减乘除
*/

#include <iostream>
#include<stdlib.h>

using namespace std;

//操作基类
class Operation {
public:
Operation() {
}

//实现多态的虚函数
virtual double Calc(double x, double y) {
return 0;
}
;

~Operation() {
}
};
class Add: public Operation {
public:
Add() {
}
;
double Calc(double x, double y) {
return x + y;
}
~Add() {
}
};
class Sub: public Operation {
public:
Sub() {
}
;
double Calc(double x, double y) {
return x - y;
}
};
class Mul: public Operation {
public:
Mul() {
};
double Calc(double x, double y) {
return x * y;
}
};
class Div: public Operation {
public:
Div() {
};
double Calc(double x, double y) {
if (y == 0) {
cout << "0不能作为被除数" << endl;
exit(0);
}
return x / y;
}
};
//工厂类
class Factory {
public:
Factory() {
}
static Operation * Count(char c) {
Operation *q = NULL;
switch (c) //根据传入的符号来判断创建什么子类
{
case '+':
q = new Add();
break;
case '-':
q = new Sub();
break;
case '*':
q = new Mul();
break;
case '/':
q = new Div();
break;
}
return q;
}
};
int main(void) {
cout << "请输入算式:" << endl;
cout << "eg:5+8" << endl;
double x, y;
char c;
cin >> x >> c >> y;
double result;
Operation *p = Factory::Count(c);
//这里将条件传入到Factory的Count静态方法中,返回对应的子类对象
result = p->Calc(x, y); //基类指针指向它,调用对应的方法。
cout << "result = " << result << endl;
return 0;
}