zoukankan      html  css  js  c++  java
  • 设计模式(第十三式:模版方法模式)

    概念:
      模版方法模式:Define the skeleton of an algoritm in an operation, deferring some step to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.定义一个操作中的算法框架,而将一些步骤延迟到子类中。使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤。

    实现:
      规定子系统

    public class NovelBook {
        public void read(){
            System.out.println("小说可以阅读");
        }
    }
    public class ToolBook {
        public void use(){
            System.out.println("阅读小说需要查工具书");
        }
    }


      对外通信实现

    public class Book {
    
        NovelBook novelBook = new NovelBook();
        ToolBook toolBook = new ToolBook();
    
        public void read(){
            novelBook.read();
            toolBook.use();
        }
    }


    分析:
      1.这么简单的,我觉得没有必要在写测试和结果了。大概说一下,就是本来如果用到工具书和小说,我们需要分别创建对象,但现在,只需要创建Book对象就可以了。相当于封装了流程在其中。
      2.这个设计模式的好处就式,外部使用时不需要清楚知道内部之间时是怎样的逻辑关系,对外的封装,会负责逻辑顺序。简化了外部使用。
      3.这样做的好处是,减少了类与类之间更多的依赖关系,也更易于逻辑上的维护。但不利于修改内部逻辑顺序,也就是如果逻辑顺序发生变化,只能重写新的逻辑,不能灵活调整逻辑顺序。当然也可以不封装逻辑,只做聚合。
      4.举个例子理解一下,去快餐店买快餐,店铺通常只提供一个收银口和取餐口,你不知道人家里面是怎么处理的。反正就是交了钱等饭就好了,这就是外观模式。
      5.适用场景:
        a.对于固有逻辑的封装,比如报警系统的通知服务
        b.有大量类存在依赖关系,可使用外观模式,也可以选择不对逻辑封装
        ...

  • 相关阅读:
    The Python Standard Library
    Python 中的round函数
    Python文件类型
    Python中import的用法
    Python Symbols 各种符号
    python 一行写多个语句
    免费SSL证书(https网站)申请,便宜SSL https证书申请
    元宇宙游戏Axie龙头axs分析
    OLE DB provider "SQLNCLI10" for linked server "x.x.x.x" returned message "No transaction is active.".
    The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "xxx.xxx.xxx.xxx" was unable to begin a distributed transaction.
  • 原文地址:https://www.cnblogs.com/ben-mario/p/11133111.html
Copyright © 2011-2022 走看看