zoukankan      html  css  js  c++  java
  • 详细讲解实用的模板方法模式和实例解析

    1.简介:

    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.( 在《设计模式》一书中。模板方法是超类(通常是抽象超类)中的方法,它根据许多高级步骤定义操作的框架。这些步骤本身由与模板方法相同的类中的其他辅助方法实现。 )

    ​ ——引用自维基百科

    ​ 这里通俗的来讲就是定义一个算法骨架,骨架中有特定方法(一般用final修饰,防止子类修改)和一般方法,一般方法用于给子类实现和扩展。

    image

    2.优点和缺点:

    优点:

    ① 封装不变部分,扩展可变部分;

    ② 提取公共代码,便于维护;

    ③ 行为由父类控制,子类实现。

    缺点:每一个不同的实现都需要一个子类来实现,导致类的个数增加,使得系统更加庞大。

    3.相关实例分析

    购物场景:

    ​ 生活中我们都离不开购物,购物我们可能在网上也可能在实体店中购买,但大体可以分为以下几个步骤:浏览商品、加购物车、结算商品价格。这里我们可以把这个总体的步骤当做是一个抽象类,然后我们有这三个步骤,其中具体浏览了什么商品,哪些加购了都可以在子类中实现,最后我们来结算。

    ​ 小明和小花去超市购物,小明想要去买水果,小花去买日用品。于是他们开始挑选相关的产品,最后将加购的商品到收银台结算。

    /**
     * 超市总体购物流程
     */
    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();
        }
    }
    
    
    输出结果:
    小明去超市找苹果、橘子、香蕉。。。
    小明挑选了5斤苹果,加入购物车
    小明选完了,开始去收银台结算
    
    小花去超市找毛巾、毯子、牙刷。。。
    小花挑选了毛巾和牙刷,加入购物车
    小花选完了,开始去收银台结算
    

    4.总结

    在日常开发中,我们不妨把公共重复利用的代码抽取出来,这样既能显得简洁而又具有逻辑性。同时我们又可以扩展其它方法,使得类具有更多的功能。

  • 相关阅读:
    Python3 使用requests库读取本地保存的cookie文件实现免登录访问
    Python3 使用requests库登陆知乎并保存cookie为本地文件
    python中的ConfigParser模块
    python中json的使用
    python中的IO模块
    python中的apscheduler模块
    ubuntu14静态ip配置
    在ubuntu14中搭建邮箱服务器
    python 生成器
    python中列表生成式
  • 原文地址:https://www.cnblogs.com/charlypage/p/11828195.html
Copyright © 2011-2022 走看看