This time I will bring you PHP factory mode use cases and analysis. What are the precautions for PHP factory mode use cases and analysis. The following is a practical case, let's take a look.
Factory Design Pattern, as a creative design pattern, follows the open-closed principle, closed to modification and open to extension. The Factory Method (Factory Method) pattern is to create "something ". With the Factory Method pattern, the "thing" to be created is a product, and there is no binding between this product and the class that creates it. In fact, in order to maintain this loose coupling, the client will make the request through a factory. Then The requested product is created by the factory. You can also think about it in another way, using the factory method pattern. The requester only issues a request without specifically creating the product.
Work of the factory
First create a factory interface
Factory.php
<?php abstract class Factory { //抽象的创建对象的方法 protected abstract function createProduct(); //该方法调用createProduct方法返回一个产品对象. public function start() { return $this->createProduct(); } }
start
The method returns a product. The method calls the createProduct
method to complete the operation of generating a product. Therefore, the specific implementation of createProduct must build and return a product object implemented according to the Product interface.
For example, products all have a common methodgetProperties()
, the following is the corresponding Product interface
Product.php
<?php //产品接口 interface Product { public function getProperties(); }
Next, we need to create two factories, the text factory TextFactory and the image factory phptoFactory
TextFactory.php
<?php include_once('Factory.php'); include_once('TextProduct.php'); class TextFactory extends Factory { protected function createProduct() { $product = new TextProduct(); return $product->getProperties(); } }
PhotoFactory.php
<?php include_once('Factory.php'); include_once('PhotoProduct.php'); class PhotoFactory extends Factory { protected function createProduct() { $product = new PhotoProduct(); return $product->getProperties(); } }
As you can see, in the implementation of the factory method, The getProperties method introduces polymorphism and will use this method to return "text" or "image". The same getProperties()
has multiple (poly) different morphs, which is polymorphism. state. In this case, one of the forms returns text, and the other returns an image.
You can put whatever you want in the properties implementation, and the factory method design will create this Object, and returns it to the Client for use.
The following is the implementation of the two products
TextProduct.php
<?php include_once('Product.php'); class TextProduct implements Product { public function getProperties() { return "这里是文本产品"; } }
PhotoProduct .php
<?php include_once('Product.php'); class PhotoProduct implements Product { //这是产品具有的方法 public function getProperties() { return "这里是图像产品"; } }
These two products implement the abstract method in the Product interfacegetProperties()
,
Customer (Client)
We don’t want customers to make product requests directly. In fact, we want customers to make requests through the Factory interface. In this way, if we add products or factories in the future, Clients can make the same request to get more types of products without breaking the application:
Client.php
<?php include_once('PhotoFactory.php'); include_once('TextFactory.php'); class Client { public function construct() { $this->somePhotoObject = new PhotoFactory(); echo $this->somePhotoObject->start() . '<br />'; $this->someTextObject = new TextFactory(); echo $this->someTextObject->start() . '<br />'; } } $worker = new Client();
Run Client.php and get the following The result
Here is the image product
Here is the text product
Note: The Client object does not make a request directly to the product. Instead, the request is made through the factory. The important thing is that the customer does not implement the product features, leaving it to the product implementation.
Adjust the product
The true meaning of the design pattern The value is not to increase the speed of operation, but to speed up development.
If the needs change now and you need to make changes to the image product, you only need to modify the getProperties method of the corresponding product PhotoProduct
The change of the object seems very simple, but the Product's getProperties()
method still maintains the same interface, requesting the factory to return a property object
Add new products and parameterization Request
The question is, if you want to add more images and text descriptions, is it necessary to add a new specific factory class every time a new area is added? This means that Each new area adds a new factory and product. Therefore, we introduced the parametric factory design pattern
One of the main differences between the parametric factory design pattern and the general factory design pattern is that the customer includes factories and products Reference. In a parameterized request, the Client class must specify the product, not the product factory. The parameter in the createProduct()
operation is a product passed in by the customer; so the customer must indicate the specific product it wants . However, the request is still made through the factory interface Factory. So, although the customer contains a product reference, the customer is still separated from the product through Factory.
一个工厂多个产品(参数化工厂方法)
对于大多数请求, 参数化工厂方法更为简单, 因为客户只需要处理一个具体工厂.工厂方法操作有一个参数,指示需要创建的产品.而在原来的设计中, 每个产品都有自己的工厂, 不需要另个传递参数; 产品实现依赖于各个产品特定的工厂.
新工厂接口
Factory.php
<?php abstract class Factory { //抽象的创建对象的方法 protected abstract function createProduct(Product $product); //该方法由factoryMethod方法返回一个产品对象. public function start($product) { return $this->createProduct($product); } }
在这个新的Factory接口中可以看到, create()
和start()
都需要一个参数,指定一个Product对象, 而不是Product接口的一个特定实现, 所以可以接受任何Product的具体实例.
工厂具体实现
具体的创建者类CommonFactory实现了createProduct()
,如下
CommonFactory.php
<?php include_once('Factory.php'); include_once('Product.php'); class CommonFactory extends Factory { protected function createProduct(Product $product) { return $product->getProperties(); } }
这个类调用Product的方法getProperties将产品返回给客户.
新产品
具体产品的变化并不会改变原来的Product接口,还是原来的代码
<?php //产品接口 interface Product { public function getProperties(); }
例如, 现在有一个钢笔产品PenProduct
PenProduct.php
<?php include_once('Product.php'); class PenProduct implements Product { public function getProperties() { return "钢笔产品"; } }
客户Clent(有参数)
<?php include_once('CommonFactory.php'); include_once('PenProduct.php'); class Client { public function construct() { $commonFactory = new CommonFactory(); echo $commonFactory->start(new PenProduct()); } } $worker = new Client();
运行后输出
钢笔产品
以后如果开发出了新的产品, 只需要创建对应的产品类, 然后客户指定想要的新产品 , 即可返回客户需要的产品.
总结:
产品改变: 接口不变
使用设计模式的一大好处就是可以很容易地对类做出改变, 而不会破坏更大的程序. 之所以能够容易地做出改变, 秘诀在于保持接口不变, 而只改变内容.
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of PHP factory pattern use cases and analysis. For more information, please follow other related articles on the PHP Chinese website!