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 subclass roles (Xiaomi): Implement the abstract methods in Makephone, subclasses can have their own unique implementation form, but the execution process is controlled by Makephone.
Core code:
* User extends Jang * Date extends 2015/6/10 * Time extends one extends 06 *///Abstract template Class abstract class makephone{Pro Tected $name; Public function __construct ($name) {$this->name= $name; Public Function Makeflow () {$this->makebattery (); $this->makecamera (); $this->makescreen (); echo $this->name. " Mobile phone production finished!
"; } public abstract function Makescreen (); public abstract function makebattery (); public abstract function Makecamera ();} Xiaomi phone class Xiaomi extends makephone{public function __construct ($name = ' millet ') {parent::__construct ($name); } public Function Makebattery () {echo "Xiaomi battery production finished!
"; } public Function Makecamera () {echo "Xiaomi camera production finished!
"; } public Function Makescreen () {echo "Xiaomi screen production finished!
"; }}//Meizu Mobile class FlyMe extends makephone{function __construct ($name = ' Meizu ') {parent::__construct ($name); } public Function Makebattery () {echo ' Meizu battery production finished!
"; } public Function Makecamera () {echo "Meizu camera production finished!
"; } public Function Makescreen () {echo ' Meizu screen 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, complete a level of detail a process or a series of steps, but its individual steps at a more detailed level of implementation may not be the same. We usually consider using template mode for processing.
2, when the invariant and variable behavior in the subclass implementation of the method is mixed together, the invariant behavior will be repeated in the subclass, we use template mode to move these behaviors to a single place, so as to help the subclass out of the repetition of the constant behavior of entanglement.
3, template mode by moving the invariant behavior to the Super abstract class, remove the duplicate code in the subclass to reflect its advantages. 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