大话设计模式-简单工厂模式、工厂方法模式、抽象工厂模式

简单工厂模式

概念:简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。

适合应用于具有多个分支,不同的情况需要生成对应的类,将分支移到工厂类中,减少客户端的复杂度。但如果要新添加一个产品类的话,需要修改工厂类的代码。

 1 // OperationFactory.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <string>
 7 using namespace std;
 8 
 9 class Operation{
10 private:
11     int numberA;
12     int numberB;
13 public:
14     void setNumberA(int a){
15         this->numberA = a;
16     }
17     void setNumberB(int b){
18         this->numberB = b;
19     }
20     int getNumberA(){
21         return numberA;
22     }
23     int getNumberB(){
24         return numberB;
25     }
26     virtual int getResult()=0;
27 };
28 
29 class OperationAdd:public Operation{
30 public:
31     int getResult(){
32         return (getNumberA() + getNumberB());
33     }
34 };
35 class OperationSub:public Operation{
36 public:
37     int getResult(){
38         return (getNumberA() - getNumberB());
39     }
40 };
41 class OperationMul:public Operation{
42 public:
43     int getResult(){
44         return (getNumberA() * getNumberB());
45     }
46 };
47 class OperationDiv:public Operation{
48 public:
49     int getResult(){
50         return (getNumberA() / getNumberB());
51     }
52 };
53 
54 class OperationFactory{
55 public:
56      Operation* createOperation(char ch){
57         Operation *p = NULL;
58         switch(ch){
59         case '+':
60             p = new OperationAdd();
61             break;
62         case '-':
63             p = new OperationSub();
64             break;
65         case '*':
66             p = new OperationMul();
67             break;
68         case '/':
69             p = new OperationDiv();
70             break;
71         }
72         return p;
73     }
74      //单例模式
75      static OperationFactory & getInstance(){
76          static OperationFactory of;
77          return of;
78      }
79 };
80 int _tmain(int argc, _TCHAR* argv[])
81 {
82     Operation *p;
83     p = OperationFactory::getInstance().createOperation('+');
84     p->setNumberA(2);
85     p->setNumberB(3);
86     cout << p->getResult()<< endl;
87     return 0;
88 }

 简单工厂模式在遇到新的类添加时,需要对工厂类进行修改,这未被了开放-封闭原则。

工厂方法模式:

概念:定义一个用于创建对象的接口,让子类决定实例化那一个类。工厂方法使得一个类的实例化延迟到其子类。

工厂方法模式是简单工厂模式的进一步抽象和推广,使用了多态性,保持了简单工厂模式的有点,也克服简单工厂模式的缺点。但是每增加一个类,就要对应的增加该类的工厂类。

 

  1 // OperationFactory.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <iostream>
  6 #include <string>
  7 using namespace std;
  8 
  9 class Operation{
 10 private:
 11     int numberA;
 12     int numberB;
 13 public:
 14     void setNumberA(int a){
 15         this->numberA = a;
 16     }
 17     void setNumberB(int b){
 18         this->numberB = b;
 19     }
 20     int getNumberA(){
 21         return numberA;
 22     }
 23     int getNumberB(){
 24         return numberB;
 25     }
 26     virtual int getResult()=0;
 27 };
 28 
 29 class OperationAdd:public Operation{
 30 public:
 31     int getResult(){
 32         return (getNumberA() + getNumberB());
 33     }
 34 };
 35 class OperationSub:public Operation{
 36 public:
 37     int getResult(){
 38         return (getNumberA() - getNumberB());
 39     }
 40 };
 41 class OperationMul:public Operation{
 42 public:
 43     int getResult(){
 44         return (getNumberA() * getNumberB());
 45     }
 46 };
 47 class OperationDiv:public Operation{
 48 public:
 49     int getResult(){
 50         return (getNumberA() / getNumberB());
 51     }
 52 };
 53 
 54 class IOperationFactory{
 55 public:
 56     virtual Operation * createOperation()=0;
 57 };
 58 class AddFactory:public IOperationFactory{
 59 
 60     Operation* createOperation(){
 61         return new OperationAdd();
 62     }
 63 };
 64 class SubFactory:public IOperationFactory{
 65     Operation* createOperation(){
 66         return new OperationSub();
 67     }
 68 };
 69 class MulFactory:public IOperationFactory{
 70     Operation* createOperation(){
 71         return new OperationMul();
 72     }
 73 };
 74 class DivFactory:public IOperationFactory{
 75     Operation* createOperation(){
 76         return new OperationDiv();
 77     }
 78 };
 79 //简单工厂模式
 80 class SimpleFactory{
 81 public:
 82     IOperationFactory *createOperationFactory(char ch){
 83         IOperationFactory *pf;
 84         switch (ch)
 85         {
 86         case '+': pf = new AddFactory();break;
 87         case '-': pf = new SubFactory();break;
 88         case '*': pf = new MulFactory();break;
 89         case '/': pf = new DivFactory();break;
 90         }
 91         return pf;
 92     }
 93     static SimpleFactory& createSimpleFactory(){
 94         static SimpleFactory sf;
 95         return sf;
 96     }
 97 };
 98 int _tmain(int argc, _TCHAR* argv[])
 99 {
100     IOperationFactory *pf = SimpleFactory::createSimpleFactory().createOperationFactory('+');
101     Operation *p = pf->createOperation();
102     p->setNumberA(2);
103     p->setNumberB(3);
104     cout << p->getResult()<< endl;
105     return 0;
106 }

 抽象工厂模式

抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类。

适用场景:易于交换产品系列。让具体的创建实例过程与客服端分离,客户端通过他们的抽象接口操纵实例,产品的具体类名被具体工厂实现分离,不会出现在客户代码中。

 

  1 // OperationFactory.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <iostream>
  6 #include <string>
  7 using namespace std;
  8 
  9 class IUser{
 10 
 11 public:
 12     virtual void inserUser()=0;
 13     virtual IUser* getUserByID(int id)=0;
 14 };
 15 class IDepartment{
 16 public:
 17     virtual void inserDepartment()=0;
 18     virtual IDepartment* getDepartmentByID(int id)=0;
 19 };
 20 class sqlUser:public IUser{
 21 public:
 22     void inserUser(){
 23         cout << "插入sqlUser"<< endl;
 24     }
 25     IUser *getUserByID(int id){
 26         cout << "得到id为"<<id<<"的sqluser"<<endl;
 27         return NULL;
 28     }
 29 };
 30 class oracleUser:public IUser{
 31 public:
 32     void inserUser(){
 33         cout << "插入oracleUser"<< endl;
 34     }
 35     IUser *getUserByID(int id){
 36         cout << "得到id为"<<id<<"的oracleUser"<<endl;
 37         return NULL;
 38     }
 39 };
 40 class sqlDepartment:public IDepartment{
 41 public:
 42     void inserDepartment(){
 43         cout << "插入sqlDepartment"<< endl;
 44     }
 45     IDepartment *getDepartmentByID(int id){
 46         cout << "得到id为"<<id<<"的sqlDepartment"<<endl;
 47         return NULL;
 48     }
 49 };
 50 class oracleDepartment:public IDepartment{
 51 public:
 52     void inserDepartment(){
 53         cout << "插入oracleDepartment"<< endl;
 54     }
 55     IDepartment *getDepartmentByID(int id){
 56         cout << "得到id为"<<id<<"的oracleDepartment"<<endl;
 57         return NULL;
 58     }
 59 };
 60 
 61 /*
 62 不使用简单工厂模式
 63 class IFactory{
 64 public :
 65     virtual IUser* createUser()=0;
 66     virtual IDepartment *createDepartment()=0;
 67 };
 68 class sqlFactory:public IFactory{
 69 public:
 70     IUser *createUser(){
 71         cout << "create sqlUser!"<<endl;
 72         return new sqlUser();
 73     }
 74     IDepartment *createDepartment(){
 75         cout << "create sqlDepartment!"<<endl;
 76         return new sqlDepartment();
 77     }
 78 };
 79 class oracleFactory:public IFactory{
 80 public:
 81     IUser *createUser(){
 82         cout << "create oracleUser!"<<endl;
 83         return new oracleUser();
 84     }
 85     IDepartment *createDepartment(){
 86         cout << "create oracleDepartment!"<<endl;
 87         return new oracleDepartment();
 88     }
 89 };*/
 90 //简单工厂模式
 91 class DataAccess{
 92 public:
 93 
 94     IUser * createUser(char ch){
 95         cout << "createUser" << endl;
 96         IUser *u;
 97         switch(ch){
 98         case 's': u = new sqlUser();break;
 99         case 'o': u = new oracleUser();break;
100         }
101         return u;
102     }
103 
104     IDepartment * createDepartment(char ch){
105         cout << "createDepartment" << endl;
106         IDepartment *d;
107         switch(ch){
108         case 's': d = new sqlDepartment();break;
109         case 'o': d = new oracleDepartment();break;
110         }
111         return d;
112     }
113     static DataAccess & createDataAccess(){
114         static DataAccess data;
115         return data;
116     }
117 };
118 int _tmain(int argc, _TCHAR* argv[])
119 {
120     //IFactory *f = new sqlFactory();
121     //IFactory *f = new oracleFactory();
122     char ch = 's';
123     IUser *u = DataAccess::createDataAccess().createUser(ch);
124     IDepartment *d = DataAccess::createDataAccess().createDepartment(ch);
125     u->inserUser();
126     d->inserDepartment();
127     return 0;
128 }

 

转载于:https://www.cnblogs.com/zhangjxblog/p/8939967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值