Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze State pattern in J2EE Design pattern

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to analyze the State pattern in the J2EE design pattern. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The concept of design pattern is flying everywhere now, and everyone probably has Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, in the book "Design Patterns-Element of Re-Useable Object-Oriented Software". According to the statement, there are three kinds of models, namely, the creative model, the structural model and the behavioral model. There are a total of 23 models, which are not listed here. In practical application, it is impossible for us to digest and absorb it in a short time, because the pattern itself is an established thing, relying on the idioms summed up by everyone's previous project experience.

Let's look at an example now. Take the behavioral model State as an example. There are many examples of the implementation of the pattern on the Internet. There are also many implementations of Java. For example, this GP-based programming template is built into Together ControlCenter, and you will know the details. )

As you know, the intention of State is to allow an object to change its behavior when its internal state changes. The object seems to have modified his class.

First of all, let's look at the interface class, State.java

Public interface State {public void handle ();}

To define the interface to encapsulate with Conext (the code will be stated later!) A behavior related to a particular state of the

Then look at the implementation class of the interface. * ConcreteState1.java

Public class ConcreteState1 implements State {public void handle () {System.out.println ("ConcreteState1.handle () executing");}}

The second one, ConcreteState2.java

Public class ConcreteState2 implements State {public void handle () {System.out.println ("ConcreteState2.handle () executing");}}

These two classes implement the State interface.

Then we'll see how Context.java connects the above three .java files.

Public class Context {public static final int STATE_ONE = 0; public static final int STATE_TWO = 1

/ / attention, this sentence is very important, where the mode is tampered with!

Private State currentState = new ConcreteState1 (); public void request () {currentState.handle ();} public void changeState (int state) {switch (state) {case STATE_ONE: currentState = new ConcreteState1 (); / / key break; case STATE_TWO: currentState = new ConcreteState2 (); / / key break;}

So, after writing four .java files, you've actually implemented the State design pattern, which is interesting, right? It's that simple.

Let's see how to use the design pattern. Write a Client.java and have a look.

Public class Client {public static void main (String [] args) {/ / construct Context Context ctx = new Context (); / / call Context.request () ctx.request (); / / change the state of ctx? Why has it changed? Think about it. Ctx.changeState (Context.STATE_TWO); / / calls Context.request () again, and the result is very different. Ctx.request ();}} above is how to analyze the State pattern in the J2EE design pattern shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report