Getting started with design Patterns-Template Method Mode (PHP version)

Source: Internet
Author: User
A joke says: How many steps does it take to put an elephant into a refrigerator?

One, open the refrigerator

Two, put the elephant in

Three, close the refrigerator

In the same vein, how many steps does it take to put a lion in the freezer?

One, open the refrigerator

Two, put the Lions in

Three, close the refrigerator

In the above example, do you find that the two methods have a common step, but the specific implementation is slightly different, in short, these two types of behavior can share a step template. This leads to the design pattern-template design pattern to be discussed.

The principle of template design pattern can be represented by a UML class diagram as shown below:

A specific code example:

lockanimal.php

 
  Open ();       $this->push ();       $this->close ();   }       /**    * Open the refrigerator *   /abstract function open ();      /**    * Push the animal into the refrigerator *   /abstract function push ();      /**    * Close the refrigerator * *   abstract function Close ();}
lockelephant.php
 
  ";   }      /**    * (non-phpdoc)    * @see lockanimal::p ush () */public   function push () {       echo "I ' m pushing the Elephant
"; } /** * (non-phpdoc) * @see lockanimal::close () */public function close () { echo ' Finally, now I can close the fridge
"; }}
locklion.php
 
  ";   }      /**    * (non-phpdoc)    * @see lockanimal::p ush () */public   function push () {       echo "I ' m pushing the lion
"; } /** * (non-phpdoc) * @see lockanimal::close () */public function close () { echo ' Finally, now I can close the fridge
"; }}

Now the problem, excavator technology which strong? A joke. Some animals are more ferocious, will not obediently be pushed into the refrigerator, need anesthesia to do, we need to add a hook for our program to deal with this situation. The modified code is as follows:

lockanimal.php

 
  Open ();       if ($this->needanesthetic ()) {           $this->anesthetic ();       }       $this->push ();       $this->close ();   }       /**    * Open the refrigerator *   /abstract function open ();      /**    * Whether anesthesia is required *   /protected function needanesthetic () {       return false;   }      protected function anesthetic () {       echo "anestheticing the Animal";   }      /**    * Push the animal into the refrigerator *   /abstract function push ();      /**    * Close the refrigerator * *   abstract function Close ();}

lockelephant.php
 
  ";   }      /**    * (non-phpdoc)    * @see lockanimal::p ush () */public   function push () {       echo "I ' m pushing the Elephant
"; } /** * (non-phpdoc) * @see lockanimal::close () */public function close () { echo ' Finally, now I can close the fridge
"; }}
locklion.php
 
  ";   }      protected function needanesthetic () {       return true;   }      protected function anesthetic () {       echo "anestheticing the Lion
"; } /** * (non-phpdoc) * @see lockanimal::p ush () */public function push () { echo "I ' m pushing the Lion
"; } /** * (non-phpdoc) * @see lockanimal::close () */public function close () { echo ' Finally, now I can close the fridge
"; }}
index.php
 
  Lock (); $lion->lock ();

The official definition of the template method design pattern is to define the skeleton of an algorithm in one method and defer some steps to the subclass. The template method allows subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm.

The above describes the design pattern Primer-Template Method Mode (PHP version), including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.