Template method mode for behavioral design patterns, policy (strategy) mode

Source: Internet
Author: User

1 Template Method Mode:

The template method pattern encapsulates the steps that we do not know to implement as abstract methods, providing specific methods for invoking them in the correct order (collectively referred to as template methods) to form an abstract base class. Subclasses inherit this abstract base class to implement the abstract method of each step, while the workflow is controlled by the parent class. 2 The template method applies to the following situations:
1) One-time implementation of an invariant part of the algorithm, and the variable behavior is left to subclass to achieve.
2) The public behavior of each subclass should be extracted and centralized into a common parent class to avoid code duplication. First identify the differences in the existing code and separate the differences into new operations. Finally, replace these different code with a template method that invokes these new operations. 3 Structure diagram: Abstract class: Defines the primitive operations of the abstract, the specific subclasses will redefine them to implement an algorithm, implement a template method, define an algorithm skeleton. Specific class: Implements primitive operations to complete the steps associated with a particular subclass in the algorithm. 4 also for example: abstract class:
1  Public Abstract  classCar {2     3      Public Abstract voidMakehead ();4     5      Public Abstract voidmakebody ();6     7     8      Public voidMakecar () {9        This. Makehead ();Ten        This. Makebody (); One     } A      -      -  the}

Specific classes:

1  Public classBusextendsCar {2 3 @Override4      Public voidMakehead () {5     6SYSTEM.OUT.PRINTLN ("Generating bus front");7                 8     }9 Ten @Override One      Public voidMakebody () { A          -SYSTEM.OUT.PRINTLN ("Generating bus body"); -          the     } -  -}

Test:

1  Public classTest {2     3      Public Static voidMain (string[] args) {4             5Car car =NewJeep ();6 Car.makecar ();7         8         9     }Ten  One}
This is a very simple pattern, but it is very widely used.  The simple reason is that only the inheritance relationship is used in this pattern. II: Strategy (Strategy) Mode 1 policy (strategy) mode: There is often a problem in the business there is a set of algorithms, in different cases we may use different algorithms. We need to find a flexible and easy way to design: encapsulate each algorithm in a separate class with a common interface, so that they can be replaced with each other. 2 structure diagram: 3 For example: a common shopping problem: To quote to the customer, for the sales department, this is a very complex problem, for different customers to quote different prices, such as:-to ordinary customers or new customers is the full price; – The price of the old customer quoted, according to the customer's age, to give a certain discount; The price of the big customers, according to the accumulated consumption of large customers to give a certain discount; – also consider the amount and amount of customer purchase, such as: Although it is a new user, but the amount of a purchase is very large, or the total amount is very high, there will be a certain discount;-also, the position of the quoted person It also determines whether he has the authority to make a certain floating discount on the price; in short, quoting a customer is a very complex Java code:
1  PackageOoad.design.Strategy;2 /**3 * Quoting interface4  * @authorHellokitty Yan5  *6  */7  Public InterfaceQuoteprice {8       Public DoubleQuoteprice (DoublePrice );9}

1  PackageOoad.design.Strategy;2 3  Public classContent {4     PrivateQuoteprice Quoteprice;5 6      PublicContent (Quoteprice quoteprice) {7         Super();8          This. Quoteprice =Quoteprice;9     }Ten     /** One * Discount A      * @param Price -      * @return -      */ the      Public DoubleDiscountingDoublePrice ) { -         returnQuoteprice.quoteprice (price); -          -     } +  -}

1  PackageOoad.design.Strategy;2 /**3 * New User4  * @authorHellokitty Yan5  *6  */7  Public classNewUserImplementsquoteprice{8 9 @OverrideTen      Public DoubleQuoteprice (DoublePrice ) { One         if(price>10000){ A             returnprice*0.95; -         } -          the         returnPrice ; -     } -  -}

Test class:

1  PackageOoad.design.Strategy;2 3  Public classTest {4 5      Public Static voidMain (string[] args) {6Quoteprice q=NewNewUser ();7Content c=NewContent (q);8        DoubleNe= Q.quoteprice (20000);9 System.out.println (NE);Ten     } One  A}

Not all of the events in the above example have been written out!

Template method mode for behavioral design patterns, policy (strategy) mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.