您的位置:首页 > 其它

(行为型模式七)策略模式

2015-04-27 14:29 190 查看
package com.eyugame.modle;
/**
 * 策略模式
 * @author JYC506
 *
 */
/*策略一*/
public class Strategy1 implements IStrategy{

	@Override
	public void show() {
		System.out.println("使用策略1");
	}
	
	public static void main(String[] args) {
		Context context;
		/*使用策略一*/
		context=new Context(new Strategy1());
		context.execute();
		/*使用策略二*/
		context=new Context(new Strategy2());
		context.execute();
	}

}
/*策略二*/
class Strategy2 implements IStrategy{

	@Override
	public void show() {
		System.out.println("使用策略2");
	}
	
}
interface IStrategy{
	void show();
}
/*调用者*/
class Context{
	IStrategy strategy;
	public Context(IStrategy strategy){
		this.strategy=strategy;
	}
	public void execute(){
		this.strategy.show();
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: