设计模式 之 状态模式

下载 23种设计模式源码 :http://download.csdn.net/download/knight_black_bob/8936043

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


创建型模式,共五种:
工厂方法模式 抽象工厂模式 单例模式 建造者模式 原型模式

结构型模式,共七种:
适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式

行为型模式,共十一种:
策略模式 模板方法模式 观察者模式 迭代子模式 责任链模式 命令模式

备忘录模式 状态模式 访问者模式 中介者模式 解释器模式

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package 设计模式.状态模式;
/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年7月24日 下午3:35:34 
 * des:
 */
public interface State {
	 public void pre(Context c);
	 public void next(Context c);
	 public  String getState(); 
}
package 设计模式.状态模式;
/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年7月24日 下午3:35:13 
 * des:
 */
public class Context {

	private State current;

	public State getCurrent() {
		return current;
	} 
	public void setCurrent(State current) {
		this.current = current;
	}
 
	public void push(){
		current.next(this);
	}
	
	public void  poll(){
		current.pre(this);
	}
}
package 设计模式.状态模式;
/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年7月24日 下午3:34:24 
 * des:
 */
public class FirstStepState implements State{

	@Override
	public void pre(Context c) { 
		//System.out.println("没有上一步");
		c.setCurrent(null);
	}

	@Override
	public void next(Context c) { 
		//System.out.println("下一步");
		c.setCurrent(new SecondStepState());
	}

	@Override
	public String getState() {  
		return "第一步";
	}

	 

}
package 设计模式.状态模式;
/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年7月24日 下午3:34:24 
 * des:
 */
public class SecondStepState  implements State{

	@Override
	public void pre(Context c) { 
		//System.out.println("上一步");
		c.setCurrent(new FirstStepState());
	}

	@Override
	public void next(Context c) { 
		//System.out.println("下一步");
		c.setCurrent(new ThirdStepState());
	}

	@Override
	public String getState() {  
		return "第二步";
	}
	 

}
package 设计模式.状态模式;
/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年7月24日 下午3:34:24 
 * des:
 */
public class ThirdStepState  implements State{

	@Override
	public void pre(Context c) { 
		//System.out.println("上一步");
		c.setCurrent(new SecondStepState());
	}

	@Override
	public void next(Context c) { 
		//System.out.println("完成");
		c.setCurrent(null);
	}


	@Override
	public String getState() {  
		return "第三步";
	} 

}
package 设计模式.状态模式;
/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年7月24日 下午3:05:10 
 * des:
 */
public class StateTest {

	public static void main(String[] args) {
		Context c = new Context();
		State state = new FirstStepState();
		c.setCurrent(state);
		System.out.println( c.getCurrent().getState() +" " );
		
		c.push();
		System.out.println( c.getCurrent().getState() +" " );
		
		c.poll();
		System.out.println( c.getCurrent().getState() +" " );
		
		c.push();
		System.out.println( c.getCurrent().getState() +" " );
		
		c.push();
		System.out.println( c.getCurrent().getState() +" " );
		
		
	}
}



 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

猜你喜欢

转载自knight-black-bob.iteye.com/blog/2230002