码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式:Strategy(策略)

时间:2020-05-23 14:46:28      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:clu   rtu   tor   turn   news   include   strategy   order   定义   

意图:定义一系列的算法,把他们一个个封装起来,并且使他们可以互相替换,使算法可以独立于使用它的客户变化而变化

暂无理解,以后再回来编辑,先贴代码

TaxStrategy.h

#pragma once
class TaxStrategy
{
    virtual double Calculate() = 0;
    virtual ~TaxStrategy() {};
};
CNTax.h

#pragma once
#include "TaxStrategy.h"
class CNTax :
    public TaxStrategy
{
public:
    virtual double Calculate();
};
DETax.h

#pragma once
#include "TaxStrategy.h"
class DETax :
    public TaxStrategy
{
    virtual double Calculate();
};
USTax.h

#pragma once
#include "TaxStrategy.h"
class USTax :
    public TaxStrategy
{
public:
    virtual double Calculate();
};
SalesOrder.h

#pragma once
#include"TaxStrategy.h"
class SalesOrder
{
    TaxStrategy * strategy;
public:
    SalesOrder(StrategeFactory * factory);//需要一个工厂类,省略
    ~SalesOrder();
    double Calculate();
};
TasStrategy.cpp

#include "TaxStrategy.h"
CNTax.cpp

#include "CNTax.h"

double CNTax::Calculate()
{
    return 111.111;
}
DETax.cpp

#include "DETax.h"

double DETax::Calculate()
{
    return 333.333;
}
USTax.cpp

#include "USTax.h"

double USTax::Calculate()
{
    return 222.222;
}
SalesOrder.cpp

#include "SalesOrder.h"

SalesOrder::SalesOrder(StrategeFactory * factory)
{
    this->strategy = StrategyFactory->newStrategy();
}

SalesOrder::~SalesOrder()
{
    delete this->strategy;
}

double SalesOrder::Calculate()
{
    double val = strategy->Calculate();
    return val;
}
main.cpp

#include <iostream>
#include"SalesOrder.h"
int main()
{
    std::cout << "Hello World!\n";
}

未完成.

 

设计模式:Strategy(策略)

标签:clu   rtu   tor   turn   news   include   strategy   order   定义   

原文地址:https://www.cnblogs.com/leocc325/p/12942363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!