设计模式学习之简单工厂模式和策略模式

 1.简单工厂模式
先看示例代码,然后结合例子说明问题,理论联系实例是初学者入门的最好方法

下面是实现一个简单计算器的小例子

Operation.h

#include <iostream>
using namespace std;

class Operation{
private:
	double _numberA;
	double _numberB;
public:
	double getNumberA(){
		return _numberA;
	}
	void   setNumberA(double value){
		_numberA=value;
	}
	
	double getNumberB(){
		return _numberB;
	}
	void   setNumberB(double value){
		_numberB=value;
	}

	virtual double GetResult(){
		double result = 0;
		return result;
	}
};

inheritOper.h

#include "Operation.h"

class OperationAdd:public Operation{
	double GetResult(){
		double result = getNumberA()+getNumberB();
		return result;
	}
};

class OperationSub:public Operation{
	double GetResult(){
		double result = getNumberA()-getNumberB();
		return result;
	}
};

class OperationMul:public Operation{
	double GetResult(){
		double result = getNumberA()*getNumberB();
		return result;
	}
};

class OperationDiv:public Operation{
	double GetResult(){
		if(getNumberB()==0){
			cout<<"除数不能为0"<<endl;
			exit(-1);
		}
		double result = getNumberA()/getNumberB();
		return result;
	}
};

OperationFactory.h

#include "inheritOper.h"

class OperationFactory{
public:
	Operation* createOperate(char operate){
		Operation *oper;
		switch(operate){
			case '+':
				oper = new OperationAdd();
				break;
			case '-':
				oper = new OperationSub();
				break;
			case '*':
				oper = new OperationMul();
				break;
			case '/':
				oper = new OperationDiv();
				break;
		}
		return oper;
	}
};

main.cpp

#include "OperationFactory.h"

void main(){
	OperationFactory a;
	Operation *oper=a.createOperate('*');
	oper->setNumberA(3);
	oper->setNumberB(4);
	double ret=oper->GetResult();
	cout<<ret<<endl;
}

从上面的例子可以看出,具体的运算类对象创建被封装在工厂类OperationFactory 中,其实例化也延迟到了这个类中,这个例子中也体现了继承和多态
Factory模式的两个最重要的功能:
1)定义创建对象的接口,封装了对象的创建;
2)使得具体化类的工作延迟到了子类中。

简单工厂模式主要用于在工厂中实例化对象,新添加类时,不会影响以前的代码。核心思想是用一个工厂来根据输入的条件产生不同的类,然后根据不同类的virtual函数得到不同的结果,适用于不同情况创建不同的类时,缺点是客户端需要知道基类和工厂类,耦合性差

现在想来,lucene/solr在处理分词时,就用到了工厂模式,当时不知道这些分词类为什么用factory来结尾,原来是工厂模式的一种应用,从这里足以体现设计模式在框架中的应用。

2.策略模式

strategy.h

#include <iostream>
using namespace std;

class strategy{
public:
	virtual void testInterface(){};
};

class strategyA:public strategy{
	void testInterface(){
		cout<<"test A"<<endl;
	}
};

class strategyB:public strategy{
	void testInterface(){
		cout<<"test B"<<endl;
	}
};

class strategyC:public strategy{
	void testInterface(){
		cout<<"test C"<<endl;
	}
};

context.h

#include "strategy.h"

class context{
private:
	strategy *stra;
public:
	context(strategy *str){
		stra=str;
	}
	void contextInterface(){
		stra->testInterface();
	}
};

main.cpp

#include "context.h"

void main(){
	context *cont=new context(new strategyB());
	cont->contextInterface();
}

从例子中可以看到,将子类中的相同方法testInterface放在了context中调用。策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合

策略模式是用来封装算法的,只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性

策略模式适合类中的成员以方法为主,算法经常变动,简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。策略模式和简单工厂模式基本相同,但简单工厂模式只能解决对象创建问题,对于经常变动的算法应使用策略模式。策略模式的缺点是客户端要做出判断

策略模式与简单工厂模式结合后,客户端只需访问context类,而不用知道其它任何类信息,实现了低耦合

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值