Factory model of php design model

Source: Internet
Author: User

Factory model:The factory class determines which type of production slice instance to create Based on the parameter.Factory type:A method class specifically used to create other objects. That is, pay-as-you-go allocation. input parameters are selected to return specific classes.Purpose:Encapsulate object creation and simplify object creation, that is, call a factory class method to obtain the required class.Supplement:1. main roles: Abstract products, specific products, and abstract factory roles. advantages and disadvantages: The Factory method mode allows the system to introduce the Product without modifying the factory role. Disadvantages: the customer may only create a specific Concrete Product object, you have to create a Creator subclass 3. applicability when a class does not know what objects it must create, when a class wants its subclass to specify the objects it creates, when a class delegates the responsibility of creating objects. to one of multiple helper classes, and when you want to localize the information of which helper subclass is the proxy

<? Php // object class MyObject {public function _ construct () {} public function test () {return 'test ';}} // factory class MyFactory {public static function factory () {return new MyObject () ;}$ myObject = MyFactory: factory (); echo $ myObject-> test ();?>

 

<? Php // abstract class definition attributes and abstract METHODS abstract class Operation {protected $ _ NumberA = 0; protected $ _ NumberB = 0; protected $ _ Result = 0; public function _ construct ($ A, $ B) {$ this-> _ NumberA = $ A; $ this-> _ NumberB = $ B ;} public function setNumber ($ A, $ B) {$ this-> _ NumberA = $ A; $ this-> _ NumberB = $ B;} public function clearResult () {$ this-> _ Result = 0;} abstract protected function getResult ();} // operation class OperationAdd exte Nds Operation {public function getResult () {$ this-> _ Result = $ this-> _ NumbserA + $ this-> _ NumberB; return $ this-> _ Result ;}} class OperationSub extends Operation {public function getResult () {$ this-> _ Result = $ this-> _ NumberA-$ this-> _ NumberB; return $ this-> _ Result ;}}............ // Factory class OperationFactory {private static $ obj; public static function CreationOperation ($ type, $ A, $ B) {switch ($ type) {case '+': self:: $ obj = new OperationAdd ($ A, $ B); break; case '-': self: $ obj = new OperationSub ($ A, $ B); break; ...... }}// Operation $ obj = OperationFactory: CreationOperation ('+',); echo $ obj-> getResult ();?>

 

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.