简单的策略模式,类模版

#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>class Operation
{
public:
virtual void Algorithm() = 0;
};

template<class T>class Add:public Operation<T>
{
public:
Add (T a , T b):m_a(a),m_b(b)
{
}

void Algorithm()
{
cout<< (m_a + m_b)<<endl;
}
private:
T m_a;
T m_b;

};

template<class T>class Sub:public Operation<T>
{
public:
Sub(T a, T b):m_a(a),m_b(b)
{
}

void Algorithm()
{
cout<< (m_a - m_b)<<endl;
}
private:
T m_a;
T m_b;
};

//
template<class T>class Contex
{
public:
Contex(Operation<T>*ptr)
{
this->ptr = ptr;
}
void Algorithm()
{
ptr->Algorithm();
}
private:
Operation<T>* ptr;
};

int main()
{
int a =10;
int b = 1;
double c =10.1;
double d = 1.2;
Operation<int> *ptr = new Add<int>(a,b);
Contex<int> *contex = new Contex<int>(ptr);
contex->Algorithm();

Contex<double>*contex1 = new Contex<double>(new Sub<double>(c,d));
contex1->Algorithm();

return 0;

}

 

写的有些拙劣,主要同时为了熟悉下类模版。得搞会儿dota啦~\(≧▽≦)/~啦啦啦

转载于:https://www.cnblogs.com/kunkka123/archive/2012/06/02/2532016.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值