PHP Implements Factory mode

Source: Internet
Author: User

Design Patterns-using PHP to implement factory method patterns

Summary
Create pattern
Defines an interface for creating objects, letting subclasses decide which class to instantiate. Factory method uses the instantiation of a class to defer to its subclass "GOF95"

"Structure Diagram"

"Primary Role"
Abstract Product Role: A parent class or interface that a detailed product object has in common
Detailed products (concrete product) role: implements the interfaces defined by the abstract product role, and each object created by the factory method pattern is an instance of a detailed Product object
Abstract Factory (Creator) Role: Any factory class that creates objects in a pattern implements this interface, which declares a factory method that returns an object of the product type.
Creator can also define the default implementation of a factory method, which returns a default Concreteproduct object
Detailed factory (concrete Creator) role: Implements an abstract factory interface, with detailed factory roles associated with the application logic, called directly by the application to create product objects.
Advantages and Disadvantages
Advantage: The factory method model allows the system to introduce new products without altering the role of the plant.
Cons: Customers may have to create a creator subclass just to create a specific Concreteproduct object
Applicability
1, when a class does not know the class of the object it must create
2. When a class wants to specify the object it creates by its subclasses
3. When a class entrusts the responsibility of creating an object to one of several helper subclasses, and you want to use which helper subclass is the agent this information is localized

"Factory mode PHP instance"

<?php/** * Factory Method Mode *-------------* @author Zhaoxuejie <[email protected]> * @package design pattern  * @vers Ion v1.0 2011-12-14 *///Abstract Product interface Work {public function doWork ();} Detailed product implementation Class Student implements work {function doWork () {echo "Students do homework! \ n ";}} Class Teacher implements work {function doWork () {echo teacher corrects the assignment! \ n ";}} Abstract Factory interface Workerfactory {public function getworker ();} Detailed abstract factory Implementation class Studentfactory {function Getworker () {return new Student ();}} Class Teacherfactory {function Getworker () {return new Teacher ();}} Clientclass Client {static function main () {$s = new Student (); $s->dowork (); $t = new Teacher (); $t->dowork ();}} Client::main ();? >

"Simple Factory mode"
From the type of design pattern, the simple factory model belongs to the creation mode, also called the Static Factory method (Staticfactory) mode, but is not one of the 23 gof design patterns. A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most useful pattern in the factory model family, and can be understood as a special implementation of different factory patterns.
"Simple Factory mode PHP instance"

<?php/** * Simple Factory mode *-------------* @author Zhaoxuejie <[email protected]> * @package design pattern * @ve Rsion v1.0 2011-12-14 */interface comput {public Function getresults ();} Operation class Operation {protected $Number _a = 0;protected $Number _b = 0;protected $Result = 0;//Assignment function Setnumber ($Num Ber_a, $Number _b) {$this->number_a = $Number _a; $this->number_b = $Number _b;} Clear 0 Function Clearresult () {$this->result = 0;}} Addition class Operationadd extends operation implements Comput {function GetResults () {return $this->result = ($this Number_a + $this->number_b);}} Subtraction class Operationsub extends operation implements Comput {function GetResults () {return $this->result = ($this number_a-$this->number_b);}} Multiplication class Operationmul extends operation implements Comput {function GetResults () {return $this->result = ($this Number_a * $this->number_b);}} Division class Operationdiv extends operation implements Comput {function GetResults () {if (Intval ($thiS->number_b) = = 0) {return $this->result = ' error:division by zero ';} return $this->result = ($this->number_a/$this->number_b);}} Factory class Operationfactory {private static $obj;p ublic static function CreateOperation ($type) {try {$error = "please INP   UT the ' + ', '-', ' * ', '/' symbols of Math. '; Switch ($type) {case ' + ': self:: $obj = new Operationadd (); Break;case '-': self:: $obj = new Operationsub (); Break;case ' * ': SE LF:: $obj = new Operationmul (); Break;case '/': self:: $obj = new Operationdiv (); Break;default:throw new Exception ($error);} Return self:: $obj;} catch (Exception $e) {echo ' Caught Exception: ', $e->getmessage (), "\ n"; exit;}}} Factory created instance $obj = Operationfactory::createoperation (' * '); $obj->setnumber (3, 4); Echo $obj->getresults (); >


PHP Implements Factory mode

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.