Translation Tipatterns - Algorithmic Partitioning

xiaoxiao2021-03-06  83

Algorithmic Partitioning

Command mode (Command): Running

In the book "Advanced C : Programming Styles and IDioms" (Addison-Wesley, 1992), Jim Copline borrows the Functor this term, it is used to refer to objects that are constructed only for encapsulating a function (because "functor" is mathematics. There is a special meaning, so I will use a more clear term in this book: Function Object. This mode is important to separate the selection of the called function and the call to the called function.

"Design Mode" is just a function object this terminology without using it. However, the topic about the function object is repeated several times in the pattern chapter of the book.

In essence, Command is a function object: a method of encapsulating into an object. By encapsulating the method into an object, you can pass it as a parameter to other methods or objects, allowing them to complete some special operations when you implement a request. You may say that Command is actually a Messenger that converts data members to behavior (because it is directly directly).

//: command:.. CommandPattern.javapackage command; import java.util *; import junit.framework *; interface Command {void execute ();} class Hello implements Command {public void execute () {System.out.print ( "Hello");}} class world import ()} ("world!");}}}}}}}}}}} Class}}} class}}} Class Iam IMplements Command {PUBLIC VOID EXECUTE () {system.out.print (" I'm the command pattern! ");}} // an Object That Holds Commands: class macro {private list commands = new arraylist (); public void add (commist c) {Commands.Add (c);} public void Run () {iterator it = commands.iterator (); while (it.hasnext ()) ((Command) IT.Next ()). Execute ();}} PUBLIC CLASS CommandPattern Extends Testcase {macro macro = new macro ); Public void test () {macro.add (new hello ()); macro.add (new world ()); macro.add (new IAM ()); macro.Run ();} public static void main String args []) {junit.textui.teestrunner.run (commandpattern.class);}} ///: ~

The most important point in Command mode is that you can hand over the action (Action) you want to complete. A method or object. The above example provides a method to centrally process a series of action queues. In this case, you can create new behaviors, and usually you can only do this by writing new code; in the above example, you can complete the script (if you need (dynamically changed) The behavior is very complicated, then see Interpreter mode). Another example about the Command mode is Refactor: Dirlist.java [????]. The Dirfilter class is a Command object that is included in the accept () method, and these actions need to be passed to the list () method. The List () method determines which things need to be included in the results by calling accept (). "Design mode" One book "Commands is actually the object-oriented substitute for the callback function (", "But I think," back "is critical to the concept of callback (Callback). . That is, the callback function will actually go back to its creator. On the other hand, usually you just create a Command object and pass it to a method or object, then there is no longer link between the creator and the Command object. In short, this is my personal opinion. The chapter behind this book, I will put a set of related design patterns together, the title is "callback". The Strategy mode looks like a series of Command classes that are inherited from the same basis. But take a closer look at the Command mode, you will find it also have the same structure: a series of hierarchical function objects. The difference is that the usage of these function objects is different from the STRATEGY mode. Just like the previous refactor: Dirlist.java, using Command is to solve a specific problem - select a file from a list. The "constant portion" is the method called, and the changed portion is separated into the function object. I want to take the liberty: Command mode provides flexibility in the coding phase, and the flexibility of Strategy mode is reflected in runtime. Despite this, this difference is very blurred.

Exercise: 4. Use the Command mode to redo the practice 1 of the third chapter.

CHAIN ​​of Responsibility

The duting chain mode can be imagined to become a recrential dynamic generalization, which is done by using the Strategy object. When called, each Strategy object in the list is attempted to meet this call. This process ends when a strategy is called successfully or the entire Strategy chain reaches the end. When recursive, a method is constantly calling itself until a certain end condition; for the duties, a method calls itself, and then calls its own different implementations until a certain end condition . The so-called end condition is either to reach the end of the linked list, or a STRATEGY call is successful. For the first case, you have to return a default object; if you do not provide a default result, you must inform the caller's linked list to success in some way.

Since there may be more than one method in the Strategy Link to meet the requirements, the responsibility chain seems to have a little expert system. Because this series of Strategy is actually a linked list, it can be dynamically created, so you can also imagine the duties chain into a more generalized, dynamically built Switch statement. In the GOF book, there are many discussions on how to build a duties chain into a list. However, take a closer look at this mode you will find how to manage the chain list is not important; it is just a detail of implementation. Because GOF books are written before the Standard Template (STL) is written by most C compilers, the reason why they want to discuss the list is (1) There is no ready-made chain class, so they have to write one (2 ) The academic community often teaches the data structure as a basic skill, and may not realize that the data structure should be a standard tool for the programming language at that time. My claim is that it is not beneficial to implement the duties chain as a chain list without any benefits, which can be implemented by using standard Java List, the following example will illustrate this. Further, you will see that I have tested the part of the list management from different Strategy's implementations, so that this part of the code is easier to reuse. In the previous chapter, the scenario is very likely that you need to automatically find a solution solution. The duties chain have achieved this effect through another method, which puts a series of Strategy objects into the list and provides a mechanism to automatically traverse each node of the list.

//: chainofresponsibility: FindMinima.javapackage chainofresponsibility; import com.bruceeckel.util *; // Arrays2.toString () import junit.framework *; // Carries the result data and // whether the strategy was successful:.. Class LineData {public double [] data; public LineData (double [] data) {this.data = data;} private boolean succeeded; public boolean isSuccessful () {return succeeded;} public void setSuccessful (boolean b) {succeeded = b;} } interface Strategy {LineData strategy (LineData m);} class LeastSquares implements Strategy {public LineData strategy (LineData m) {System.out.println ( "Trying LeastSquares algorithm"); LineData ld = (LineData) m; // [Actual test / calculation here] LineData r = new LineData (new double [] {1.1, 2.2}); // Dummy data r.setSuccessful (false); return r;}} class NewtonsMethod implements Strategy {public LineData strategy (LineData m) {System.out.Println ("Trying NewtonsMethod Algorithm); LINEDATA LD = (LINEDATA) M; // [Actual test / calculation here] LineData r = new LineData (new double [] {3.3, 4.4}); // Dummy data r.setSuccessful (false); return r;}} class Bisection implements Strategy {public LineData strategy (Linedata M) {System.out.Println ("Trying Bisection Algorithm); Linedata LD = (Linedata) m; // [Actual Test / Calculation Here] Linedata R = New Linedata (New Double [] {5.5, 6.6} ); // Dummy data r.setSuccessful (true); return r;}} class ConjugateGradient implements Strategy {public LineData strategy (LineData m) {System.out.println ( "Trying ConjugateGradient algorithm"); LineData ld = (LineData) M.

// [Actual Test / Calculation Here] Linedata R = New Linedata (New Double [] {5.5, 6.6}); // Dummy Data R.setsuCcessful (True); Return R;}} Class MinimaFinder {Private Strategy [] Solutions = {New Leastsquares (), New NewTonsMethod (), New New Trick (), New ConjugateGradient (),}; Public Static Linedata Solve (Linedata Line) {LINEDATA R = line; for (int i = 0; i

table of Contents

转载请注明原文地址:https://www.9cbs.com/read-121882.html

New Post(0)