Home >php教程 >php手册 >PHP design pattern ------- (1) Strategy pattern

PHP design pattern ------- (1) Strategy pattern

WBOY
WBOYOriginal
2016-07-21 14:52:561178browse

1. Why should I learn design patterns.

My last project was to make an App interface. Due to the tight schedule, the boss was in a hurry, so when the project was completed, I found that I had written tens of thousands of lines of code. You can imagine how bad the code quality was. And many times, developers who call interfaces come to me and say that a certain interface is wrong. I go back to debug and find out that I accidentally wrote a wrong line of code. However, I have the same statement in six or seven controllers. This is As a result, I have to go back and find those six or seven files, and then modify them one by one. I was dumbfounded now, and suddenly realized that I really should look at design patterns, which can improve the quality of the code and prevent customers from grinding their teeth when modifying their requirements (everyone should have this experience, haha);

2. Strategy Mode

1. Concept

Strategy mode: defines a family of algorithms and encapsulates them separately so that they can be replaced with each other. This mode makes the changes of the algorithm independent of the customers who use the algorithm. (The concept is too difficult to understand)

2. Why is there strategy mode

The company where Joe works has made a duck simulation game. Various ducks will appear in the game, some can quack, and some can fly.

Code:

<span style="color: #008080;"> 1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Duck {
</span><span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> quack(){}
</span><span style="color: #008080;"> 4</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> swim(){}
</span><span style="color: #008080;"> 5</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> display(){}<br />       public function fly(){<br />           //会飞了<br />       }
</span><span style="color: #008080;"> 6</span>     <span style="color: #008000;">//</span><span style="color: #008000;">鸭子的其他方法  </span>
<span style="color: #008080;"> 7</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 8</span> 
<span style="color: #008080;"> 9</span> <span style="color: #0000ff;">class</span> MallarDuck <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Duck{
</span><span style="color: #008080;">10</span>      <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> display(){
</span><span style="color: #008080;">11</span>           <span style="color: #008000;">//</span><span style="color: #008000;">外观是绿头</span>
<span style="color: #008080;">12</span> <span style="color: #000000;">     }
</span><span style="color: #008080;">13</span> <span style="color: #000000;">}
</span><span style="color: #008080;">14</span> <span style="color: #0000ff;">class</span> RedDuck <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Duck{
</span><span style="color: #008080;">15</span>      <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> display(){
</span><span style="color: #008080;">16</span>           <span style="color: #008000;">//</span><span style="color: #008000;">外观是红头</span>
<span style="color: #008080;">17</span> <span style="color: #000000;">     }
</span><span style="color: #008080;">18</span> }

In this way, all subclasses that inherit the Duck class can implement flying behavior, but wouldn’t it be a joke if the toy duck couldn’t have flying behavior? Some people will say that I can override the fly method of the parent class in the subclass, so that different behaviors can be given to different specific subclasses. But in this case, wouldn't it be duplicating a lot of code? I believe everyone will copy it directly, so if something goes wrong in one place, everything will have to be changed again, and if the boss changes the requirements at any time, we will be directly exhausted.

So here are the principles in design patterns:

1. Find out the areas that may need to be changed in the application, isolate them, and do not mix them with code that does not need to be changed.

2. Program for interfaces, not for implementation.

3. Use combination more and inheritance less.

3. Specific code

<?<span style="color: #000000;">php
</span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Duck {
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$flyBehavior</span>;    <span style="color: #008000;">//</span><span style="color: #008000;">飞行行为</span>
    <span style="color: #0000ff;">public</span> <span style="color: #800080;">$quackBehavior</span>;  <span style="color: #008000;">//</span><span style="color: #008000;">叫行为</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> swim(){
        </span><span style="color: #0000ff;">echo</span> '鸭子会游泳'<span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> performQuack(){
        </span><span style="color: #800080;">$this</span>->quackBehavior-><span style="color: #000000;">quack();
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> performFly(){
        </span><span style="color: #800080;">$this</span>->flyBehavior-><span style="color: #000000;">fly();
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setFlyBehavior(FlyBehavior <span style="color: #800080;">$fb</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$this</span>->flyBehavior = <span style="color: #800080;">$fb</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setQuackBehavior(QuackBehavior <span style="color: #800080;">$qb</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$this</span>->quackBehavior = <span style="color: #800080;">$qb</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> display();
}

</span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> FlyBehavior {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> fly();
}
</span><span style="color: #0000ff;">class</span> FlyWithWings <span style="color: #0000ff;">implements</span><span style="color: #000000;"> FlyBehavior{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> fly(){
        </span><span style="color: #0000ff;">echo</span> '会飞'<span style="color: #000000;">;
    }
}
</span><span style="color: #0000ff;">class</span> FlyNoWay <span style="color: #0000ff;">implements</span><span style="color: #000000;"> FlyBehavior {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> fly(){
        </span><span style="color: #0000ff;">echo</span> '不会飞'<span style="color: #000000;">;
    }
}

</span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> QuackBehavior {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> quack();
}
</span><span style="color: #0000ff;">class</span> Quackd <span style="color: #0000ff;">implements</span><span style="color: #000000;">  QuackBehavior {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> quack(){
        </span><span style="color: #0000ff;">echo</span> '呱呱叫'<span style="color: #000000;">;
    }
}
</span><span style="color: #0000ff;">class</span> Squeak <span style="color: #0000ff;">implements</span><span style="color: #000000;">  QuackBehavior {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> quack(){
        </span><span style="color: #0000ff;">echo</span> '吱吱叫'<span style="color: #000000;">;
    }
}
</span><span style="color: #0000ff;">class</span> MuteQuack <span style="color: #0000ff;">implements</span><span style="color: #000000;">  QuackBehavior {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> quack(){
        </span><span style="color: #0000ff;">echo</span> '不会叫'<span style="color: #000000;">;
    }
}

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 模型鸭类
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">class</span> ModelDuck <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Duck
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct()
    {
        </span><span style="color: #800080;">$this</span>->flyBehavior = <span style="color: #0000ff;">new</span><span style="color: #000000;"> FlyNoWay();
        </span><span style="color: #800080;">$this</span>->quackBehavior = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Quackd();
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> display()
    {
        </span><span style="color: #0000ff;">echo</span> '模型鸭'<span style="color: #000000;">;
    }
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">测试代码</span>
<span style="color: #800080;">$m</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ModelDuck();
</span><span style="color: #800080;">$m</span>-><span style="color: #000000;">display();
</span><span style="color: #800080;">$m</span>-><span style="color: #000000;">performQuack();
</span><span style="color: #800080;">$m</span>-><span style="color: #000000;">performFly();
</span><span style="color: #800080;">$m</span>->setFlyBehavior(<span style="color: #0000ff;">new</span><span style="color: #000000;"> FlyWithWings());
</span><span style="color: #800080;">$m</span>->performFly();

Finally, design pattern is a kind of idea and does not need to be applied mechanically, otherwise it will be counterproductive.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn