zoukankan      html  css  js  c++  java
  • Head First Design Patterns Template Method Pattern

    The Template Method Pattern defines the skeleton of algorithm in a method, define some steps to 

    subclasses. Template Method lets subclass redefine certain steps of an algorithm without changing

    the algorithm's structrue.

    Class diagram:

    abstract class AbstractClass{

      final void templateMethod(){

        primitiveOperation1();

        primitiveOperation2();

        concreteOpration();

      }

      abstract void primitiveOperation1();

      abstract void primitiveOperation2();

      void concreteOperation(){

        // implementation here

      }

      void hook(){}

    }

    A hook is a method that is declared in the abstract class, but only given an empty or default implementation,use hooks

    when that part of the algorithm is optional, with hooks, a subclass may choose to implement that hook, but it doesn't have to.

    The Hollywood Priciple:

    • Don't call us, we'll call you

    the Hollywood principle gives us a way to prevent "dependency rot". Dependency rot happens when you have high-level 

    components depending on low-level components.

    The Strategy and Template Method Patterns both encapsulate algorithms, one by inheritance and one by composition.

  • 相关阅读:
    ubuntu装openssh-client和openssh-server
    路由器开源系统openwrt配置页面定制
    linux 串口接收
    SHA算法
    密码学Hash函数
    椭圆曲线加密
    ElGamal密码
    Diffie-Hellman密钥交换
    RSA加密
    公钥密码学
  • 原文地址:https://www.cnblogs.com/zhuqiang/p/2474179.html
Copyright © 2011-2022 走看看