Design Pattern. This concept is now full of sky and we can estimate it in our hands: Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, in the book "Design Patterns-Element of Re-Useable Object-Oriented Software", this book. There are three modes, creation mode, structure mode, and behavior mode. There are 23 modes in total, which are not listed here. In practical application, we cannot digest and absorb it in a short period of time, because the pattern itself is a conventional thing and relies on previous project experience to summarize the usage habits. It takes time to cultivate and form these models. Therefore, we should not rush to use all models, but we must have this kind of consciousness. We should start to use it in projects and learning, what value does the research model bring to you. However, if you are interested in the design patterns used by the Java JDK1.4 source code, you can download them from the official website! For more information, see http://www.javaresearch.org/dn.jsp.
Before entering the topic, I declare that this article does not have any commercial purpose, which involves some source code of open source software, so if someone uses it for commercial purposes when it involves intellectual property issues, individuals are not responsible for this. It is only used for learning and communication purposes. Thank you for your cooperation.
Okay. Let's continue.
But the problem arises. How can we apply these patterns to Java projects? It was really a headache at the beginning. On the one hand, we need to know the application scenarios of the model, the quality of the system brought by the introduction model, and so on. On the other hand, the model itself is understood and absorbed. In addition, your boss forces you to complete the task -:). And so on. In fact, it is not impossible to analyze it calmly. In the project, we need to complete our work on the one hand, but on the other hand, we need to consider our own career, to put Java Coder, Java Developer, Java Architect, or something ...... All these are basic skills.
Scholars like to use 1 + 1 to solve such simple problems .... The theory is deduced. As a technical worker, for example, we are looking at the results and the practical nature of technology. While everyone is learning the design pattern, they often leave the practice to look at the UML diagram of the design pattern (to be honest, everyone's UML skills will not be too good, using it for our UP is less likely .) And the intention, alias, motivation, applicability, structure, participants, collaboration, effect, implementation, code instance ,....... My head is dizzy. Have you forgotten this type of GP? It is very abstract. With so many lines and frames added, the words are "depressing ". The analysis of the GP code is very effective. This is what I learned. Let's take a look at an example. Take the behavior mode State as an example. (There are many implementation examples of the mode on the network. There are many Java implementations. For example, this GP-based programming template is built in Together ControlCenter. You can use it for details .)
As you know, the purpose of State is to allow an object to change its behavior when its internal State changes. The object seems to have modified its class. Let's first look at the gof software design patterns catalogue provided on the http://www.javacoder.net/patterns.jsp State mode source code implementation.
First look at the Interface Class, State. java
Public interface State {
Public void handle ();
}
To define the interface to encapsulate it with Conext (the code will be presented later !) .
Then, we can see the implementation class of the interface. First, ConcreteState1.java
Public class ConcreteState1 implements State {
Public void handle (){
System. out. println ("ConcreteState1.handle () executing ");
}
}
Second, ConcreteState2.java
Public class ConcreteState2 implements State {
Public void handle (){
System. out. println ("ConcreteState2.handle () executing ");
}
}
These two classes implement the State interface.
Then let's look at how Context. java associates the above three. java files.
Public class Context {
Public static final int STATE_ONE = 0;
Public static final int STATE_TWO = 1;
// You should note that this sentence is very important. This mode is useful!
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;
}
}
}
In this way, after writing four. java files, you have actually implemented the State design mode, which is very interesting, right? This is simple.
Let's take a look at how to use this design pattern. Write a Client. java file.
Public class Client {
Public static void main (String [] args ){
// Construct Context
Context ctx = new Context ();
// Call Context. request ()
Ctx. request ();
// Change the ctx status? Why did it change? Let's take a look.
Ctx. changeState (Context. STATE_TWO );
// Call Context. request () Again, with different results.
Ctx. request ();
}
}
Do you also want to try it? If you want to run it on your own, you can see it.
After the analysis is complete, you will know how the State mode cleverly achieves "moving flowers and trees", or "changing the prince", and so on.
At this point, the analysis of this mode is not over yet. You 'd better go back to the textbook and read it again. If you still have problems reading this mode, I think I have failed to write it!
Complicated problems are simplified !!!!!