A simple factory model of PHP

Source: Internet
Author: User
This article is mainly for you to share a detailed explanation of PHP simple factory model, with a good reference value, I hope to be helpful to everyone. Follow the small series together to see it.

1. Definition of Simple factory

provides a capability to create an instance of an object without having to relate to its specific implementation, and the type of the instance being created can be an interface, an abstract class, or a concrete class.

2. Structure and description of the simple factory

The structure of a simple factory is as follows:

    • API: Define the functional interfaces required by the customer

    • Impl: Implements the implementation class of the API specifically, there may be multiple

    • Factory: Factory, select the appropriate implementation class to create the API interface object

    • Client: Clients, Get API interface object through factory, and then program for API interface

API interface:

/** * Interface definition, this interface can be created through a simple factory *  * @author Administrator * */public interface Api {/** * Specific function method definition *  * @param s *            Required parameter */public void operation (String s);}

IMPLA:

/** * Interface specific Implementation object A *  * @author Administrator * */public class Impla implements API {@Overridepublic void operation (Strin G s) {//Implemented function System.out.println ("Impla s==" + s);}}

IMPLB:

/** * Interface Specific Implementation object B *  * @author Administrator * */public class IMPLB implements API {@Overridepublic void operation (Strin G s) {//Implemented function System.out.println ("IMPLB s==" + s);}}

Factory:

/** * Factory class, used to create API Object *  * @author Administrator * */public class Factory {/** * How to create API object specifically *  * @param condition *< c6/> incoming selection criteria from the external * @return */public static Api createapi (int condition) {////should be based on certain conditions to choose exactly which specific implementation object to create//These conditions can be imported from outside, can also be obtained from other channels//if there is only one implementation, you can omit the Conditional API API = NULL;IF (condition = = 1) {API = new Impla ();} else if (condition = = 2) {API = new Im PlB ();} return API;}}

Test class Client:

public class Client {public static void main (string[] args) {///through simple factory to get interface Object API API = FACTORY.CREATEAPI (1); Api.operation ( "Using simple Factory mode");}}

Simple Factory Call Order:

Naming recommendations for simple factories:

    • The class naming recommendation is "module name +factory", for example, the factory of the user module is called Userfactory

    • The method name is typically either "get+ interface name" or "Create+ Interface name". For example, if the user interface name is Userebi, the method name is usually Getuserebi or Createuserebi.

The main function of the simple factory approach is to " Choose the right implementation class ", or the source of the parameter is usually the focus:

    1. From client, incoming parameters

    2. From the configuration file, get the value for judging from the configuration file

    3. A value from the program's runtime, such as getting a value from the cache for a run-time

Advantages of a simple factory:

    1. Help encapsulation

Simple factory is simple, but it is very friendly to help us implement the package of components, and then let the component outside can be really interface-oriented programming.

2. Decoupling

The decoupling of the client and the specific implementation class is realized through a simple factory.

Disadvantages of a simple factory:

1. May increase the complexity of the client

2. Inconvenient expansion of sub-factory

The essence of a simple factory: choose to implement


When to choose a simple factory:

    • If you want to fully encapsulate the implementation of the isolation, so that the external only through the interface to manipulate the package, then you can choose a simple factory, let the client through the factory to obtain the corresponding interface, without concern for the specific implementation.

    • If you want to centrally manage and control the responsibility of creating objects externally, you can choose a simple factory.

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.