Elaborate practical template method pattern and parsing examples

1 Introduction:

in the book Design Patterns. The template method is a method in a superclass, usually an abstract superclass, and defines the skeleton of an operation in terms of a number of high-level steps. These steps are themselves implemented by additional helper methods in the same class as the template method. (in "design patterns" book. template is a superclass (usually abstract superclass method) in which the definition of many advanced step operation according to the frame. these steps per se by a method of the template other auxiliary method implemented in the same class.)

- quoted from Wikipedia

Is defined herein in terms of a popular algorithm skeleton, skeleton specific method (typically with final modification to prevent modification subclass) subclass and general methods, a general method for expansion and to implement.

image

2. The advantages and disadvantages:

advantage:

① same package part, a variable extension portion;

② extracting common code, easy to maintain;

③ behavior controlled by the parent class, subclass achieved.

Disadvantages: each requires a different implementation to implement a subclass, resulting in an increase of the number of classes, making the system more bulky.

3. Related Case Study

Shopping Scene:

Life we ​​can not do without shopping, shopping We may or may buy in physical stores online, but can be roughly divided into the following steps: browse merchandise, plus cart, settlement commodity prices. Here we can put this step as a whole is an abstract class, then we have these three steps, which the browser what specific merchandise, which added to the purchase can be achieved in a subclass, we finally settled.

Xiao Ming and flowers go to the supermarket shopping, Xiao Ming wants to buy fruits, flowers to buy daily necessities. So they began to pick related products, and finally add the purchase of goods to the cashier settlement.

/**
 * 超市总体购物流程
 */
abstract class GoToSuperMarket {

    // 浏览商品
    abstract void BrowseGoods();

    // 加入购物车
    abstract void addToCart();

    // 商品结算
    abstract void settlement();

    public final void shop() {
        BrowseGoods();
        addToCart();
        settlement();
    }
}

/**
 * 小明去超市买水果
 */
class XiaoMing extends GoToSuperMarket {


    @Override
    void BrowseGoods() {
        System.out.println("小明去超市找苹果、橘子、香蕉。。。");
    }

    @Override
    void addToCart() {
        System.out.println("小明挑选了5斤苹果,加入购物车");
    }

    @Override
    void settlement() {
        System.out.println("小明选完了,开始去收银台结算");
    }
}

/**
 * 小花去超市买日用品
 */
class XiaoHua extends GoToSuperMarket {

    @Override
    void BrowseGoods() {
        System.out.println("小花去超市找毛巾、毯子、牙刷。。。");
    }

    @Override
    void addToCart() {
        System.out.println("小花挑选了毛巾和牙刷,加入购物车");
    }

    @Override
    void settlement() {
        System.out.println("小花选完了,开始去收银台结算");
    }
}

public class process {

    public static void main(String[] args) {
        GoToSuperMarket shopperOne = new XiaoMing();
        shopperOne.shop();
        System.out.println();
        GoToSuperMarket shopperTwo = new XiaoHua();
        shopperTwo.shop();
    }
}
Output:
小明去超市找苹果、橘子、香蕉。。。
小明挑选了5斤苹果,加入购物车
小明选完了,开始去收银台结算

小花去超市找毛巾、毯子、牙刷。。。
小花挑选了毛巾和牙刷,加入购物车
小花选完了,开始去收银台结算

4. Summary

In the daily development, we wish to reuse common code extracted, so that both appear to be concise and logical nature. At the same time we can expand to other methods that the class has more features.

Guess you like

Origin www.cnblogs.com/charlypage/p/11828195.html