Design Patterns - Template Method

Template Method pattern is a very simple just use inheritance patterns can be achieved.


Inheritance => realization

Template Method structure consists of two parts, the first part is the abstract parent class, the second portion is the specific implementation subclasses.

Typically encapsulates algorithm framework subclass abstract parent class, including execution of the sequence to achieve common methods for packaging and in all methods subclass
sequence. Subclasses inherit the abstract class inherits the entire structure of the algorithm selection method and can override the parent class


Template pattern is a typical method of improving the design by encapsulating the change patterns of the extended system. In traditional object-oriented language
words in a program using the template method pattern, the method of the type and order of execution of sub-class they are the same, so we put
this part of the abstract logic inside the template approach to the parent class. The method how to achieve specific subclass is variable, so we put
the logic portion of the package to change subclass. By adding new sub-class, we will be able to add new features to the system, do not need to change the
dynamic abstract superclass and other subclasses, which is in line with the principle of open  the closure.

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <Title> Template Method </ title>
</head>
<body>
    
</body>
<script>
  var   do =   function () {

  }
  lianAi.prototype.renshi =   function () {
       console.log ( ' we finally met ' )
  };
  lianAi.prototype.tianjia   = function () {
       the console.log ( ' added a communications software ' )
  };
  lianAi.prototype.yuehui = function () {
       console.log ( ' dating ' )
  };
  lianAi.prototype.gaobai = function () {
        the console.log ( ' confession ' )
  };
  lianAi.prototype.dengdai = function () {
        the console.log ( ' waits for a reply message ' )
  };
  lianAi.prototype.init = function () {
        the this .renshi ();
        the this .tianjia ();
        the this .yuehui ();
        the this .gaobai ();
        the this .dengdai ();
  }




  var  Xiaoming  = function() {

  };
  Xiaoming.prototype = new do ();
  Xiaoming.prototype.renshi = function () {
      the console.log ( ' and Mary know ' );
  }
  Xiaoming.prototype.yuehui = function () {
      the console.log ( ' the date of the cinema ' );
  }
  Xiaoming.prototype.gaobai = function () {
       the console.log ( ' to the advertising Mary ' )
  };
  var  xiaoming =new Xiaoming();
  xiaoming.init();
  // and Mary met
   // add a communications software
   // to the cinema dating
   // to Mary confession
 // wait for a reply message
 </script> 
</html>

Love can be summarized as five processes:

(1) understanding

(2) plus a software, such as micro-Letter

(3) two dating

(4) boys Confessions

(5) Awaiting Reply

 

 

 

(1) understanding

lianAi.prototype.renshi =   function () {
       console.log ( ' we finally met ' )
  };

(2) plus a software, such as micro-Letter

lianAi.prototype.tianjia = function () {
       the console.log ( ' added a communications software ' )
  };

(3) two dating

  lianAi.prototype.gaobai = function () {
        the console.log ( ' confession ' )
  };

(4) boys Confessions

lianAi.prototype.yuehui = function () {
       console.log ( ' dating ' )
  };

(5) Awaiting Reply

 

lianAi.prototype.dengdai = function () {
        the console.log ( ' waits for a reply message ' )
  };

Initialization method

= lianAi.prototype.init function () {
        the this .renshi ();
        the this .tianjia ();
        the this .yuehui ();
        the this .gaobai ();
        the this .dengdai ();
  }

Subclasses inherit the parent class above:

 

  var  Xiaoming  = function() {

  };
  Xiaoming.prototype = new do ();

The method of the parent class subclass overrides

  = Xiaoming.prototype.renshi function () {
      the console.log ( ' and Mary know ' );
  }
  Xiaoming.prototype.yuehui = function () {
      the console.log ( ' the date of the cinema ' );
  }
  Xiaoming.prototype.gaobai = function () {
       the console.log ( ' to the advertising Mary ' )
  };

Directly call init method, since the prototype and the prototype objects xiaoming xiaoming constructors are not corresponding to the init method,

Therefore, the request will follow the prototype chain,

init method of the "parent" lianAi xiaoming is delegated to the prototype.

 

Guess you like

Origin www.cnblogs.com/guangzhou11/p/11074482.html