PHP design pattern: Factory pattern

Source: Internet
Author: User
Factory mode: The factory class determines the product type instance to be created based on the parameters. the factory class refers to a class specifically used to create other objects. The so-called pay-as-you-go allocation, input parameters for selection, return a specific class. The main role of the factory mode is to encapsulate object creation and simplify object creation operations. Factory model:

The factory class determines which product class instance to create based on the parameter;

A factory class is a class that contains a method specifically used to create other objects. The so-called pay-as-you-go allocation, input parameters for selection, return a specific class. The main role of the factory mode is to encapsulate object creation and simplify object creation operations.

Simply put, it is to call a method (input parameter) of the factory class to obtain the required class;

Code implementation:

Example 1 (the most basic factory class ):

 Test ();

Example 2:

 _ NumberA = $ A; $ this-> _ NumberB = $ B;} public function setNumber ($ A, $ B) {$ this-> _ NumberA = $; $ this-> _ NumberB = $ B;}/1 * protected function clearResult () {$ this-> _ Result = 0;} */public function clearResult () {$ this-> _ Result = 0;} // when the abstract method has no method body abstract protected function getResult ();} // inherits an abstract class, subclass must implement all abstract methods in the abstract class; // In addition, the visibility of these methods must be the same as (or more loose) in the abstract class OperationAdd extends Operation {public function getResult () {$ this-> _ Result = $ this-> _ NumberA + $ this-> _ NumberB; return $ this-> _ Result ;}} class OperationSub extends Operation {public function getResult () {$ this-> _ Result = $ this-> _ NumberA-$ this-> _ NumberB; return $ this-> _ Result;} class OperationMul extends Operation {public function getResult () {$ this-> _ Result = $ this-> _ NumberA * $ this-> _ NumberB; return $ this-> _ Result ;}} class OperationDiv extends Operation {public function getResult () {$ this-> _ Result = $ this-> _ NumberA/$ this-> _ NumberB; return $ this-> _ Result;} class OperationFactory {// Create a static member variable private static $ obj that saves the instance; // create A public static method for accessing the instance public static function CreateOperation ($ type, $ A, $ B) {switch ($ type) {case '+': self :: $ obj = new OperationAdd ($ A, $ B); break; case '-': self: $ obj = new OperationSub ($ A, $ B); break; case '*': self: $ obj = new OperationMul ($ A, $ B); break; case '/': self: $ obj = new OperationDiv ($, $ B); break;} return self: $ obj;} // $ obj = OperationFactory: CreateOperation ('+ '); // $ obj-> setNumber (4, 4); $ obj = OperationFactory: CreateOperation ('*', 5, 6); echo $ obj-> getResult ();/1 * echo'
'; $ Obj-> clearResult (); echo'
'; Echo $ obj-> _ Result ;*/

Example 3:

 GetFactory ('silver '); // let the Silver factory make a recommended product $ product = $ factory-> create ('commendatory '); // Obtain the recommended product echo $ product-> desc for Silver members.'
'; // Get a Bronze factory $ factory2 = CenterFactory: getFactory2 ('Bronze '); // let the bronze factory manufacture a discounted product $ product2 = $ factory2-> create ('rebate'); // Obtain the recommended product echo $ product2-> desc for Bronze members;

Example 4:

 _ File = $ file; $ this-> _ parse ();} private function _ parse () {// complete parsing in PNG format // fill in $ _ width, $ _ height and $ _ data $ this-> _ data = getimagesize ($ this-> _ file); list ($ this-> _ width, $ this-> _ height) = $ this-> _ data;} public function getWidth () {return $ this-> _ width;} public function getHeight () {return $ this-> _ height ;} public function getData () {return $ this-> _ data;} class Image_JPEG implements IImage {protected $ _ width, $ _ height, $ _ data; public function _ construct ($ file) {$ this-> _ file = $ file; $ this-> _ parse ();} private function _ parse () {// complete parsing in JPEG format // and fill in $ _ width, $ _ height and $ _ data // $ this-> _ width = imagesx ($ this-> _ file ); // $ this-> _ height = imagesy ($ this-> _ file); $ this-> _ data = getimagesize ($ this-> _ file ); list ($ this-> _ width, $ this-> _ height) = $ this-> _ data;} public function getWidth () {return $ this-> _ width ;} public function getHeight () {return $ this-> _ height;} public function getData () {return $ this-> _ data ;}} // factory class ImageFactory {public static function factory ($ file) {$ filename = pathinfo ($ file); switch (strtolower ($ filename ['extension']) {case 'jpg ': $ return = new Image_JPEG ($ file); break; case 'PNG': $ return = new Image_PNG ($ file); break; default: echo 'incorrect Image type'; break;} if ($ return instanceof IImage) {return $ return;} else {echo 'error'; exit ();}}} $ image = ImageFactory: factory ('images/11.jpg '); var_dump ($ image-> getWidth (); echo'
'; Print_r ($ image-> getheight (); echo'
'; Print_r ($ image-> getData ());



For more articles about the PHP design mode: Factory mode, refer to the PHP Chinese network!

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.