자바 상태 모드 해설 데모 샘플 코드

5366 단어 자바
package org.rui.pattern;

import junit.framework.*;

/**
 *                    ,State      (surrogate) 
 *               (implementation)。    ,             (Object decoupling)
 * http://blog.csdn.net/lxwde 28                     ,             。  ,
 *                (    ),                   。        (boolean)        ,        :
 * 
 * @author Administrator
 *
 */
class Creature
{
	private boolean isFrog = true;

	public void greet()
	{
		if (isFrog)
			System.out.println("Ribbet!");
		else
			System.out.println("Darling!");
	}

	public void kiss()
	{
		isFrog = false;
	}
}

public class KissingPrincess extends TestCase
{
	Creature creature = new Creature();

	public void test()
	{
		creature.greet();
		creature.kiss();
		creature.greet();
	}

	public static void main(String args[])
	{
		junit.textui.TestRunner.run(KissingPrincess.class);
	}
} // /:~
package org.rui.pattern;

import junit.framework.*;

/**
 *     :                (   )  。   :           (   )  (   )  。
 * 
 * 
 *   ,greet()   (                  isFrog      
 *  )               。
 *                        (State object),         。
 * 
 * @author Administrator
 *
 */
class Creature2
{

	private interface State
	{
		String response();
	}

	private class Frog implements State
	{
		public String response()
		{
			return "Ribbet!";
		}
	}

	private class Prince implements State
	{
		public String response()
		{
			return "Darling!";
		}
	}

	private State state = new Frog();

	public void greet()
	{
		System.out.println(state.response());
	}

	public void kiss()
	{
		//       
		state = new Prince();
	}
}

public class KissingPrincess2 extends TestCase
{
	Creature2 creature = new Creature2();

	public void test()
	{
		creature.greet();
		creature.kiss();
		creature.greet();
	}

	public static void main(String args[])
	{
		junit.textui.TestRunner.run(KissingPrincess2.class);
	}
} // /:~
package org.rui.pattern;

import org.junit.Test;

import junit.framework.*;

/**
 *          State        
 */

interface State
{
	void operation1();

	void operation2();

	void operation3();
}

class ServiceProvider
{
	private State state;

	public ServiceProvider(State state)
	{
		this.state = state;
	}

	public void changeState(State newState)
	{
		state = newState;
		System.out.println("========changeState==========="
				+ newState.getClass().getSimpleName());
	}

	//         
	public void service1()
	{
		// ...
		state.operation1();
		// ...
		state.operation3();
	}

	public void service2()
	{
		// ...
		state.operation1();
		// ...
		state.operation2();
	}

	public void service3()
	{
		// ...
		state.operation3();
		// ...
		state.operation2();
	}
}

class Implementation1 implements State
{
	public void operation1()
	{
		System.out.println("Implementation1.operation1()");

	}

	public void operation2()
	{
		System.out.println("Implementation1.operation2()");
	}

	public void operation3()
	{
		System.out.println("Implementation1.operation3()");
	}
}

class Implementation2 implements State
{
	public void operation1()
	{
		System.out.println("Implementation2.operation1()");
	}

	public void operation2()
	{
		System.out.println("Implementation2.operation2()");
	}

	public void operation3()
	{
		System.out.println("Implementation2.operation3()");
	}
}

public class StateDemo extends TestCase
{
	static void run(ServiceProvider sp)
	{
		sp.service1();
		sp.service2();
		sp.service3();
	}

	ServiceProvider sp = new ServiceProvider(new Implementation1());

	@Test
	public void test()
	{
		run(sp);
		sp.changeState(new Implementation2());
		run(sp);
	}

	/*
	 * public static void main(String args[]) {
	 * junit.textui.TestRunner.run(StateDemo.class); }
	 */
} // /:~

/*
 *   main( )   ,          ,         。        State                  ,         
 *           ,       (State)           ,            。     (   Swing  
 * LayoutManager),,client          ,    KissingPrincess2.java      ,    client    
 *    。  ,             easy       -               (State
 * Machine),                      。      Swing   LayoutManager         ,        
 * Strategy      State      。 Proxy     State                  。《    》         
 * Proxy         : 1.     (Remote Proxy)                   。 RMI    (rmic)   
 * stubs   skeletons                    。 2.    (Virtual proxy),    ,          
 * “      (lazy initialization)” . 3.     (protection proxy)       client         
 *    (proxied object)    。 4.     (smart reference).                 。
 *   ,                 ,         (copy-on-write),        (object aliasing).     
 *                      。      java     (reference)         ,        
 * (heap)         (                (null reference))。 『  : 《    》   ,Proxy    
 * State            ,                           (             )。    State
 *   ,              ,           ,                 (            ,    
 *          ,                )。  ,Proxy     
 *             ,                    。          ,Proxy     State
 *          (surrogate)             。』
 */

좋은 웹페이지 즐겨찾기