-
-
-
-
-
-
- public class Program {
-
-
-
-
-
-
-
- public static void main(String[] args) throws IOException {
- System.out.println( "单价:" );
- String strA = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- System.out.println( "数量:" );
- String strB = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- double numberd = Double.valueOf(strA) + Double.valueOf(strB);
- System.out.println( "价格为:" + numberd);
-
- }
-
- }
/**
* <h1>策略模式</h1>
* QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
* @author xangqun
*
*/
public class Program {
/**
*PS:
* <pre>如果商场搞活动,商品打8折,打5折怎么办??</pre>
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("单价:");
String strA = new BufferedReader(new InputStreamReader(System.in))
.readLine();
System.out.println("数量:");
String strB = new BufferedReader(new InputStreamReader(System.in))
.readLine();
double numberd = Double.valueOf(strA) + Double.valueOf(strB);
System.out.println("价格为:" + numberd);
}
}
-
-
-
-
-
-
- public class ProgramTwo {
-
-
-
-
-
-
-
- public static void main(String[] args) throws IOException {
-
- System.out.println( "单价:" );
- String strA = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- System.out.println( "数量:" );
- String strB = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- System.out.println( "折扣:" );
- String strC = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- int numc= Integer.valueOf(strC);
- double result = 0 ;
- switch (numc){
- case 1 :result=Double.valueOf(strA) + Double.valueOf(strB); break ;
- case 2 :result=(Double.valueOf(strA) + Double.valueOf(strB))* 0.8 ; break ;
- case 3 :result=(Double.valueOf(strA) + Double.valueOf(strB))* 0.5 ; break ;
- }
- System.out.println(result);
- }
/**
* <h1>策略模式</h1>
* QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
* @author xangqun
* 改进的程序1
*/
public class ProgramTwo {
/**
* PS:3个分支执行的语句除了打折不一样外几乎没什么不同应该考虑重构下
* @param args
* @throws IOException
*
*/
public static void main(String[] args) throws IOException {
System.out.println("单价:");
String strA = new BufferedReader(new InputStreamReader(System.in))
.readLine();
System.out.println("数量:");
String strB = new BufferedReader(new InputStreamReader(System.in))
.readLine();
System.out.println("折扣:");
String strC = new BufferedReader(new InputStreamReader(System.in))
.readLine();
int numc= Integer.valueOf(strC);
double result = 0;
switch(numc){
case 1:result=Double.valueOf(strA) + Double.valueOf(strB);break;
case 2:result=(Double.valueOf(strA) + Double.valueOf(strB))*0.8;break;
case 3:result=(Double.valueOf(strA) + Double.valueOf(strB))*0.5;break;
}
System.out.println(result);
}
- public abstract class CashSuper {
-
- public abstract double acceptCash( double money);
- }
public abstract class CashSuper {
public abstract double acceptCash(double money);
}
- public class CashNormal extends CashSuper {
-
- @Override
- public double acceptCash( double money) {
-
- return money;
- }
-
- }
public class CashNormal extends CashSuper {
@Override
public double acceptCash(double money) {
return money;
}
}
- public class CashRebate extends CashSuper {
-
- private double moneyRebate=1d;
- public CashRebate(){}
- public CashRebate(String moneyRebate){
- this .moneyRebate=Double.valueOf(moneyRebate);
- }
- @Override
- public double acceptCash( double money) {
- return money*moneyRebate;
- }
-
- }
public class CashRebate extends CashSuper {
private double moneyRebate=1d;
public CashRebate(){}
public CashRebate(String moneyRebate){
this.moneyRebate=Double.valueOf(moneyRebate);
}
@Override
public double acceptCash(double money) {
return money*moneyRebate;
}
}
- public class CashReturn extends CashSuper {
-
- private double moneyCondition= 0 .0d;
- private double moneyReturn= 0 .0d;
- public CashReturn(){}
- public CashReturn( double moneyCondition, double moneyReturn){
- this .moneyCondition=moneyCondition;
- this .moneyReturn=moneyReturn;
- }
- @Override
- public double acceptCash( double money) {
- double result=money;
- if (money>moneyCondition){
- result=money-Math.floor(money/moneyCondition)*moneyReturn;
- }
- return result;
- }
-
- }
public class CashReturn extends CashSuper {
private double moneyCondition=0.0d;
private double moneyReturn=0.0d;
public CashReturn(){}
public CashReturn(double moneyCondition,double moneyReturn){
this.moneyCondition=moneyCondition;
this.moneyReturn=moneyReturn;
}
@Override
public double acceptCash(double money) {
double result=money;
if(money>moneyCondition){
result=money-Math.floor(money/moneyCondition)*moneyReturn;
}
return result;
}
}
- public class CashFactory {
-
- public static CashSuper createCashAccept( int type){
- CashSuper cs= null ;
- switch (type){
- case 1 :cs= new CashNormal(); break ;
- case 2 : cs= new CashReturn( 300 , 100 ); break ;
- case 3 :cs= new CashRebate( "0.8" ); break ;
- }
- return cs;
- }
- }
public class CashFactory {
public static CashSuper createCashAccept(int type){
CashSuper cs=null;
switch(type){
case 1:cs=new CashNormal();break;
case 2: cs=new CashReturn(300,100);break;
case 3:cs=new CashRebate("0.8");break;
}
return cs;
}
}
-
-
-
-
-
-
- public class ProgramThree {
-
-
-
-
-
-
- public static void main(String[] args) throws IOException {
- System.out.println( "单价:" );
- String strA = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- System.out.println( "数量:" );
- String strB = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
-
- CashSuper cs=CashFactory.createCashAccept( 1 );
- double totalprices=0d;
- totalprices=cs.acceptCash(Double.valueOf(strA)*Double.valueOf(strB));
- System.out.println(totalprices);
- }
-
- }
/**
* <h1>策略模式</h1>
* QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
* @author xangqun
* 改进的程序2(采用简单工厂模式)
*/
public class ProgramThree {
/**PS:
* 简单工厂模式只是解决对象的创建问题,面对算法时常变动应该考虑用其他方法
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("单价:");
String strA = new BufferedReader(new InputStreamReader(System.in))
.readLine();
System.out.println("数量:");
String strB = new BufferedReader(new InputStreamReader(System.in))
.readLine();
CashSuper cs=CashFactory.createCashAccept(1);
double totalprices=0d;
totalprices=cs.acceptCash(Double.valueOf(strA)*Double.valueOf(strB));
System.out.println(totalprices);
}
}
- public class CashContext {
-
- private CashSuper cs;
- public CashContext(CashSuper csuper){
- this .cs=csuper;
- }
- public double getResult( double money){
- return cs.acceptCash(money);
- }
- }
public class CashContext {
private CashSuper cs;
public CashContext(CashSuper csuper){
this.cs=csuper;
}
public double getResult(double money){
return cs.acceptCash(money);
}
}
-
-
-
-
-
-
- public class ProgramFour {
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void main(String[] args) throws IOException {
- System.out.println( "单价:" );
- String strA = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- System.out.println( "数量:" );
- String strB = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- System.out.println( "类型:" );
- String strC = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- CashContext cc= null ;
- switch (Integer.valueOf(strC)){
- case 1 :cc= new CashContext( new CashNormal()); break ;
- case 2 : cc= new CashContext( new CashReturn( 300 , 100 )); break ;
- case 3 :cc= new CashContext( new CashRebate( "0.8" )); break ;
- }
- double totalprices=0d;
- totalprices=cc.getResult(Double.valueOf(strA)*Double.valueOf(strB));
- System.out.println(totalprices);
-
- }
/**
* <h1>策略模式</h1>
* QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
* @author xangqun
* 改进的程序3(采用策略模式)
*/
public class ProgramFour {
/**<b>策略模式(strategy)</b>
*<br> 它定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化不会影响到使用算法的客户<br>
*策略模式是一种定义一系列算法的方法,从概念上来看,所以这些算法完成的都是相同的工作,只是实现不同,他可以
*以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。
*优点:1.context定义了一系列的可供重用的算法或行为,继承有助于析取出这些算法中公共功能(这里是getresult)
* 2.简化了单元测试因为每个算法都有自己的类,可以通过自己的接口单独测试
* 策略模式封装了变化
* 应用:策略模式就是用来封装算法的,但实践中,我们发现可以用他来封装几乎任何类型的规则,只要在分析过程中
* 听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("单价:");
String strA = new BufferedReader(new InputStreamReader(System.in))
.readLine();
System.out.println("数量:");
String strB = new BufferedReader(new InputStreamReader(System.in))
.readLine();
System.out.println("类型:");
String strC = new BufferedReader(new InputStreamReader(System.in))
.readLine();
CashContext cc=null;
switch(Integer.valueOf(strC)){
case 1:cc=new CashContext(new CashNormal());break;
case 2: cc=new CashContext(new CashReturn(300,100));break;
case 3:cc=new CashContext(new CashRebate("0.8"));break;
}
double totalprices=0d;
totalprices=cc.getResult(Double.valueOf(strA)*Double.valueOf(strB));
System.out.println(totalprices);
}
- public class CashContextFactory {
-
- private CashSuper cs;
- public CashContextFactory( int type){
- switch (type){
- case 1 : this .cs= new CashNormal(); break ;
- case 2 : this .cs= new CashReturn( 300 , 100 ); break ;
- case 3 : this .cs= new CashRebate( "0.8" ); break ;
- }
- }
- public double getResult( double money){
- return cs.acceptCash(money);
- }
- }
public class CashContextFactory {
private CashSuper cs;
public CashContextFactory(int type){
switch(type){
case 1:this.cs=new CashNormal();break;
case 2:this.cs=new CashReturn(300,100);break;
case 3:this.cs=new CashRebate("0.8");break;
}
}
public double getResult(double money){
return cs.acceptCash(money);
}
}
-
-
-
-
-
-
- public class ProgramFive {
-
-
-
-
-
- public static void main(String[] args) throws IOException {
- System.out.println( "单价:" );
- String strA = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- System.out.println( "数量:" );
- String strB = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- System.out.println( "类型:" );
- String strC = new BufferedReader( new InputStreamReader(System.in))
- .readLine();
- CashContextFactory cc= new CashContextFactory(Integer.valueOf(strC));
- double totalprices=cc.getResult(Double.valueOf(strA)*Double.valueOf(strB));
- System.out.println(totalprices);
- }
-
- }
自己C++实现的策略模式
#include <fstream> #include <iostream> #include <string> using namespace std; /*策略模式*/ /*超类,定义接口*/ class CashSuper { public: virtual double acceptCash(double money) = 0; }; /*正常情况下*/ class CashNormal:public CashSuper { public: double acceptCash(double money) { return money; } }; /*打折*/ class CashRebate:public CashSuper { public: CashRebate(double moneyRebate) { this->moneyRebate = moneyRebate; } double acceptCash(double money) { return moneyRebate * money; } private: double moneyRebate; }; /*满3000返回100*/ class CashReturn:public CashSuper { public: CashReturn(double moneyCondition, double moneyReturn) { this->moneyCondition = moneyCondition; this->moneyReturn = moneyReturn; } double acceptCash(double money) { if (money >= moneyCondition) { return money - moneyReturn; } else { return money; } } private: double moneyCondition; double moneyReturn; }; /*策略上下文类:type=1表示“正常”;type=2表示“打折”;type=3表示“满300返100”*/ class CashContext { public: CashContext(int type) { switch (type) { case 1: m_cashSuper = new CashNormal(); break; case 2: m_cashSuper = new CashRebate(0.8); break; case 3: m_cashSuper = new CashReturn(300, 100); break; } } double getResult(double money) { return m_cashSuper->acceptCash(money); } private: CashSuper* m_cashSuper; }; int main() { CashContext *cash1 = new CashContext(1); cout<<cash1->getResult(350)<<endl; CashContext *cash2 = new CashContext(2); cout<<cash2->getResult(350)<<endl; CashContext *cash3 = new CashContext(3); cout<<cash3->getResult(350)<<end