What is the PHP Factory mode? Why use PHP Factory mode?

Source: Internet
Author: User
Believe that a lot of small partners to see the title is the same question, in our development in PHP many models, today we will introduce you to one of the PHP Factory mode, then we first understand what is the PHP Factory mode, why use PHP Factory mode?

  So what is the factory model?

Judging from the name, there seems to be no clue. Factory mode, and production-related? Or is it related to the production process? Does it have to do with the factory leadership? Is it related to the leading secretary? Secretary... Well, not suspense, the so-called factory model is really related to production. What does it produce? The production is an instance object . What equipment is produced? produced through a factory class . How to produce it? The factory class calls its own static method to produce an object instance .

 The factory model has a key construct, named Factory static method according to general principle, however this is only a principle, although the factory method can be arbitrarily named this static can also accept arbitrary data parameters, must return an object.

  Why use Factory mode?

Many people who have not contacted the factory model can not help asking, why do I have to take so much effort to construct a factory class to create objects? We can consider such a simple question without applying those easy-to-maintain, extensible, and so on. If you are in a project, we create an object from a class. At the time of completion or completion, to extend the function, it is found that the original class name is not appropriate or that the class needs to add constructor parameters to implement the function extension. Holy shit! I've created a whole bunch of object instances through this class. Oh, I'm going to have to change one more? We now feel the "high cohesion and low coupling" of the profound. No problem, the factory method can solve this problem.

Think again, I want to connect the database, in PHP there are several methods, MySQL extension, mysqli extension, PDO extension. I just want an object to be used in the future operation, depending on which, depending on the situation. Since you are all connected to the database, you should have the same functionality, make connections, query, disconnect ... (The importance of the interface is shown here.) In a word, these methods should be "united and consistent". How to achieve it? Take advantage of Factory mode.

  How is the factory model implemented?

With respect to the singleton pattern, we have provided enough information, factory class, and static methods inside the factory class. In the static method, the object instance that needs to be created in new is done. Of course, considering the second question above, according to the parameters of static method of factory class, we can make a simple judgment. Tube you with If. else.. or switch. Case: It's good to be able to quickly and efficiently decide which class to create. Finally, be sure to remember that the factory class static method returns an object. Not two, not three.

  Basic Factory class :

Class to create an instance of object class myobject{}//factory class Myfactory{public static function factory () {return new MyObject ():   }} $ Instance=myfactory::factory ();

  A slightly more complex factory pattern:

<?phpinterface transport{public    function Go (); Class Bus implements transport{public    function Go () {        echo Bus stops at every station;    }} Class Car implements transport{public    function Go () {        echo "Car runs Fast";    }} Class Bike implements transport{public    function Go () {        echo "Bike slower";}    } Class transfactory{public    static function factory ($transport)    {                switch ($transport) {case                    ' bus ' :                        return new Bus ();                        break;                    Case ' car ':                        return new car ();                        break;                    Case ' bike ':                        return new bike ();                        Break;}}}    $transport =transfactory::factory (' car '); $transport->go ();

When you need a factory static method for factory (), don't be silly to name the factory class factory. Why, Why? Don't forget the same name constructor thing.

Finally, a little bit of feeling, a lot of novice compared above his business, just will be if. Else..,session,cookie is going to be a little taller. Talk with people easily extensibility, maintainability and so on, as for instance, it will be a temporary speechless. Sometimes feel, regardless of their own code or to learn from others, are in "the crowd to find him thousands of degrees," the real practical study, suddenly look back, "the person is in the lights dim place," The big Call: "The original TM is * * * AH."

Summarize:

After reading this article I believe that you have a general understanding of the PHP factory model, and hope that your work has helped!

Related recommendations:

Three forms of sample code in PHP Factory mode

Summary of usage examples of PHP Factory mode

PHP Factory mode PHP design mode iterator mode

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.