Template method)

Source: Internet
Author: User

1. Policy mode 1.1 policy Mode Policy mode defines a series of algorithms and encapsulates each algorithm so that they can be replaced with each other. The rule mode allows algorithms to change independently of customers who use it. The rule mode is the behavior mode of the object. It is intended to encapsulate an algorithm into an independent class with a common interface for mutual replacement. The policy mode allows the algorithm to change without affecting the client. 1.2 composition of the Policy mode (1) Abstract policy role: Policy class, usually implemented by an interface or abstract class. (2) specific policy roles: encapsulate related algorithms and behaviors. The class diagram is as follows: 1.3 Use Case hypothesis: now we need to design a shopping cart system for e-commerce websites selling books. The simplest case is to multiply the unit price of all goods by the quantity, but the actual situation must be more complicated than that. For example, this website may offer a 20% discount to all senior members, a 10% discount to intermediate members, and no discount to junior members. According to the description, discounts are based on one of the following algorithms: algorithm 1: There is no discount for junior members. Algorithm 2: provides a 10% discount to intermediate members. Algorithm 3: provides a 20% discount to senior members. The implementation code is as follows: [java]/*** @ Title: commodity abstract discount class * @ Description: TODO * @ Company: Orclight's Studio * @ author: shuzl 03:24:53 * @ mover: Never put off what you can do today until Tomorrow * @ version 1.0.0 */public interface MemberStrategy {public double discountPrice (double bookPrice );} [java]/*** @ Title: junior member -- discount class * @ Description: TODO * @ Company: Orclight's Studio * @ author: shuzl: 59 * @ motil: Never put off what you can do today until Tomorrow * @ version 1.0.0 */public class JuniorMemberStrategy implements MemberStrategy {public double discountPrice (double bookPrice) {return bookPrice;} [java]/*** @ Title: intermediate member -- discount class * @ Description: TODO * @ Company: Orclight's Studio * @ author: shuzl 03:58:01 * @ motto: Never put off what you can do today until Tomorrow * @ ve Rsion 1.0.0 */public class InterMediateMemberStrategy implements MemberStrategy {@ Override public double discountPrice (double bookPrice) {return bookPrice * 0.9;} [java]/*** @ Title: senior Member-discount class * @ Description: TODO * @ Company: Orclight's Studio * @ author: shuzl 03:59:30 * @ mo: never put off what you can do today until Tomorrow * @ version 1.0.0 */public class AdvancedMemberStrategy Implements MemberStrategy {@ Override public double discountPrice (double bookPrice) {return bookPrice * 0.8;} [java]/*** @ Title: book price class * @ Description: TODO * @ Company: Orclight's Studio * @ author: shuzl 04:15:57 * @ motto: never put off what you can do today until Tomorrow * @ version 1.0.0 */public class BookPrice {private MemberStrategy strategy; private double price;/*** @ param Strategy */public BookPrice (MemberStrategy strategy, double price) {super (); this. strategy = strategy; this. price = price;}/***** getRealBookPrice (price after discount) * @ return double * @ createDate 04:30:26 * @ since 1.0.0 */public double getRealBookPrice () {return strategy. discountPrice (this. price);} public MemberStrategy getStrategy () {return strategy;} public void setStrategy (MemberStra Tegy strategy) {this. strategy = strategy;} [java]/*** @ Title: the price of books after discounts-test class * @ Description: TODO * @ Company: Orclight's Studio * @ author: shuzl 04:17:23 * @ mover: Never put off what you can do today until Tomorrow * @ version 1.0.0 */public class TestBookPrice {public static void main (String [] args) {MemberStrategy strategy = new AdvancedMemberStrategy (); BookPrice bookPrice = new BookPrice (strategy, 99.0); System. out. println ("book price:" + bookPrice. getRealBookPrice () ;}} 2 template method mode 2.1 template method mode defines the skeleton of an algorithm, and delays some implementation steps to the subclass. Move the unchanged behavior to the superclass and remove repeated code in the subclass to reflect its advantages. The class diagram is as follows: 2.2 Application Scenario (1) Implement the unchanged part of an algorithm at a time and leave the variable behavior to the subclass for implementation (2) common behaviors in each subclass should be extracted and concentrated 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 codes with a template method that calls these new operations. The implementation code is as follows: [java]/*** @ Title: Template Method abstract class -- defines a service skeleton, and delays some implementation steps to the subclass * @ Description: TODO * @ Company: Orclight's Studio * @ author: shuzl 02:10:38 * @ motto: never put off what you can do today until Tomorrow * @ version 1.0.0 */public abstract class implements acttemplate {/*** executeMain (required in the subclass) * @ createDate 02:12:43 * @ since 1.0.0 */public abstract void executeMain (); public void execute () {beforeAction (); executeMain (); afterAction ();} public void beforeAction () {System. out. println ("before Action... ");} public void afterAction () {System. out. println ("after Action... ") ;}} [java]/*** @ Title: Template Method subclass -- Implement the variable part * @ Description: TODO * @ Company: orclight's Studio * @ author: shuzl 02:13:09 * @ motto: never put off what you can do today until Tomorrow * @ version 1.0.0 */public class TemplateImpl extends acttemplate {public void executeMain () {System. out. println ("core operation processing... implementation Method 1 ");} [java]/*** @ Title: Template Method Test class * @ Description: TODO * @ Company: Orclight's Studio * @ author: shuzl 02:14:18 * @ mover: Never put off what you can do today until Tomorrow * @ version 1.0.0 */public class TestTemplate {public static void main (String [] args) {AbstractTemplate temp = new TemplateImpl (); temp.exe cute ();}}

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.