PHP Factory Method Design Model case study

Source: Internet
Author: User
This time to bring you the PHP factory method Design model case, the PHP factory method design pattern of the use of considerations, the following is the actual case, together to see.

First, what is the factory method mode

As a creation design pattern, the factory method pattern is to create "something". For a factory method, the "thing" to be created is a product and there is no binding between the product and the class that created it. In fact, in order to maintain this loose coupling, the customer makes a request through a factory and creates the requested product from the factory. Using the factory method pattern, the requestor only makes the request, not the product.

Second, when to use the factory method mode

If the subclass of the instantiated object may change, use the factory method pattern.

Three, general factory method mode

When using the generic factory method pattern, the customer only contains references to the factory, and a factory produces a product. Adding a product requires adding a new factory class and a product class.

<?php/*** General Factory Method design Pattern **///Factory abstract class factory{protected abstraction function produce ();    Public Function Startfactory () {$pro = $this->produce ();  return $pro;    }}//text Factory class Textfactory extends factory{protected function produce () {$textProduct = new textproduct ();  return $textProduct->getproperties ();    }}//image Factory class Imagefactory extends factory{protected function produce () {$imageProduct = new imageproduct ();  return $imageProduct->getproperties (); }}//Product Class Interface interface product{public function getProperties ();  Text Product class Textproduct implements product{private $text;    function GetProperties () {$this->text = "here is Text";  return $this->text;  }}//image Product Class Imageproduct implements product{private $image;    function GetProperties () {$this->image = "image here";  return $this->image;  }}//Customer class client{private $textFactory;  Private $imageFactory; Public Function construct () {$this->textfactory = new TextfactorY (); echo $this->textfactory->startfactory ().    ' <br/> ';    $this->imagefactory = new Imagefactory (); echo $this->imagefactory->startfactory ().  ' <br/> '; }} $client = new client ();/* Run Result: Here is the text here for the image */?>

Four, parameter chemical plant method mode

When using the parametric chemical plant method mode, the customer contains a reference to the factory and the product, and the request requires a specified product type, and a factory produces a variety of products. To add a product, you only need to add a new product class.

<?php/*** Parametric chemical Plant method design Pattern **///Factory abstract class factory{protected abstraction function produce (Product $product);    Public Function startfactory (Product $product) {$pro = $this->produce ($product);  return $pro; }}//factory Implementation Class Concretefactory extends factory{protected function produce (Product $product) {return $product->get  Properties (); }}//Product Class Interface interface product{public function getProperties ();  Text Product class Textproduct implements product{private $text;    Public Function getProperties () {$this->text = "here is Text";  return $this->text;  }}//image Product Class Imageproduct implements product{private $image;    Public Function getProperties () {$this->image = "Here is an image";  return $this->image;  }}//Customer class client{private $factory;  Private $textProduct;  Private $imageProduct;    Public Function construct () {$factory = new concretefactory ();    $textProduct = new Textproduct ();    $imageProduct = new Imageproduct (); echo $factory->startfactory ($textproduct).    ' <br/> '; echo $factory->startfactory ($imageProduct).  ' <br/> '; }} $client = new client ();/* Run Result: Here is the text here for the image */?>

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Share image function

PHP Singleton mode use case study

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.