首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

四-State: changing object behavior

2013-11-09 
4-State: changing object behavior//: state:KissingPrincess.javapackage stateimport junit.framework

4-State: changing object behavior

//: state:KissingPrincess.java

package state;

import junit.framework.*;

?

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 extendsTestCase? {

? 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);

? }

} ///:~

//: state:KissingPrincess2.java

package state;

import junit.framework.*;

?

class Creature {

? 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 = newPrince(); }

}

?

public class KissingPrincess2 extendsTestCase? {

? Creature creature = new Creature();

? public void test() {

??? creature.greet();

??? creature.kiss();

??? creature.greet();

? }

? public static void main(String args[]){

???junit.textui.TestRunner.run(KissingPrincess2.class);

? }

} ///:~

//: state:StateDemo.java

// Simple demonstration of the Statepattern.

package state;

import junit.framework.*;

?

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;

? }

? // Pass method calls to theimplementation:

? 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(newImplementation1());

? public void test() {

??? run(sp);

??? sp.changeState(newImplementation2());

??? run(sp);

? }

? public static void main(String args[]){

???junit.textui.TestRunner.run(StateDemo.class);

? }

} ///:~

?

Proxy 和 State 总结:

Object decoupling

When a surrogateobject is created, it is given an implementation to which to send all of themethod calls.

Structurally, thedifference between Proxy and State is simple: a Proxy hasonly one implementation, while State has more than one. The applicationof the patterns is considered (in Design Patterns) to be distinct: Proxyis used to control access to its implementation, while State allows youto change the implementation dynamically. However, if you expand your notion of“controlling access to implementation” then the two fit neatly together.

?

热点排行