Home  >  Article  >  Backend Development  >  PHP design patterns (2)

PHP design patterns (2)

WBOY
WBOYOriginal
2016-08-08 09:33:08895browse

Since recently, I have set a goal for myself to update at least 2 blogs every week to record the problems I encountered or the new ideas I came up with in the previous week. On the one hand, I will record the knowledge I have mastered, so as to avoid unnecessary mistakes. After a long time, I forgot all about it. Secondly, although my blog posts are not very good, they may have a certain impact on a small number of readers, and I believe that as I write more and more original blog posts, the level will definitely improve. As you get higher and higher, the depth will get deeper and deeper (haha, I also envy those famous bloggers, who not only have great professional knowledge, but also have great writing skills). Two articles a week, I find that I can’t record everything I want to record. Just like this week, I also have a log system, a binary data cache (actually, it is more appropriate to call it a lower database), QQwry.dat data interpretation, and zip compression file addition. I have not written about these aspects of mass generation. I will slowly update it to my blog in the future. Okay, without further ado, this time I will introduce another model.

  • Simple factory mode

 In fact, this mode is also quite commonly used and should be introduced first. However, since I have come into contact with more singleton modes, I will introduce the singleton mode first. Simple Factory Pattern, first look at its definition: In terms of the type of design pattern, the Simple Factory Pattern is a creational pattern, also called the Static Factory Method pattern, but it is not one of the 23 GOF design patterns. one. The simple factory pattern uses a factory object to decide which product class instance to create. The simple factory pattern is the simplest and most practical pattern in the factory pattern family and can be understood as a special implementation of different factory patterns.

 What are the benefits of the simple factory pattern in projects? He has two benefits:

1. The first is to use the simple factory pattern to instantiate different classes according to different parameters, instead of using the new method to instantiate each different class, which provides users with better management.

2. Secondly, if the class to be instantiated is used in multiple files, when we modify the class name, we only need to modify the factory class, instead of modifying every file that instantiates the class (a bit tasteless) , this situation is rare).

Look at the most classic and best-understood example of a simple factory pattern, operator operation:

 The factory class of the simple factory pattern generally uses static methods to return different object instances by accepting different parameters. The code is hard-coded, so it cannot be extended without modifying the code, which violates OCP (for extension development, for modification) closure principle).

<?<span>php
</span><span>/*</span><span>*
 * 简单工厂模式&mdash;&mdash;经典运算符例子
 * @author 燕睿涛(luluyrt@163.com)
 </span><span>*/</span>
<span>/*简单</span><span>工********************厂********************类</span><span>*/</span>
<span>class</span><span> Operation{
    </span><span>/*</span><span>*
     * @var int $numa
     * 要操作的两个数字
     </span><span>*/</span>
    <span>protected</span> <span>$numa</span><span>;
    </span><span>protected</span> <span>$numb</span><span>;
    </span><span>public</span> <span>function</span> __construct(<span>$a</span>,<span>$b</span><span>){
        </span><span>$this</span>->numa = <span>$a</span><span>;
        </span><span>$this</span>->numb = <span>$b</span><span>;
    }

    </span><span>//</span><span>静态方法,通过接受不同的参数生成不同的对象实例</span>
    <span>public</span> <span>static</span> <span>function</span> create(<span>$operation</span>,<span>$a</span>,<span>$b</span><span>){
        </span><span>switch</span> (<span>$operation</span><span>) {
            </span><span>case</span> '+':
                <span>return</span> <span>new</span> Operationadd(<span>$a</span>,<span>$b</span><span>);
                </span><span>break</span><span>;
            
            </span><span>case</span> '-':
                <span>return</span> <span>new</span> Operationminus(<span>$a</span>,<span>$b</span><span>);
                </span><span>break</span><span>;
            </span><span>default</span>:
                <span>#</span><span> code...</span>
                <span>break</span><span>;
        }
    }

}
</span><span>/*</span><span>**********************************************</span><span>*/</span>

<span>/*</span><span>加法</span><span>*/</span>
<span>class</span> Operationadd <span>extends</span><span> Operation{
    </span><span>public</span> <span>function</span><span> doing(){
        </span><span>return</span> <span>$this</span>->numa + <span>$this</span>-><span>numb;
    }
}

</span><span>/*</span><span>减法</span><span>*/</span>
<span>class</span> Operationminus <span>extends</span><span> Operation{
    </span><span>public</span> <span>function</span><span> doing(){
        </span><span>return</span> <span>$this</span>->numa - <span>$this</span>-><span>numb;
    }
}

</span><span>$test</span> = Operation::create('+',2,56<span>);
</span><span>echo</span> <span>$test</span>->doing();
  • Factory Mode

I have only come into contact with the simple factory pattern in the project before. In order to write this article, I checked the information and found that there are three factory patterns: simple factory pattern (also called static factory pattern), factory pattern, and abstract factory pattern. After reading After the factory pattern, I feel that it is not very useful. It just conforms to the OCP principle. When there are new products that conform to the abstract product interface and abstract factory interface, we only need to extend a specific product and factory, without having to Modify the original code and summarize the advantages and disadvantages of the factory mode:

Advantages: First of all, it complies with the OCP principle and the scalability is improved; secondly, the maintainability is improved. When modifying a specific factory role, you only need to find your own factory role, and there is no need to worry about affecting the implementation of other factory roles.

Disadvantages: Too much code, each product requires a product class and a factory class. This shortcoming can be achieved by combining the simple factory pattern and the factory pattern, merging factory classes of similar product classes into one.

<?<span>php
</span><span>/*</span><span>*
 * 简单工厂模式&mdash;&mdash;经典运算符例子
 * @author 燕睿涛(luluyrt@163.com)
 </span><span>*/</span>
<span>/*</span><span>工*********厂**********方**********法</span><span>*/</span>
<span>//</span><span>交通工具接口(抽象产品角色)</span>
<span>interface</span><span> vehicle{
    </span><span>public</span> <span>function</span><span> runing();
}
</span><span>//</span><span>交通工具工厂接口(抽象工厂角色)</span>
<span>interface</span><span> vehiclefactory{
    </span><span>public</span> <span>static</span> <span>function</span><span> get();
}
</span><span>/*</span><span>具体产品角色</span><span>*/</span>
<span>class</span> car <span>implements</span><span> vehicle{
    </span><span>public</span> <span>function</span><span> runing(){
        </span><span>echo</span> "My speed is 120KM/h \r"<span>;
    }
}

</span><span>class</span> bicycle <span>implements</span><span> vehicle{
    </span><span>public</span> <span>function</span><span> runing(){
        </span><span>echo</span> "My speed is 30KM/h \r"<span>;
    }
}
</span><span>/*</span><span>具体工厂角色</span><span>*/</span>
<span>class</span> carfactory <span>implements</span><span> vehiclefactory{
    </span><span>public</span> <span>static</span> <span>function</span><span> get(){
        </span><span>return</span> <span>new</span><span> car();
    }
}
</span><span>class</span> bicyclefactory <span>implements</span><span> vehiclefactory{
    </span><span>public</span> <span>static</span> <span>function</span><span> get(){
        </span><span>return</span> <span>new</span><span> bicycle();
    }
}

</span><span>$test</span> = bicyclefactory::<span>get();
</span><span>$test</span>->runing();

The factory class has never been used in specific projects. It always feels a bit weird when talking about it. If there is anything wrong or inappropriate, I hope all the experts and seniors will point it out.

send Me~

The above has introduced PHP design pattern (2), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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