Factory mode of php pattern design, php pattern design factory

Source: Internet
Author: User

Factory mode of php pattern design, php pattern design factory

Take on the singleton mode of php Model Design in the previous article (although it seems that the relationship is not big ). Today we will talk about the second basic model design-the factory model.

  So what is the factory model?

From the perspective of name, it seems that there is no clue. What is the factory model related to production? Or is it related to the production process? Is it still related to factory leaders? Is it related to the lead secretary? The secretary... I don't want to sell my customs. The so-called factory model is really related to production. What is production? What is produced isAn Instance Object. Through which devices are produced? PassOne factory production. How can this problem be produced?Factory class calls its own static method to produce object instances.

The Factory mode has a key structure, which is named as the static method of Factory according to general principles. However, this is only a principle, although the factory method can be named arbitrarily, this static method can accept any data parameter, and an object must be returned.

  Why use the factory model?

A lot of people who have never touched the factory model can't help asking, why do I have to spend so much time constructing a factory class to create objects? We can consider such a simple problem without applying such features as ease of maintenance and scalability. In the project, we create an object through a class. If you want to extend the function, you can find that the original class name is not very suitable or that you need to add constructor parameters to the class to implement function extension. Rely on me! I have created a lot of object instances through this class. Can't I change them one by one? We now feel the profoundness of "high cohesion and low coupling. No problem. The factory method can solve this problem.

I want to connect to the database. There are several methods in php: mysql extension, mysqli extension, and PDO extension. I just want to use an object for future operations. The specific operation depends on the actual situation. Since you are connecting to the database, you should have the same functions, establish connections, query, and disconnect... (the importance of the interface is shown here ). All in all, these methods should be "united and consistent ". How to implement it? Use the factory model.

  How to Implement the factory model?

Compared with the singleton mode, the above provides sufficient information, static methods in the factory class and factory class. In the static method, the new object instance needs to be created. Of course, considering the second problem above, we can make a simple judgment based on the parameters of the factory-class static method. You can use if... else... or switch... case... to quickly and efficiently determine which class to create. Finally, remember that the factory class static method returns an object. Not two, not three.

  Basic factory type:

// Class MyObject {}// factory class MyFactory {public static function factory () {return new MyObject () :}}$ instance = MyFactory :: factory ();

  A slightly complicated factory model:

<? Phpinterface Transport {public function go ();} class Bus implements Transport {public function go () {echo "bus stops every station ";}} class Car implements Transport {public function go () {echo "car running fast" ;}} class Bike implements Transport {public function go () {echo "bike is slow" ;}} 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 the factory static method is required to be Factory (), do not name the factory class as Factory. Why? Don't forget about the same name constructor ~

  

Finally, let's talk about how it feels. Many new users are very eager to get started. Now, if... else..., session, and cookie are coming soon. Talking to people is prone to cloud Clouds such as scalability and maintainability. When it comes to instances, it will get stuck. Sometimes I feel that no matter whether I write code or learn from others, I am in the "Searching for him in the crowd". After studying it practically, I suddenly look back, "the man is in the dark ", shout: "This TM is ".

I dare not admit that I can design my own models. I am also a beginner in less than a year. I just want to record my learning history by sharing my blog, and I am not sure I can understand it. It would be better if it could help others ~~~

 

 

Series of articles:

Php Pattern Design-Singleton pattern

Factory model of php Pattern Design

Php Pattern Design-registration tree pattern

 

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.