Java State Mode explanation demo sample code

Source: Internet
Author: User

Package Org.rui.pattern;import junit.framework.*;/** * In order for the same method invocation to produce different behavior, the state mode is in the proxy (surrogate) * Switch the corresponding implementation (implementation) in the life cycle. This is a way to optimize the implementation of code when you find that it is necessary to make very many tests before deciding how to implement any object decoupling (object decoupling) * Http://blog.csdn.net/lxwde 28. For example, the fairy tale Frog Prince consists of an object (a creature) whose behavior depends on its own state. You can indicate its state with a Boolean (Boolean) value, such as the following: *  * @author Administrator * */class creature{private Boolean isfrog = true; public void greet () {if (Isfrog) System.out.println ("ribbet!"); ElseSystem.out.println ("darling!");} public void Kiss () {Isfrog = false;}} public class Kissingprincess extends testcase{creature creature = new Creature ();p ublic 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.*;/** * State mode: Changing the behavior of an object an object that is used to change the class (state). Signs: Almost all methods appear (the same) conditional (expression) code. * * * However, the Greet () method (and all other parties that have to test the Isfrog value before completing the operation) finally produce a lot of hard-to-handle code. * Assuming that these operations are entrusted to a State object that can be changed, the code is much simpler. * * @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 ();p ublic void greet () {System.out.println (State.response ());} public void Kiss () {//change object state = new Prince ();}} public class KissingPrincess2 extends testcase{creature2 creature = new Creature2 ();p ublic 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.*;/** * The following code demonstrates the basic structure of state mode */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 ());} Implement public void Service1 () by method call () {//... 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 ()), @Testpublic Void Test () {run (SP); Sp.changestate (new Implementation2 ()); run (sp);} /* public static void Main (String args[]) {* Junit.textui.TestRunner.run (statedemo.class);} */}///:~/* * in Main () function , the first implementation is used, and then the second one is implemented. When you implement state mode yourself, you encounter a lot of detail, and you have to choose the appropriate implementation method based on your own needs *, such as whether you want to expose to the calling client and how to make the state change. In some cases (like Swing's * LayoutManager), the client can directly pass in the object, but in Kissingprincess2.java's case, the state is not visible to the client. In addition, the mechanism used to change the state may be very easy or very complex-for example, state machines, which will be mentioned later in this book, where there are a series of States and different mechanisms for changing the state. The example of the LayoutManager of Swing mentioned above is very interesting, and it embodies the behavior of the * strategy mode and state mode at the same time. The difference between Proxy mode and state mode is that they solve different problems. "Design Mode"It's so descriptive. * Proxy mode for general applications: 1. Remote Proxy provides a local proxy for an object in a different address space. The RMI compiler (rmic) creates a remote proxy for you when creating * stubs and skeletons. 2. Virtual agent (virtual proxy), as required, use * "lazy initialization" when creating complex objects. 3. The Protection agent (protection proxy) is used in cases where you do not want the client program ape to fully control the surrogate object (Proxied objects). 4. Smart references (smart Reference). Provides additional action when visiting the object being proxied. * For example, it can be used to count a reference to a particular object, enabling write-time Replication (copy-on-write), thus avoiding object aliases (objects aliasing). A simpler * example is used to record the number of times a particular method has been called. You can think of reference (reference) in Java as a protection agent that controls access to the actual objects allocated on the heap * (and ensures that you don't use a null reference (reference)). Rewrite: In the book design mode, the Proxy mode and the state mode are considered to be irrelevant, because the structure given by the book to implement the two patterns is completely different (I think this implementation is somewhat arbitrary). In particular, the state * mode, which uses a separate implementation hierarchy, but I feel absolutely unnecessary, unless you assume that the implementation code is not controlled by you (of course, it is a possibility, but assuming that the * code is controlled by you, it is still a separate base class is more concise and useful). In addition, the real * of proxy mode does not need to use a common base class, because the proxy object only Controls access to the Proxied object. Although there are differences in detail, both the proxy mode and the state * mode use a proxy (surrogate) to pass the method call to the implementation object. 』 */

Java State Mode explanation demo sample code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.