PHP-template form

Source: Internet
Author: User
PHP-template Mode 1. definition: defines the framework of an operation algorithm, and delays the implementation of some steps to subclass. in fact, it means that the skeleton of the operating algorithm remains unchanged, that is, the first step should be done, the second step should be done, and the second step should be done again, but no specific content is specified. 2. abstractClass (abstract class): defines one or more abstract methods, and the PHP-template mode.
1. Definition: defines the framework of an operation algorithm and delays the implementation of some steps to subclass.
In fact, it means that the skeleton of the operating algorithm remains unchanged, that is, the first step should be done, the second step should be done, and the second step should be done again, but no specific content is specified.
2. Illustration

AbstractClass (abstract class): defines one or more abstract methods for specific subclasses to implement them. it also implements a Template method to define the skeleton of an algorithm.
ConcreteClass (specific class): implements abstract methods in the parent class to complete steps related to specific subclass in the algorithm


In this example, shopping in A supermarket is very regular. he generally goes to the book area first, and then to the vegetables and fruits area, and then to the clothing, shoes, and hats area, finally, go to the digital home appliance zone (which is equivalent to AbstractClass)
That is to say, I went to the supermarket once last week.
When I first arrived at the book area and saw Mo Yan's novel, I felt that I should have bought one with the masses. < <丰乳肥臀> > I immediately felt like a young man of literature and art;
Then I went to the fruits and vegetables area and saw my favorite oranges .....;
Then I went to the clothing, shoes, and hats area, tried the latest model of Wang and bought it;
In the digital home appliance zone, I took a fancy to a mobile phone and had no money to buy it.
(This is equivalent to ConcreteClass)

Convention code:
1. abstract class
abstract class AbstractClass{    public function trip(){        $this->bookTrip();        $this->vegetablesTrip();        $this->shoesTrip();        $this->digitalTrip();    }    abstract protected function bookTrip();    abstract protected function vegetablesTrip();    abstract protected function shoesTrip();    abstract protected function digitalTrip();}

2. specific classes
Class ConcreteClass extends AbstractClass {protected function bookTrip () {echo "buy <
  <丰乳肥臀>
   
> \ N ";} protected function vegetablesTrip () {echo" buy 10 oranges \ n ";} protected function shoesTrip () {echo" Buy King \ n ";} protected function digitalTrip () {echo "no money, no phone \ n ";}}
  

3. client
$instance=new ConcreteClass();$instance->trip();


The final nature of the template mode is the fixed algorithm skeleton.

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.