자바 상태 모드 해설 데모 샘플 코드
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) 。』
*/
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.