Php design patterns ------- (1) policy patterns, design patterns -------
I. Why do I need to learn design patterns.
My last project was an App interface. Due to the tight schedule, my boss was in a hurry. So at the end of the project, I found that tens of thousands of lines of code had been written, it can be imagined how bad the code quality is. In addition, many times, the developers who call the interface came to me and told me that an interface had an error. After I went back to debug the interface, I found that I was careless about writing a wrong code, however, I have the same statements in the six or seven controllers, which causes me to go back to the six or seven controllers and modify them one by one. This is dumb, and suddenly I found that I really should look at the design model, which can improve the code quality, so that when the customer modifies the demand, it will not make them bite your teeth (everyone should have this experience, haha );
Ii. Policy Mode
1. Concept
Policy mode: defines algorithm families, which are encapsulated separately so that they can be replaced with each other. This mode allows algorithm changes to be independent of algorithm customers. (The concept is too difficult to understand)
2. Why is there a policy pattern?
Joe's company at work played a duck simulation game, where various ducks emerged, some of which were screaming and some of which were flying.
Code:
1 <? Php 2 class Duck {3 public function quack () {} 4 public function swim () {} 5 public function display (){}
Public function fly (){
// Fly
} 6 // other Duck Methods 7} 8 9 class MallarDuck extends Duck {10 public function display () {11 // The exterior is green head 12} 13} 14 class RedDuck extends Duck {15 public function display () {16 // The exterior is red head 17} 18}
In this way, the sub-classes of the Duck class can implement flight behavior, but isn't it a joke if the Duck cannot perform flight behavior? Some people will say that I can rewrite the fly method of the parent class in the subclass so that different behaviors can be given to different specific subclasses. But isn't that a lot of code repeated? I believe that everyone will copy the file directly. In this way, if an error occurs in a certain place, all the parts should be changed again, and the boss will change the demand at any time, so we will be overwhelmed.
So the principles in the design pattern come:
1. Identify potential changes in the application and separate them from those that do not need to be changed.
2. Programming for interfaces, rather than implementing programming.
3. Multi-Purpose Combination and less inheritance.
Iii. Specific Code
<? Phpabstract class Duck {public $ flyBehavior; // flight behavior public $ quackBehavior; // call behavior public function swim () {echo 'duck swim ';} public function implements mquack () {$ this-> quackBehavior-> quack ();} public function initialize mfly () {$ this-> flyBehavior-> fly ();} public function setFlyBehavior (FlyBehavior $ fb) {$ this-> flyBehavior = $ fb;} public function setQuackBehavior (QuackBehavior $ qb) {$ this-> quackBehavior = $ qb;} abstract function display ();} interface FlyBehavior {public function fly ();} class FlyWithWings implements FlyBehavior {public function fly () {echo 'fly ';} class FlyNoWay implements FlyBehavior {public function fly () {echo '';} interface QuackBehavior {public function quack ();} class Quackd implements QuackBehavior {public function quack () {echo '';}} class Squeak implements QuackBehavior {public function quack () {echo 'squeaking';} class MuteQuack implements QuackBehavior {public function quack () {echo 'won't be called ';}} /*** model Duck class */class ModelDuck extends Duck {public function _ construct () {$ this-> flyBehavior = new FlyNoWay (); $ this-> quackBehavior = new Quackd ();} public function display () {echo 'model duck ';}// test code $ m = new ModelDuck (); $ m-> display (); $ m-> parse mquack (); $ m-> parse mfly (); $ m-> setFlyBehavior (new FlyWithWings ()); $ m-> mongomfly ();
Finally, the design pattern is an idea, so you don't have to stick to it. Otherwise, it will be counterproductive.