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.