PHP design model-simple factory

Source: Internet
Author: User

PHP design model-simple factory

 

The previous two sections introduced the design patterns and six principles. I believe that after reading the previous two sections, you have a preliminary understanding of the design patterns. Next, let's talk about the classification of the design patterns.

 

Object-Oriented Design Patterns are generally divided into three types: creation, structure, and behavior.

 

Creation type: when creating an object, we do not directly instantiate the object. Instead, the program determines the method of creating the object based on the specific scenario, so as to ensure greater performance and better architecture advantages. The creation mode mainly includes simple factory mode (not one of 23 design modes), factory method, abstract factory mode, Singleton mode, generator mode, and prototype mode.

 

Structure: Used to help organize multiple objects into a larger structure. The structural mode includes the adapter mode, Bridge Mode, bundle mode, decorator mode, facade mode, hengyuan mode, and proxy mode.

 

Behavior: it helps communication between objects and controls processes in a complex system. The behavior modes mainly include command mode, interpreter mode, iterator mode, intermediary mode, memo mode, observer mode, status mode, policy mode, template mode, visitor mode, and responsibility chain. mode.

 

Today, we will mainly introduce the first simple factory mode for creating models.

Note: when reading this series of blogs, you must have the basics of reading UML class diagrams and object-oriented PHP programming.

 

 

The simple factory model is not one of 23 common object-oriented design models. The simple factory mode is determined by a factory object to create a product instance. The simple factory model is the simplest and Practical Model in the factory model family. It can be understood as a special implementation of different factory models. The essence is that a factory class dynamically determines which product class should be created (these product classes inherit from a parent class or interface) based on the input parameters.

 

Roles and responsibilities:

SimpleFactory role: the Core of the simple factory model, which is responsible for implementing the internal logic for creating all instances. The factory class can be directly called by the outside world to create the required product objects.

Abstract product (IProduct) role: the parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances.

The role of a specific Product is the creation target of the simple factory model. All created objects are instances of a specific class that acts as the role.

Requirement: Create Product objects with corresponding features from a simple factory based on the property values provided.

The following PHP code is compiled based on the above UML class diagram.

 

 XMax = $ xMax; $ this-> yMax = 1;} function XRotate () {echo Hello, I am an X axis rotating product, and I am a X axis rotating product ......;} Function YRotate () {echo sorry, I'm a X axis rotating product and I don't have the Y axis ......; }/** Specific product role * Class YProduct Y axis rotating product */class YProduct implements IProduct {private $ xMax = 1; private $ yMax = 1; function _ construct ($ xMax, $ yMax) {$ this-> xMax = 1; $ this-> yMax = $ yMax;} function XRotate () {echo sorry, I am a Y axis rotating product, and I don't have the X axis ......;} Function YRotate () {echo hello, I'm a Y-axis rotating product. The Y-axis rotating ......; }/** Specific product role * Class XYProduct XY axis can rotate products */class XYProduct implements IProduct {private $ xMax = 1; private $ yMax = 1; function _ construct ($ xMax, $ yMax) {$ this-> xMax = $ xMax; $ this-> yMax = $ yMax;} function XRotate () {echo hello, I can rotate products on the XY axis, and rotate products on the X axis ......;} Function YRotate () {echo Hello, I am a XY-axis rotating product, and the y-axis rotating ......; }/** Factory role * Class ProductFactory */class ProductFactory {static function GetInstance ($ xMax, $ yMax) {if ($ xMax> 1 & $ yMax ===1) {return new XProduct ($ xMax, $ yMax );} elseif ($ xMax ===1 & $ yMax> 1) {return new YProduct ($ xMax, $ yMax );} elseif ($ xMax> 1 & $ yMax> 1) {return new XYProduct ($ xMax, $ yMax) ;}else {return null ;}}}

Test code:

 

 

 ; $ V-> XRotate (); echo; $ v-> YRotate ();} illegal else {echo product !; } Echo
;}
Use a browser to access the test code. We can find that the created objects are YProduct, XProduct, XYProduct, and null. The core code of a simple factory lies in the role of ProductFactory. Here we create different objects based on the passed xMax and yMax values, which is the essence of a simple factory, in addition, we do not know what the specific product class is like when testing the call client, so that the call and creation are separated.

 

 

Advantages of a simple factory: separates the caller of an object from the object creation process. When the caller of an object needs the object, the caller can directly request it from the factory. This avoids the coupling of the caller of the object and the implementation class of the object in hard coding mode to improve the maintainability and scalability of the system.

Disadvantages of a simple factory: when the product is modified, the factory class also needs to be modified accordingly. For example, to add an operation class, for example, to calculate the Npower of M, you have to change the case, modifying the original class violates the open-closed principle.

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.