Home >Backend Development >PHP Tutorial >PHP design patterns (2), php design patterns_PHP tutorial

PHP design patterns (2), php design patterns_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:371012browse

PHP Design Pattern (2), PHP Design Pattern

Since recently, I have set a goal for myself to update at least 2 blogs every week to record my progress on the Internet. On the one hand, I record the problems I encounter or the new ideas I come up with during the week, so as not to forget them all over time. On the other hand, although my blog posts are not very good, they may be useful to a small number of readers. It has a certain impact, and I believe that as I write more and more original blog posts, the level will definitely become higher and higher, and the depth will become deeper and deeper (haha, I also envy those famous bloggers, not only The professional knowledge is great and the writing style is also great). 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 relatively common 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 a 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 model 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) feeling, which is rare).

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

Factory classes in the simple factory pattern generally use 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, Principle of closure for modification).

<?<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 my projects 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 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, and There is no need to modify the original code. Let’s 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();

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

send Me~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920433.htmlTechArticlePHP Design Pattern (2), PHP Design Pattern. Recently, I have set a goal for myself, at least once a week Update 2 blogs to record the problems you encountered or the new ones you came up with in the past week...
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