PHP design pattern-template pattern Statement: This series of blog reference material "big talk design pattern", author Cheng Jie.
In the Template Mode, prepare an abstract class, implement part of the logic in specific methods and construction forms, and declare some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. First, create a top-level logic framework, and leave the logic details to specific sub-classes for implementation.
UML class diagram:
Role:
Abstract template role (MakePhone): an abstract template class that defines a specific algorithm flow and some abstract methods that must be implemented by subclass.
XiaoMi: implements the abstract method in MakePhone. the subclass can have its own unique implementation form, but the execution process is controlled by MakePhone.
Core code:
* User extends Jang * Date extends 2015/6/10 * Time extends 11 extends 06 * // abstract template class 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 is complete!
";}Public abstract function MakeScreen (); public abstract function MakeBattery (); public abstract function MakeCamera ();} // XiaoMi mobile phone class XiaoMi extends MakePhone {public function _ construct ($ name = 'Xiaomi ') {parent ::__ construct ($ name);} public function MakeBattery () {echo "Xiaomi battery production is complete!
";} Public function MakeCamera () {echo" Xiaomi camera production is complete!
";} Public function MakeScreen () {echo" Xiaomi screen production is complete!
";}}// Meizu mobile phone class FlyMe extends MakePhone {function _ construct ($ name = 'meizu ') {parent ::__ construct ($ name );} public function MakeBattery () {echo "Meizu battery finished!
";}Public function MakeCamera () {echo" Meizu camera production is complete!
";}Public function MakeScreen () {echo" Meizu screen production is complete!
";}}
Call the client test code:
Header ("Content-Type: text/html; charset = utf-8"); // ------------------------- Template Mode ----------------------- require_once ". /Template. php "; $ miui = new XiaoMi (); $ flyMe = new FlyMe (); $ miui-> MakeFlow (); $ flyMe-> MakeFlow ();
Applicable scenarios and advantages:
1. complete a process or a series of steps at a specific level of detail, but the implementation of individual steps at a more detailed level may be different. We usually consider using the template mode for processing.
2. when the immutable and variable behaviors are mixed in the implementation of sub-classes of the method, the immutable behaviors will be repeated in the sub-classes, we use the template mode to move these actions to a single place, so that the subclass can get rid of the entanglement of repeated unchanged behaviors.
3. the template mode moves unchanged behaviors to the Super abstract class, removing repeated code in the subclass to reflect its advantages. The template mode provides a good platform for code reuse.
Welcome to my video course. The address is as follows. thank you.
PHP object-oriented design model