API Development Second: PHP design mode of the factory model

Source: Internet
Author: User

used to write code old think, make so many disorderly seven or eight Groove design mode Why, this is not their own to find sin to suffer. Now in this API development process only know the design pattern of the fierce, really is not the realm, never understand it. Fortunately adhere to the code so long, and finally into the design of the use of patterns, is a progress. OK, nonsense not much to say, into today's theme, PHP (object-oriented) basic model has three: Factory mode, singleton mode, registration mode . Today I want to tell the factory model.

The role of the factory model: Do not know why use, like a person deep Chivan gold, but do not know that gold can let him extremely wealthy the same. Factory model, as the name implies is a factory, this factory is the production of objects of this product. Before the programmer needs objects, are new one, now technology developed, need to object, we plant to build one. Was new not good before? Why go to the factory, direct their own manual production more convenient, where to need new, through the factory also through an intermediary, good trouble. Yes, it is this seemingly troublesome process is its role, think, if you have a class, in your project has 50+ a place to use, OK, you need to new50 times, yes, there are children boots will say, you use factory method or call 50 times ah, yes, but now think about it, I this class, the construction method transforms, need to initialize a parameter more, or less initialize a parameter, if you are direct new, congratulate you, you will change 50+ times, and I, only need to change this factory's production process, namely only modifies one place .

Now understand, this is why some people work overtime to bitter force, or one months only four thousand or five thousand, some people live a little Sasa, a monthly salary of tens of thousands, the revolution has not been successful, we strive together.


Factory mode Simple code:

Classes that need to be produced by the factory:

Class Database {public    function where ($str) {        echo $str. ' <br/> ';                return $this;    }}
Factory class:

Class Factory {public        static function CreateInstance () {        $instance = new Database ();                return $instance;    }}

External calls:

$db = Factory::createinstance (); $db->where (' where ')->order (' Order ');

API Development Second: PHP design mode of the factory model

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.