Java 设计模式 之 策略模式

http://www.verejava.com/?id=16999166513476

package com.strategy.theory;

public class TestStrategy
{
	public static void main(String[] args)
	{
		Context ctx=new Context(new StrategyImplA());
		ctx.doMethod();
		
		ctx=new Context(new StrategyImplB());
		ctx.doMethod();
		
		ctx=new Context(new StrategyImplC());
		ctx.doMethod();
	}
}





package com.strategy.theory;

public class Context
{
	private Strategy strategy;
	
	public Context(Strategy strategy)
	{
		this.strategy=strategy;
	}
	
	public void doMethod()
	{
		strategy.method();
	}
}





package com.strategy.theory;

public abstract class Strategy
{
	public abstract void method();
}





package com.strategy.theory;

public class StrategyImplA extends Strategy
{

	@Override
	public void method()
	{
		System.out.println("方法实现 A");
		
	}

}





package com.strategy.theory;

public class StrategyImplB extends Strategy
{

	@Override
	public void method()
	{
		System.out.println("方法实现 B");
		
	}

}






package com.strategy.theory;

public class StrategyImplC extends Strategy
{

	@Override
	public void method()
	{
		System.out.println("方法实现 C");
		
	}

}


http://www.verejava.com/?id=16999166513476

阅读更多
换一批

没有更多推荐了,返回首页