阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某大型购物中心欲开发一套收银软件,要求其能够支持购物中心在不同时期推出的各
种促销活动,如打折、返利(例如,满300返100)等等。现采用策略(Strategy)模式实现该要求,得到如图5-1所示的类图。
图5-1策略模式类图
【C++代码】
#include<iostream>
usingnamespacestd;
enumTYPE{NORMAL,CASH_DISCOUNT,CASH_RETURN};
classCashSuper{
public:
(1);
};
classCashNormal:publicCashSuper{//正常收费子类
public:
doubleacceptCash(doublemoney){
retummoney;}
};
classCashDiscount:publicCashSuper{
private:
doublemoneyDiscount;//折扣率
public:
CashDiscount(doublediscount){moneyDiscount=
discount;}
doubleacceptCash(doublemoney){retummoney*
moneyDiscount;}
};
classCashRetum:publicCashSuper{//满额返利
private:
doublemoneyCondition;//满额数额
doublemoneyReturn;//返利数额
public:
CashRetnm(double
motieyCondition,doublemoneyReturn){
this->moneyCondition=moneyCondition;
this->moneyReturn=moneyRetum;
}
doubleacceptCash(double
money){
doubleresult=
money;
if(money>=moneyCondition)
result=money-(int)(money/moneyCondition)*moneyRetum;
return
result;
}
};
classCashContext{
private:
CashSuper*cs;
public:
CashContext(inttype){
switch(type){
case
NORMAL://正常收费
(2)
;
break;
caseCASH_RETURN://满300返100
(3)
;
break;
caseCASH_DISCOUNT://打八折
(4)
;
break;
}
}
doubleGetResult(doublemoney){
(5);
}
};
//此处略去main()函数
(1)virtual double acceptCash(double money) = 0
(2)cs = new CashNormal()
(3)cs = new CashReturn(300,100)
(4)cs = new CashDiscount(0.8)
(5)return cs->acceptCash(money)
试题分析:
策模式的结构图如下: