策略模式 商场促销

策略模式 <wbr>商场促销
策略模式 <wbr>商场促销

 

封装的简单策略类

#ifndef _STRATEGE_H_

#define _STRATEGE_H_

#include <iostream>

using namespace std;

class Stratege

{

public:

       virtual void AlgorithmInterface(){}

};

class ConcreteStrategeA : public Stratege

{

public:

       void AlgorithmInterface() { cout<<"aaaaaaa"<<endl; }

};

class ConcreteStrategeB : public Stratege

{

public:

       void AlgorithmInterface() { cout<<"cccccccc"<<endl; }

};

///

class Context

{

public:

       Context(Stratege *stratege){ this->stratege = stratege;}

       void ContextInterface()

       {

              this->stratege->AlgorithmInterface();

       }

private:

       Stratege *stratege;

};

 

 

 

#endif//~_STRATEGE_H_

/ main

#include "stdafx.h"

#include "Strategy.h"

 

int main(int argc, char* argv[])

{

       Context *context = NULL;

       context = new Context(new ConcreteStrategeB());

       context->ContextInterface();

       return 0;

}

/之前的cash

#ifndef _STRATEGE_H_

#define _STRATEGE_H_

#include <iostream>

using namespace std;

//现金

 

 

//策略类和cash简单工厂类区别

#ifndef _STRATEGE_H_

#define _STRATEGE_H_

#include <iostream>

using namespace std;

//现金

class CashSuper

{

public:

       virtual double acceptCash(double money){return 0;}

};

class CashNormal : public CashSuper

{

public:

       CashNormal(){}

       virtual double acceptCash(double money){return money;}

};

class CashRebate : public CashSuper

{

public:

       CashRebate(){}

       CashRebate(int rate){this->rate = rate;}

       virtual double acceptCash(double money){return money*rate;}

private:

       int rate;

};

class CashReturn : public CashSuper

{

public:

       CashReturn(){}

       CashReturn(int a ,int b){this->a=a;this->b=b;}

       double acceptCash(double money){ if(a>b)return (money+a)/2*b; return money;}

private:

       int a,b;

};

//现金工厂类

class CashFactory

{

public:

       CashSuper * CashCreateFactory(int type)

       {

              switch(type)

              {

              case 0: super = new CashNormal();break;

              case 1: super = new CashRebate();break;

              case 2: super = new CashReturn();break;

              default: break;

              }

              return super;

       }

 

private:

       CashSuper *super;

};

//感觉策略模式就是给简单工厂的生成,放到了构造函数中,下面的和策略模式一样

class CashContext

{

public:

 

       CashContext(CashSuper *super){this->super = super;}

       double GetResult(double money)

       {

              return this->super->acceptCash(money);

       }

private:

       CashSuper *super;

};

/*

main()

{

       CashContext cc;

       cc = new CashContext(new CashReturn());

       cc.getResult(30);

}

下面的图才是真正实现的,只需要认识一个类就行了。减少了耦合度。

策略模式 <wbr>商场促销

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值