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 apply J2EE-related Design patterns

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

Share

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

This article mainly explains "how to use J2EE-related design patterns". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use J2EE-related design patterns.

How can these patterns be applied specifically to Java Project? It was a real headache at first. On the one hand, we need to know the applicable situation of the model, the quality that the introduction of the pattern brings to the system, and so on. On the other hand, the understanding, digestion and absorption of the pattern itself. In addition, your boss forces you to finish the task -:). Wait. In fact, it is not impossible to analyze calmly. In the project, we finish our work on the one hand, but on the other hand, we have to think about our career, if we want to put Java Coder, Java Developer, Java Architect, or something. Do a good job, these are all basic skills.

Scholars just like to use such a simple question. Theoretical derivation, as technical workers, such as us, look at the results, the implementation of technology. In the process of learning design patterns, we often break away from practice and take a look at the UML diagram of design patterns. (to be honest, our UML skills are not very good, and it is estimated that there is little use of it in our UP. ), and what intentions, aliases, motivations, applicability, structure, participants, collaboration, effects, implementations, code examples, of the pattern. I lost my head. Have you forgotten that this kind of GP is already very abstract, adding so many rules and regulations, the word "depressed". We might as well start with analyzing the GP code and it works very well, as I did in the process of learning. Why don't we 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. Let's first look at the State pattern source code implementation of GOF SOFTWARE DESIGN PATTERNS CATALOGUE.

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. The first one, 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 point

Break

Case STATE_TWO:

CurrentState = new ConcreteState2 (); / / key point

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 status of ctx? Why has it changed? Think about it.

Ctx.changeState (Context.STATE_TWO)

/ / call Context.request () again, and the result is very different.

Ctx.request ()

}

}

Thank you for your reading, the above is the content of "how to use J2EE-related design patterns". After the study of this article, I believe you have a deeper understanding of how to use J2EE-related design patterns, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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