Statement: This series of blog reference "Big Talk design mode", author Geoscience.
The template schema prepares an abstract class, implements some of the logic in concrete ways and concrete constructs , and then declares some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways , thus having different implementations of the remaining logic. First, a top-level logical framework is developed, and the details of the logic are left to the specific subclasses to implement.
UML Class Diagrams:
Role:
Abstract Template Role ( Makephone ): abstract template class, which defines a specific algorithm flow and some abstract methods that must be implemented for subclasses.
specific sub-class roles ( Xiaomi ): Implement Makephone the subclass can have its own unique implementation form, but the execution process is subject to the Makephone Control .
Core code:
<?php/** * Created by phpstorm-> * User extends Jang * Date extends 2015/6/10 * Time extends one extends 06 */ Abstract class makephone{protected $name; Public function __construct ($name) {$this->name= $name; Public Function Makeflow () {$this->makebattery (); $this->makecamera (); $this->makescreen (); echo $this->name. " Mobile phone production finished!
To invoke the client test code:Header ("Content-type:text/html;charset=utf-8");//-------------------------template mode---------------------require_ Once "./template/template.php"; $miui =new Xiaomi (); $flyMe =new flyMe (); $miui->makeflow (); $flyMe->makeflow ();
application Scenarios and advantages:
1 , a process or series of steps that complete a level of detail, but the implementation of individual steps at a more detailed level may not be the same. We usually consider using template mode for processing.
2 , when immutable and mutable behaviors are mixed together in a subclass implementation of a method, the invariant behavior is repeated in the subclass, and we move the behavior to a single place through the template pattern, which helps the subclass to get rid of the entanglement of repeated invariant behavior.
3 , template mode is the advantage of removing repetitive code from subclasses by moving unchanged behavior to super abstract classes. Template mode provides a good code reuse platform.
Welcome to follow my video course, the address is as follows, thank you.
PHP Object-oriented design pattern
PHP design mode-template mode