Big Talk Design pattern Tenth Chapter---Template method mode PHP implementation

Source: Internet
Author: User

<?PHPAbstract classAbstract_class {Abstract  Public functionPrimitive_operation1 (); Abstract  Public functionPrimitive_operation2 ();  Public functionTemplate_method () {Echo"This is the template_method<br/>"; $this-Primitive_operation1 (); $this-Primitive_operation2 (); }}classConcrete_class_aextendsAbstract_class { Public functionPrimitive_operation1 () {Echo"Concrete Class A Method 1 implementation <br/>"; }     Public functionPrimitive_operation2 () {Echo"Concrete Class A Method 2 implementation <br/>"; }}classConcrete_class_bextendsAbstract_class { Public functionPrimitive_operation1 () {Echo"Concrete Class B Method 1 Implementation <br/>"; }     Public functionPrimitive_operation2 () {Echo"Concrete Class B Method 2 Implementation <br/>"; }}$c=Newconcrete_class_a ();$c-Template_method ();$c=Newconcrete_class_b ();$c->template_method ();

Template method Mode:

Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.

The template method pattern is to realize its advantage by moving the invariant behavior to the superclass and removing the duplicated code in the subclass.

When immutable and mutable behaviors are mixed together in a subclass implementation of a method, the invariant behavior repeats itself in the subclass. We move these behaviors to a single place (superclass) through the template method pattern, which helps the subclass to get rid of the entanglement of repeated invariant behavior.

Big Talk Design pattern Tenth Chapter---Template method mode PHP implementation

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.