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.