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 ();?>