A _php tutorial on the factory model of PHP design pattern

Source: Internet
Author: User
In the article "Do You know PHP design Patterns", we have briefly introduced the factory model, and today we will look at the application scenario of the factory pattern in PHP development in detail.

For more information on PHP design patterns, please visit: PHP design mode

In the first book of design patterns, many design patterns encourage the use of loose coupling. To understand this concept, let's talk about the arduous journey of many developers working on large systems. A problem occurs when you change a code fragment, and other parts of the system--the ones you thought were completely unrelated--might also have cascading corruption.

The problem is tight coupling. Functions and classes in a part of the system depend heavily on the behavior and structure of functions and classes in other parts of the system. You need a set of patterns that enable these classes to communicate with each other, but do not want to tightly bind them together to avoid interlocking.

In large systems, many of the code relies on a few key classes. There may be difficulties when you need to change these classes. For example, suppose you have a User class that reads from a file. You want to change it to another class read from the database, but all code references the original class that was read from the file. At this point, it is convenient to use the Factory mode.

A factory pattern is a class that has some methods for creating objects for you. You can use the factory class to create objects without using new directly. This way, if you want to change the type of object you are creating, you can simply change the factory. All code that uses the factory is automatically changed.

Example 1: Displays a sample column for the factory class. The server side of the equation consists of two parts: a database and a set of PHP pages that allow you to add feedback, request a feedback list, and get articles related to specific feedback.

 
 
  1. Interface Iuser
  2. {
  3. function GetName ();
  4. }
  5. class User Implements Iuser
  6. {
  7. Public function __construct ( $id ) {}
  8. Public function getName ()
  9. {
  10. return "Jack";
  11. }
  12. }
  13. class userfactory
  14. {
  15. Public static function Create ( $id )
  16. {
  17. return New User ( $id );
  18. }
  19. }
  20. $uo = userfactory::create (1);
  21. Echo ( $uo->getname (). "\ n" );
  22. ?>

The Iuser interface defines what action the user object should perform. The implementation of Iuser, called the User,userfactory factory class, creates Iuser objects. This relationship can be represented by the UML in Figure 1.


Figure 1. Factory classes and their associated Iuser interfaces and user classes

If you use the PHP interpreter to run this code on the command line, you will get the following result:

 
  
  
  1. % PHP factory1.php
  2. Jack
  3. %

The test code requests the User object from the factory and outputs the result of the GetName method.

There is a factory pattern variant that uses the factory method. These public static methods in the class construct objects of that type. This method is useful if it is important to create objects of this type. For example, suppose you need to create an object first, and then set many properties. This version of the factory pattern encapsulates the process in a single location, so that you don't have to copy complex initialization code or paste the copied code around the code base.

Example 2 shows an example of using a factory method.

 
 
  1. Interface Iuser
  2. {
  3. function GetName ();
  4. }
  5. class User Implements Iuser
  6. {
  7. Public static function Load ( $id )
  8. {
  9. return New User ( $id );
  10. }
  11. Public static function Create ()
  12. {
  13. return New User (null);
  14. }
  15. Public function __construct ( $id ) {}
  16. Public function getName ()
  17. {
  18. return "Jack";
  19. }
  20. }
  21. $uo = User::load (1);
  22. Echo ( $uo->getname (). "\ n" );
  23. ?>

This piece of code is much simpler. It has only one interface Iuser and one User class that implements this interface. The User class has two static methods for creating objects. This relationship can be represented by the UML in Figure 2.


Figure 2. Iuser interface and user class with factory method

Running the script on the command line produces the same result as in Listing 1, as follows:

 
  
  
  1. % PHP factory2.php
  2. Jack

As mentioned above, sometimes such patterns seem to be overqualified in small environments. However, it is best to learn this solid coding format for use in projects of any size.

    1. The proxy mode of PHP design pattern
    2. The model of responsibility chain of PHP design pattern ramble
    3. The structure pattern of PHP design pattern ramble
    4. A random mode of command in PHP design mode

http://www.bkjia.com/PHPjc/587969.html www.bkjia.com true http://www.bkjia.com/PHPjc/587969.html techarticle in the article "Do You know PHP design Patterns", we have briefly introduced the factory model, and today we will look at the application scenario of the factory pattern in PHP development in detail. Learn more about ...

  • 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.