设计模式(抽象工厂模型)

2017-01-04

版权声明:本文为作者原创文章,可以随意转载,但必须在明确位置表明出处!!!

设计模式:

设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。

设计模式的六大原则:

  1. 开闭原则:

    对扩展开放,对修改关闭。在程序需要进行扩展的时候,不能去修改原有的代码,实现一种热插拔的效果。这样有利于程序的可维护性和易升级性。

  2. 里氏代换原则:

    里氏代换原则是面向对象设计的基本原则之一,里氏代换原则中说,任何基类可以出现的地方,子类一定可以出现,里氏代换原则是继承复用的基础,只有当派生类可以替换基类,且原有功能不被影响时基类才能真正被复用,而派生类也能够在基类的基础上新增新的行为。里氏代换原则是对开闭原则的补充,实现开闭原则的关键步骤就是抽象化,而基类和子类的继承关系就是抽象化的具体实现。

  3. 依赖倒转原则:

    这个原则是开闭原则的基础,它是针对接口编程,依赖抽象而不依赖具体

  4. 接口隔离原则:

    使用多个隔离的接口,比使用单个接口要好。它的另外一个意识是降低类之间的耦合度。

  5. 迪米特法则:

    迪米特发展又称最少知道原则,一个实体应当尽量少的与其它实体之间发生相互作用,使得系统功能模块相对独立。

  6. 合成复用原则:

    尽量使用合成/聚合的方式,而不是使用继承方式。

抽象工厂模型

抽象工厂模型是围绕一个超级工厂去创建其它工厂,工厂和工厂之间相互独立,在抽象工厂模式中,超级工厂只负责创建一个相关对象的工厂,不需要显示的指定他们的类,每个生成的工厂都能够按照工厂模式提供对象

优点

当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

缺点

产品族扩展非常困难,当要增加某一系列产品时,不仅要在抽象类里加代码,还需要在具体实现类里添加相应代码

UML类图

从UML可以看出,若我们要新增背包这类产品是,我们需要新增背包这个生产抽象类,同时还需要在macFactory,lenovoFactory工厂中新增相应的生产类,并且IFactory也需要新增相应的生产类。所以抽象工程模式对产品扩展非常困难,但是保证了客户端使用同一个产品族中的对象,比如你需要mac系类的产品是你只需要实例化一个macFactory就可以使用mac系类的产品了。

实现

根据类图一步一步实现,首先实现IFactory超级工厂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef __IFACTORY__
#define __IFACTORY__
#include "keyBoardProduct.h"
#include "mouseProduct.h"
class IFactory
{
public:
IFactory(){};
virtual ~IFactory(){};
public:
virtual CKeyBoardProduct* createKeyBoardProduct() = 0;
virtual CMouseProduct* createMouseProduct() = 0;
};
#endif

实现lenovoFactory工厂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef __LENOVOFACTORY__
#define __LENOVOFACTORY__
#include "IFactory.h"
class CLenovoFactory : public IFactory
{
public:
CLenovoFactory();
~CLenovoFactory();
public:
virtual CKeyBoardProduct* createKeyBoardProduct();
virtual CMouseProduct* createMouseProduct();
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "lenovoFactory.h"
#include "lenovoStyleKeyBoard.h"
#include "lenovoStyleMouse.h"
CLenovoFactory::CLenovoFactory()
{
}
CLenovoFactory::~CLenovoFactory()
{
}
CKeyBoardProduct* CLenovoFactory::createKeyBoardProduct()
{
return new CLenovoStyleKeyBoard();
}
CMouseProduct* CLenovoFactory::createMouseProduct()
{
return new CLenovoStyleMouse();
}

实现macFactory工厂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __MACFACTORY__
#define __MACFACTORY__
#include "IFactory.h"
class CMacFactory : public IFactory
{
public:
CMacFactory();
~CMacFactory();
public:
virtual CKeyBoardProduct* createKeyBoardProduct();
virtual CMouseProduct* createMouseProduct();
};
#endif
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
#include "macFactory.h"
#include "macStyleKeyBoard.h"
#include "macStyleMouse.h"
CMacFactory::CMacFactory()
{
}
CMacFactory::~CMacFactory()
{
}
CKeyBoardProduct* CMacFactory::createKeyBoardProduct()
{
return new CMacStyleKeyBoard();
}
CMouseProduct* CMacFactory::createMouseProduct()
{
return new CMacSytleMouse();
}

创建抽象类KeyBoardProduct

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef __KEYBOARDRODUCT__
#define __KEYBOARDRODUCT__
class CKeyBoardProduct
{
public:
CKeyBoardProduct(){};
virtual ~CKeyBoardProduct(){};
public:
virtual void createKeyBoard() = 0;
};
#endif

创建抽象类MouseProduct

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef __MOUSEPRODUCT__
#define __MOUSEPRODUCT__
class CMouseProduct
{
public:
CMouseProduct(){};
virtual ~CMouseProduct(){};
public:
virtual void createMouse() = 0;
};
#endif

实现抽象类lenovoStyleKeyBoard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __LENOVOSTYLEKEYBOARD__
#define __LENOVOSTYLEKEYBOARD__
#include "keyBoardProduct.h"
class CLenovoStyleKeyBoard : public CKeyBoardProduct
{
public:
CLenovoStyleKeyBoard();
~CLenovoStyleKeyBoard();
public:
virtual void createKeyBoard();
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "lenovoStyleKeyBoard.h"
#include <iostream>
CLenovoStyleKeyBoard::CLenovoStyleKeyBoard()
{
}
CLenovoStyleKeyBoard::~CLenovoStyleKeyBoard()
{
}
void CLenovoStyleKeyBoard::createKeyBoard()
{
std::cout << "lenovo style keyboard" << std::endl;
}

实现抽象类macStyleKeyBoard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __MACSTYLEKEYBOARD__
#define __MACSTYLEKEYBOARD__
#include "keyBoardProduct.h"
class CMacStyleKeyBoard : public CKeyBoardProduct
{
public:
CMacStyleKeyBoard();
~CMacStyleKeyBoard();
public:
virtual void createKeyBoard();
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "macStyleKeyBoard.h"
#include <iostream>
CMacStyleKeyBoard::CMacStyleKeyBoard()
{
}
CMacStyleKeyBoard::~CMacStyleKeyBoard()
{
}
void CMacStyleKeyBoard::createKeyBoard()
{
std::cout << "mac style keyboard" << std::endl;
}

实现抽象类lenovoStyleMouse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __LENOVOSYTLEMOUSE__
#define __LENOVOSYTLEMOUSE__
#include "mouseProduct.h"
class CLenovoStyleMouse : public CMouseProduct
{
public:
CLenovoStyleMouse();
~CLenovoStyleMouse();
public:
virtual void createMouse();
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "lenovoStyleMouse.h"
#include <iostream>
CLenovoStyleMouse::CLenovoStyleMouse()
{
}
CLenovoStyleMouse::~CLenovoStyleMouse()
{
}
void CLenovoStyleMouse::createMouse()
{
std::cout << "lenovo style mouse" << std::endl;
}

实现抽象类macStyleMouse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __MACSYTLEMOUSE__
#define __MACSYTLEMOUSE__
#include "mouseProduct.h"
class CMacSytleMouse : public CMouseProduct
{
public:
CMacSytleMouse();
~CMacSytleMouse();
public:
virtual void createMouse();
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "macStyleMouse.h"
#include <iostream>
CMacSytleMouse::CMacSytleMouse()
{
}
CMacSytleMouse::~CMacSytleMouse()
{
}
void CMacSytleMouse::createMouse()
{
std::cout << "mac style mouse" << std::endl;
}

调用实例

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
#include <iostream>
#include "IFactory.h"
#include "macFactory.h"
#include "mouseProduct.h"
#include "keyBoardProduct.h"
#include "lenovoFactory.h"
int main(void)
{
//获取一个苹果风格的鼠标
IFactory *factory = new CMacFactory();
CMouseProduct * macMouse = factory->createMouseProduct();
macMouse->createMouse();
//获取一个苹果风格的键盘
CKeyBoardProduct *keyBoard = factory->createKeyBoardProduct();
keyBoard->createKeyBoard();
//获取一个联想风格的鼠标
IFactory *lenovoFactory = new CLenovoFactory();
CMouseProduct *lenovoMouse = lenovoFactory->createMouseProduct();
lenovoMouse->createMouse();
//获取一个联想风格的键盘
CKeyBoardProduct *lenovoKeyBoard = lenovoFactory->createKeyBoardProduct();
lenovoKeyBoard->createKeyBoard();
if(0 != factory)
{
delete factory;
factory = 0;
}
if(0 != macMouse)
{
delete macMouse;
macMouse = 0;
}
if(0 != keyBoard)
{
delete keyBoard;
keyBoard = 0;
}
if(0 != lenovoFactory)
{
delete lenovoFactory;
lenovoFactory = 0;
}
if(0 != lenovoMouse)
{
delete lenovoMouse;
lenovoMouse = 0;
}
if(0 != lenovoKeyBoard)
{
delete lenovoKeyBoard;
lenovoKeyBoard = 0;
}
return 0;
}

项目文件:https://github.com/Gavinxyj/designPattern


推荐我的微信公众号:爱做饭的老谢


上一篇:2016总结,2017目标
下一篇:设计模式(策略模式)