<pre name= "code" class= "CPP" >package org.rui.pattern;import junit.framework.testcase;/** * Decomposition Common (Factoring Commonality) * Apply the "once and only once" principle to produce the most basic pattern, and place the changed part of the code in the method. This can be expressed in two ways: Policy mode (strategy *): Run-time Selection algorithm in addition, the strategy mode can also add a "context", which can be a proxy class (surrogate * Class), Used to control the selection and use of a particular strategy object. * * @author Administrator * */public class Strategypattern extends Testcase{minimasolver solver = new Minimasolver (New Le Astsquares ());d ouble[] line = {1.0, 2.0, 1.0, 2.0, -1.0, 3.0, 4.0, 5.0, 4.0};p ublic void Test () {SYSTEM.OUT.PRINTLN (solve R.minima (line)); Solver.changealgorithm (new bisection ()); System.out.println (Solver.minima (line));//System.out.println (arrays2.tostring (line));} public static void Main (String args[]) {junit.textui.TestRunner.run (Strategypattern.class);}} /:~/** * Note that the similarity of template method and strategy pattern is the most notable feature of--template methods * is that it has several methods that need to be called and that it is segmented to implement a function. However, this is not to say that strategy objects cannot have multiple method invocations; Consider an example of an order processing system given by Shalloway *, each strategy containing different country information. Example of strategy pattern in JDK: Comparator * objects. */
Package Org.rui.pattern;import java.util.*;/** * If Java has a template mechanism, the above (type-safe) iterator can easily return objects of a particular type. But without a template mechanism, you have to return * generic Objects, or manually add code for each object that needs to be traversed. Here I will use the previous method. * Another question that needs to be decided at design time is when to determine the type of object. One method is to iterate over the type of the first object (as the type of the iterator), but this method has a problem when the container class re-sorts the objects according to its own internal algorithm (such as the hash table), so that the two * times of the same iterator may have different results. It is safe to let the user specify the type of the iterator when constructing the iterator. The final question is how to build iterators. It is not possible to rewrite the existing Java * class library, which already contains enumerators and iterators. However, we can use the Decorator pattern to simply create an enumerator or an overlay of an iterator that produces a new object with the iteration behavior we want (in this case, the RuntimeException * exception thrown when the type is incorrect). And this new object has the same interface as the original enumerator or iterator, so it can be used on the same occasion (you might argue that this is actually a Proxy * mode, but it's more like Decorator mode for its purpose (intent)). * * @author Administrator * */public class Typediterator implements Iterator{private Iterator Imp;private class Type;publ IC Typediterator (Iterator it, Class type) {imp = It;this.type = type;} public Boolean Hasnext () {return Imp.hasnext ();} public void Remove () {Imp.remove ();} Public Object Next () {Object obj = Imp.next (), if (!type.isinstance (obj)) throw new ClassCastException ("Typediterator for Type "+ tYpe+ "encountered type:" + obj.getclass ()); return obj;}} // /:~
Package Org.rui.pattern;import junit.framework.*;/** * Application Framework allows you to inherit from one or a series of classes to create a new application, You can reuse most of the code from existing classes and overwrite some of them with your own needs to customize the application from *. Template method is a basic concept of the application framework, which is usually hidden behind the (framework) by invoking a set of methods of the base class (some methods you may have already overridden) to drive the application. An important feature of the Template Method is that it is defined in the base class and cannot be changed by (derived classes). Sometimes it is a private method, but in fact it is often declared final. It works by invoking other base class methods (covered), but it is often called as part of the initialization process, so there is no need for the client programmer to invoke it directly. * * @author Administrator * */abstract class Applicationframework{public applicationframework () {Templatemethod ();//Dangerous }abstract void Customize1 (), abstract void Customize2 (), Final void Templatemethod () {for (int i = 0; i < 5; i++) {Customiz E1 (); Customize2 ();}}} Create a new "application": Class MyApp extends Applicationframework{void customize1 () {System.out.print ("Hello");} void Customize2 () {System.out.println ("world!");}} public class Templatemethod extends Testcase{myapp app = new MyApp ();p ublic void Test () {//The MyApp constructor does all The work.//this just makes sure it wouldcomplete//without throwing an exception.} public static void Main (String args[]) {junit.textui.TestRunner.run (Templatemethod.class);}} // /:~
Java Policy Mode Template Method Example