The object-oriented development of PHP--Factory mode

Source: Internet
Author: User
Factory mode in layman's terms, create a class family for some classes that have the same structure, and provide only one entry class for the group. Notice the description of "same structure", which refers to something that has some characteristics that allow us to differentiate ourselves from other objects, for example, now there is a Lenovo question, which is associative by two words. "Four wheels, burning petrol". I think you can immediately associate with cars, vans, taxis and other vehicles, do not associate with motorcycles. The "Four Wheels" and "burning petrol" here are the same characteristics of cars, vans and taxis, and we classify cars, vans and taxis as "automobiles".

Back in PHP, the factory model is illustrated with an example.

Now there is a car factory that manufactures cars and buses, and cars and buses are made up of engines, bodies and wheels.

Analysis:

1, the production of a variety of cars have common standards, must produce engines, bodies and wheels, so we can create an interface class for the production of automobiles, all kinds of cars must follow the standard in this interface class.

2. The outside world needs to provide the entrance for the production of cars and buses from the factory.

The standard interface carnorms{function engine for the production of automobiles;//Engine function body ();//Body function Whell ()              ;//wheel}//Production car class car implements carnorms{Public function engine () {          Return ' car engine ';          } Public Function Body () {return ' car body ';          } public Function Whell () {return ' car wheel ';              }}//Production bus class bus implements carnorms{Public Function engine () {          Return ' bus engine ';          } Public Function Body () {return ' bus body ';          } public Function Whell () {return ' bus wheel ';              }}//Auto factory class carfactory{static public function carinstance ($type) {              $instance = "; Switch ($type) {case ' car ': $instance =new CAR ();                  Break                      Case ' bus ': $instance =new bus ();                  Break              Default:throw New Exception (' The factory cannot produce this type of vehicle ');              } if (! $instance instanceof carnorms) {throw new Exception (' This vehicle does not meet production standards ');          } return $instance;      }} $createCar =carfactory::carinstance (' car ');      $car [' Engine ']= $createCar->engine ();      $car [' Body ']= $createCar->body ();            $car [' Whell ']= $createCar->whell (); Print_r ($car);

This is a standard factory class, and now new requirements are coming, and factories need to produce trucks.

Analysis: As the production trucks also meet the factory's production standards, we can directly add the truck class, and add the corresponding entrance in the factory class.

Class Truck implements carnorms{public                    function engine () {              return ' truck engine ';          }                    Public Function Body () {              return ' truck body ';          }                    Public Function Whell () {              return ' truck wheel ';          }                }
Auto Factory        class carfactory{                        static public function carinstance ($type) {                $instance = ';                Switch ($type) {case                    ' car ':                        $instance =new car ();                        break;                    Case ' bus ':                        $instance =new bus ();                        break;                    Case ' truck ':                        $instance =new truck ();                        break;                    Default:                        throw new Exception (' The factory cannot produce this type of car ');                }                if (! $instance instanceof carnorms) {                    throw new Exception (' This vehicle does not meet production standards ');                }                return $instance;            }                    }

It's easy to do, but we're assuming that if the production standard changes, the production process also needs to include quality testing, and we have to complete this change by adding a quality test method to the interface.

The standard      interface carnorms{          function engine ()//engine          function body ();//Body          function Whell ();//Wheel          function Check ();//Quality Test      }

The trouble is that all the cars in the factory must be added to the quality inspection method, in which only three kinds of cars are produced, which does not seem to be very troublesome. But let's think again, if a factory has thousands of products, adding a standard to each product will be added to this standard, how much cost overhead! This is the shortcomings of the factory method, the use of Factory mode must pay attention!

The above is the object-oriented development of PHP-Factory mode content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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