Design Pattern Introduction - Catch The Core
WANG Hailong
Data Type and Algorithm Data Types and Algorithms
The target of Design Pattern is to separate the constant sections and changes in the common problem. The constant portion constitutes the Design Pattern (design mode). This is some like this and the framework.
Let's look at the following questions.
1. There is an int array, int [] Intarray; find the largest number in the array.
2. There is a FLOAT array, float [] floatarray, find the largest number in the array.
Corresponding question 1, have a function, int max (int [] IntArray).
Corresponding question 2, having functions, float max (float [] floatarray).
The portion where the change here is the data type (INT AND float), the constant portion is the algorithm max.
In C , you can solve this problem with the Template mechanism.
Template
C STL is to solve these problems.
1. STL provides a set of containers (set) classes such as List, Vector, Set, etc. Container (set) class can place any type of data.
2. STL provides a set of Algorithm algorithm functions to operate the container. The MAX function mentioned above is naturally included.
With regard to design patterns, there is also a saying that Design Pattern is design by Contract. Design mode is to provide a good set of operational interface protocols.
The Algorithm Algorithm function of STL If you want to operate the container, these containers must provide a universal interface. The container must provide basic traversal operation. This abstracts a Design Pattern - Iterator.
In exchange, the algorithm and containers must meet this protocol: the container must provide the Iterator interface. Compared to the MAX algorithm, the data type inside the container must be a comparison, that is, supported <, =,> and other operators. If we have a SUM summation algorithm, then relative to SUM, the data type inside the container must be added, that is, support operators.
About Java examples, Java.util.Collections also provided similar algorithms. Max, Sort, BinarySearch, etc.
STL's containers and algorithms are comprehensively, powerful than Java.
Dimensions of the problem
We often write some common processing parts to a public function, and each part of the program can call this function. The logic and algorithm of functions are constant, and only the parameters are changed. This is called reuse.
The design model is also to achieve the purpose of reuse - reach the design structure. However, because the problem corresponding to the design mode is more complicated, the change in the problem cannot be expressed simply by parameters, and some object-oriented characteristics must be used.
Analyze a similar problem, from the first step of the design mode, it is to identify changes in the changed portion and the constant portion; the second step is the structure of the design constant part, define the interface protocol between the objects, The smaller the changed portion, the easier change, the better.
Determine a factor in design mode complexity, mainly the dimension of the problem. Here, "Dimensions" means that the dimension of the variation section is mainly: What are the differences in these changes? For example, the above data type and algorithm example, we can see that different parts have different data types, and the container type is different; the same part there is, the logic and processes of the algorithm are the same. In this way, we abstract a 2D (layer) design pattern. The first layer, the data type, and the different data type are different, the data operation provided by the container is the same - traversal, increase, delete, lookup, etc.; the separation of the second layer, the container and algorithm, the type of container, but algorithm Logic and processes are the same, such as the MAX algorithm: (1) Each of the data inside the container, (2) compares the data, records the maximum data, and (3) returns the maximum data.
The 23 basic design patterns raised by Gang ', most of the corresponding problem space is 1 dimension. Some slightly complex design patterns are 2D, such as Double Dispatch (Visitor) and Abstract Factory.
Let's look at Abstract Factory. Take the AWT Form Control in Java as an example.
(This example appears in a lot of books that describe design patterns, there are some good, gave me a lot of inspiration.)
(I try to avoid writing repeated things. Most books and materials are repeatedly written everyone knowing things, and they are not to know what everyone doesn't know. Books and information can also achieve good reuse Ok. After a painful lesson, I finally realized that the gap between the publisher is so big. I strongly recommend that only the first-class publishing house, such as Tsinghua Publishing House, is worthy, almost every book is Boutique. Some publishers, thinking, thinking. I will not mention here. I regret it. Question out :-)
Because Java is a cross-platform, the AWT form control also has to correspond to the current operating system. The dimensions of the problems here are divided into two layers: (1) different operating systems, such as Unix, Windows, (2) different types, such as Button, Dialog, Scrollbar, etc.
We define a Factory factory base class (or interface) Toolkit, CreateButton, Createdialog, etc. See the Java.awt.Toolkit class, which is the Factory factory base class.
We redefine a UnixToolkit, inherit (or implementation) Toolkit, where the CreateButton function returns Unix Button, CreateDialog Returns Unix Dialog.
We redefine a WindowsToolkit, inherit (or implementation) Toolkit, where the CreateButton function returns Windows Button, CreateDialog Returns Windows Dialog.
The above is an example of Abstract Factory. Note that the above UnixToolkit and WindowsToolkit portions exist inside the operating system related Native Code, which is not found in the JDK document and Java Source Code.
Then, how do you work after the form control is created? After all, these form controls are related to the operating system. Similarly, the form control needs to call some operating systems related Native Code. In Java, this is called Peer. For example, the Button class needs to call the ButtonPeer class to implement the control of the control, and the Dialog class needs to call the Dialogpeer class to implement the operation of the control. This structure is called Bridge mode. It can be seen that the problem space corresponding to the Bridge mode is only 1 dimension - the corresponding operating system is different.
(Because the java.awt.peer package is the operating system is related, do not appear in Java DOC, but in the JDK source code, you can see this package, such as the button.java file of the java.awt package, you use Java. AWT.PEER.BUTTONPEER.)
Through the above analysis, we can see that the above UnixToolkit and WindowsToolkit, the operation of the created form control is the same, just the peer corresponding to the control, resulting in the specific operation. We can see that due to the application of Bridge mode, now, there is only the peer part of the problem.
According to this idea, we can "translate" translate the AWT form control "in another operational platform as long as we provide additional set of Peers for the AWT Form Control. For example, we provide a group of HTML peer corresponding to the AWT form control. Translate AWT Form Controls as an HTML control. In this way, generate an AWT button, which is equivalent to generating an HTML button on the web page, which is equivalent to the Event triggered by HTML Button.
See, http: //sourceforge.net's Echo Development Framework is an outstanding implementation. Using the ECHO development framework, you can write a web program with a method of writing AWT, SWING.