最初版本
cpp
复制代码
enum TaxBase
{
CN_Tax,
US_Tax,
DE_Tax
};
class SalesOrder
{
TaxBase tax;
public:
double CalcaulateTax()
{
if (tax == CN_Tax)
{
}
else if (tax == US_Tax)
{
}
else if (tax == DE_Tax)
{
}
}
};
最终版本
cpp
复制代码
class TaxStrategy
{
public:
virtual double Calculate(const Context& contxt) = 0;
virtual ~TaxStrategy(){}
};
class CNTax : public TaxStrategy
{
public:
virtual double Calculate(const Context& contxt)
{
}
};
class USTax : public TaxStrategy
{
public:
virtual double Calculate(const Context& contxt)
{
}
};
class DETax : public TaxStrategy
{
public:
virtual double Calculate(const Context& contxt)
{
}
};
class SaleOrder
{
private:
TaxStrategy* strategy;
public:
SaleOrder(StrategyFactory* strategyfactory)
{
strategy = strategyfactory->NewStrategy;
}
~SaleOrder()
{
delete strategy;
}
double CalculateTax()
{
Context context;
double val = strategy->Calculate();
}
};