Head First design mode the strategy mode of reading notes

Source: Internet
Author: User

As a PHP developer, know that many programmers have despised PHP, why? Because they think PHP syntax is dirty, and because of the uneven level of developers, PHP code is more chaotic, maintenance is almost a lump of shit. As PHP joined the object-oriented formation, many developers began to use OOP ideas to write code, and PHP became more and more standard and more standardized. And the design pattern plays a very important role. Recently, the boss to talk to me, said the PHP side of the development module coupling over high, the code is not feeling high quality, want to try a code refactoring action. My code refactoring is also smattering, and the basis of code refactoring is to understand design patterns, so I turned up this <

So, let's start with an appetizer today, write a simple note on Strategy mode and get your own understanding.

  1. What the hell is a strategy model?

Let's start with an official explanation: The policy pattern defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other. The policy pattern makes the algorithm independent of the customers who use it. Simply put, it is quite a class that can be switched at any time to provide it to the client. It may be said that long more abstract, do not matter, from the example slowly.

2, from the code to explain

In fact, the strategy model can be said to be a kind of behavior. If we have experience with the development of Yii or other PHP frameworks, we can see that these frameworks provide us with a behavioral mechanism. What is behavior? Behavior is something that a class can have at any time and can be deleted and changed at any time. Like human behavior, our behavior can have fights, eat, discuss, dance square dance, normally, these actions we are not born, but from the outside to obtain. Conversely, the same is true for classes. Then you can say, I can be born, ah, I inherit my parents and grandparents can not? Well, to meet your wishes, let's take a look at inheritance, on the code:

//FatherclassFather { Public functioneat () {} Public functionDance () {} Public functionSaymyname () {}}//son 1 Xiao MingclassXiaoming { Public functioneat () {Echo' Like to eat tomato scrambled eggs '; }      Public functionDance () {Echo' Like to dance Jazz '; }      Public functionSaymyname () {Echo' Xiao Ming '; }}//son 2 Little MoeclassXiaomeng { Public functioneat () {Echo' Like to eat potatoes and fry meat '; }    //Sorry, I don't like dancing//Public Function Dance () {//echo ' like to dance Jazz '; // }     Public functionSaymyname () {Echo' Little Moe '; }}$xiaoming=Newxiaoming ();$xiaomeng=NewXiaomeng ();$xiaoming-eat ();$xiaoming-dance ();$xiaomeng-eat ();$xiaoming->dance ();//I don't like dancing, but I can Dance.

Can obviously see the inheritance of a disadvantage is that some methods will be forced to "plug into" a class, so it is not flexible, not robust, and if one day Xiao Ming suddenly like to eat "fried meat", it would have to change the Eat method of Xiao Ming class, it can not be modified dynamically?

At this time, the "combination of the first" strategy model can be shot, this time we first write a eatbehavior and Dancebehavior interface, the code is as follows:

Interface Eatbehavior {    publicfunction  eat ();} Interface Dancebehavior () {    publicfunction  Dance ();}

Then, for this eating and dancing behavior interface, we're going to do a series of different eating and dancing behavior classes, the code is as follows:

classEatxihongshibehaviorImplementsEatbehavior { Public functioneat () {Echo' Scrambled eggs with tomatoes '; }}classEattudoubehaviorImplementsEatbehavior { Public functioneat () {Echo' Potato slices '; }}classJuqshibehaviorImplementsDancebehavior { Public functionDance () {Echo' Jazz Dance '; }}classJiewubehaviorImplementsDancebehavior { Public functionDance () {Echo' Hip-Hop '; }}

As you can see, we have two eat behaviors and two dance behaviors here, so we can get rid of the Eat () and dance () of the Father class, and the three classes of our Father, Xiaoming, and Xiao Meng are simplified into this way:

  

//FatherclassFather {//Public function Eat () {}//Public function Dance () {}     Public functionAddeatbehavior (Eatbehavior$eb) {        $eb.eat (); }     Public functionAdddancebehavior (Dancebehavior$db) {        $db.Dacne (); }     Public functionSaymyname () {}}//son 1 Xiao MingclassXiaoming { Public functionSaymyname () {Echo' Xiao Ming '; }}//son 2 Little MoeclassXiaomeng { Public functionSaymyname () {Echo' Little Moe '; }}

We can see that our little Moe has no need to force the integration of dance behavior, but can have a way to add dance behavior, so much more free! At this time, if we want Xiao Ming to learn to dance jazz, then we use $xiaoming->addeatbehavior (new Juqshibehavior), we want to jump the street dance, we can $xiaoming-> Addeatbehavior (New Jiewubehavior ()), want to learn other dance species, write more than one class to inherit the Dancebehaivor interface is OK!

The advantage of this is that it is flexible and able to dynamically switch the functions of the combined class itself, without having to hard inherit all the methods of the parent class, and the attentive friend discovers that the Addeatbehavior or Adddancebehavior parameter type is an interface class, which conforms to the design pattern " The principle of programming for interfaces, rather than implementations, so that we can polymorphic pass in different eating and dancing behavior classes to get these functions, so that the random combination of split chapters makes classes and classes more loose. In the framework, we use the behavior is equal to the use of this design pattern, so in the future development, I would like to write more of the framework of the behavior of the class, and then the redundant repeated to the proposed behavior, so that different controllers arbitrarily combination, so that can be further decoupled. Okay, this is the strategy mode!

 

Head First design mode the strategy mode of reading notes

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.