Decomposition Common (Factoring CommONAlicity)
Applying "once and only once" principles generate the most basic mode, put the part of the code that changes in the method. This can be expressed in two ways: Strategy: Runtime Select Algorithm In addition, Strategy mode can also add a "context", this context can be a proxy class (Surrogate Class), used to control Selection and use of a particular Strategy object.
//: Strategy: Strategypattern.java
Package statne;
Import com.bruceeckel.util. *; // arrays2.tostring ()
Import junit.framework. *;
// The Strategy Interface:
Interface findminima {
// Line is a sequence of point:
Double [] algorithm (double [] line);
}
// the various statulegies:
Class Leastsquares IMPLEMENTS FINDMINIMA {
Public Double [] Algorithm (double [] line) {
Return New Double [] {1.1, 2.2}; // Dummy
}
}
Class NewtonsMethod IMPLEments Findminima {
Public Double [] Algorithm (double [] line) {
Return New Double [] {3.3, 4.4}; // Dummy
}
}
Class Bisection Implements Findminima {
Public Double [] Algorithm (double [] line) {
Return New Double [] {5.5, 6.6}; // Dummy
}
}
Class conjugategradient imports findminima {
Public Double [] Algorithm (double [] line) {
Return New Double [] {3.3, 4.4}; // Dummy
}
}
// The "context" Controls the Strategy:
Class minimasolver {
PRIVATE FINDMINIMA STRATEGY;
Public minimasolver (Findminima Strat) {
Strategy = Strat;
}
Double [] minima (double [] line) {
Return Strategy.Algorithm (line);
}
Void ChangeAlgorithm (Findminima Newalgorithm) {
Strategy = newalgorithm;
}
}
Public class strategypattern extends testcase {
Minimasolver Solver =
New minimasolver (New Leastsquares ());
Double [] line = {
1.0, 2.0, 1.0, 2.0, -1.0,
3.0, 4.0, 5.0, 4.0};
Public void test () {
System.out.println (
Arrays2.toString (Solver.minima (Line)));
Solver.changeAlgorithm (New Bisection (); System.out.Println
Arrays2.toString (Solver.minima (Line)));
}
Public static void main (string args []) {
JUnit.textui.teStrunner.run (StrategyPattern.Class);
}
} ///: ~
Note that the template method (Template Method) mode and Strategy mode - the most significant feature ofTemplate Method is that it has multiple methods to be called, which is a segment implementation a feature. However, this is not to say that the Strategy object cannot have multiple method calls; consider the example of the order processing system given by ShaLloway, each Strategy contains different country information. (SHALLOWAY "," Design Patterns Explained ", author alalishoway
An example of a Strategy mode in JDK: Comparator Objects.
Policy Mode: General STRATEGY Mode Although Gof said that the Policy mode is just a alias of the Strategy mode, the example of the Strategy mode they gives is implicitly assumed that the Strategy object only uses a method - that is to assume that you already put it. The change algorithm is dismantled into a separate code. Others (referring to SHALLOWAY, DESIGN PATTERNS EXPLAINED, And AlexandRescu, Advanced C Design) uses Policy mode to indicate a number of methods for multiple methods, for different classes, which may be independent of each other. The Policy model has greater flexibility relative to (Strategy mode). For example, when a product needs to be shipped to different countries, Shipping Policy can be used to illustrate some issues related to transportation. These problems may include transportation, how to calculate postage or freight, customer needs, fees, special processing costs, and so on. All these things may not be different from each other, and it is important to require the above information in different locations (Points) during transportation. (Foreign trade professional vocabulary does not understand)
Typically, the Strategy mode and the Policy model are very good, and a method (change) is handled with the Strategy mode, and multiple methods are processed in the Policy mode.
Template method (Template Method)
The application framework allows you to inherit from one or a series of classes, and then create a new application, you can reuse most of the code of both class and to override some of the methods you need to overwrite it, thus Customization of the program. Template Method is a basic concept of the application framework, which is typically hidden behind (framework), drive applications by calling a group of methods (some methods you may have overwritten) by calling the base class.
For example, when you create an applet, you are already using the application framework: you inherited from the japplet and then reload the init () method. The Applet mechanism (actually Template Method) completes the rest of the work, such as screen display, processing event cycle, adjustment, and so on.
An important feature of Template Method is: it is defined in the base class and cannot be changed (derived). Sometimes it is private method, but in fact it is often declared as Final. It works by calling other base classes (overwritten), but it is often called as part of the initialization process, so it is not necessary to let client programmers can call it directly. //: TemplateMethod: TemplateMethod.java
// Simple Demonstration of Template Method.
Package templateMethod;
Import junit.framework. *;
Abstract class applimentFramework {
Public ApplicationFramework () {
TemplateMethod (); // dangerous!
}
Abstract void customer1 ();
Abstract void customer2 ();
Final void templateMethod () {
For (int i = 0; i <5; i ) {
Customize1 ();
Customize2 ();
}
}
}
// CREATE A New "Application":
Class myapp extends ApplicationFramework {
Void Customize1 () {
System.out.print ("Hello");
}
Void customer2 () {
System.out.println ("World!");
}
}
Public class templateMethod Extends testcase {
Myapp app = new myapp ();
Public void test () {
// The MyApp Constructor Does All The Work.
// this Just Makes Sure IT Will Complete
// WITHOUT THROWING AN Exception.
}
Public static void main (string args []) {
JUnit.textui.teStrunner.run (TemplateMethod.class);
}
} ///: ~
The constructor of the base class is responsible for completing the necessary initialization and launching the application "Template Method) (in a graphical user interface (GUI) program, this" engine "is usually" Main Event Loop ") . The client is only necessary to provide a definition of the Customize1 () and Customize2 () methods, the entire program can run.
Exercise 1. Write a framework, read from the command line into a series of file names. Turn all files that remove the last file for reading; open the last file for writing. Framework processes the input file with a policy-to-style, and writes the output to the last file. Inherit this framework, customize two different applications. A. Convert the text of each (input) file to uppercase letters. B. Use the word search (input) file given by the first file.
table of Contents