设计模式——策略模式

策略模式
为所有的算法定义一个抽象的算法接口,并通过继承该接口对所有的算法加以封装和实现,具体的算法选择交由客户端决定。(【1】把变化的代码从不变的代码中分离出来;【2】针对接口编程,而不是具体类;【3】将继承换成组合或聚合)
在这里插入图片描述
客户context有成员变量strategy(或者其他的策略接口),至于需要使用到哪一个策略,可以在构造器中指定。

package strategy;

public interface Flybehavior {
	void fly();//子类具体实现
}
package strategy;

public class GoodFly implements Flybehavior {

	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("飞行能力好");
	}

}
package strategy;

public class BadFly implements Flybehavior {

	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("飞行能力不好");
	}

}
package strategy;

public abstract class Duck {
	//注意抽象类里必须有抽象方法,也可以有非抽象方法
	//属性,策略接口
	Flybehavior flybehavior;
	public void fly() {
		if(flybehavior!=null)//属性不为空,说明已经把具体子类给了它
		flybehavior.fly();
	}
	public abstract void show();
	public Duck() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}
package strategy;

public class ToyDuck extends Duck {
	//构造器传入Flybehavior的对象
	public ToyDuck() {
		// TODO Auto-generated constructor stub
		flybehavior=new BadFly();
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		System.out.println("玩具鸭");
	}

}
package strategy;

public class WildDuck extends Duck {
	
	public void show() {
		System.out.println("野鸭");
	}
	//构造器传入Flybehavior的对象
	public WildDuck() {
		super();
		// TODO Auto-generated constructor stub
		flybehavior=new GoodFly();
	}

}
package strategy;

public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WildDuck a=new WildDuck();
		a.fly();
		ToyDuck b=new ToyDuck();
		b.fly();
	}

}

在这里插入图片描述

package zhifu;

public interface PaymentStrategy {
	void pay(PaymentContext ctx);//把上下文作为参数传递给策略对象
}

package zhifu;

public class PaymentContext {
	private String name=null;
	private double salary=0.0;
	private PaymentStrategy strategy=null;
	public PaymentContext(String name, double salary, PaymentStrategy strategy) {
		super();
		this.name = name;
		this.salary = salary;
		this.strategy = strategy;
	}
	public String getName() {
		return name;
	}
	public double getSalary() {
		return salary;
	}
	public void payNow(){
		this.strategy.pay(this);
	}

}


package zhifu;

public class RMBCash implements PaymentStrategy {

	public RMBCash() {
		// TODO Auto-generated constructor stub
		
	}

	@Override
	public void pay(PaymentContext ctx) {
		
		// TODO Auto-generated method stub
		System.out.println("人民币支付:"+ctx.getName()+" "+ctx.getSalary());
	}

}

package zhifu;

public class DollarCash implements PaymentStrategy {

	public DollarCash() {
		
	}

	@Override
	public void pay(PaymentContext ctx) {
		
		// TODO Auto-generated method stub
		System.out.println("美元支付:"+ctx.getName()+" "+ctx.getSalary());
	}

}

package zhifu;

public class Card implements PaymentStrategy {

	public Card() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void pay(PaymentContext ctx) {
		
		// TODO Auto-generated method stub
		System.out.println("信用卡支付:"+ctx.getName()+" "+ctx.getSalary());
	}
}
public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建相应的支付策略
		PaymentStrategy rmb=new RMBCash();
		PaymentStrategy dollar=new DollarCash();
		PaymentStrategy card=new Card();
		PaymentContext ctx1=new PaymentContext("li",30000,rmb);
		ctx1.payNow();
		PaymentContext ctx2=new PaymentContext("zhang",22000,dollar);
		ctx2.payNow();
		PaymentContext ctx3=new PaymentContext("wang",30800,card);
		ctx3.payNow();
	}

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值