The factory pattern PHP design patterns

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

factory pattern: by the factory according to the parameter to decide what kind of products to create an instance of the class

factory class: a method is used exclusively to create other objects of the class. The distribution according to need, the incoming parameters, return to a concrete class

The role of : object creation package, simplifying the creation of object manipulation, i.e. a method invocation factory class to get the required class

Supplement:

The 1 main characters: the abstract product (Product), a specific product (Concrete Product), the abstract factory (Creator)

2 advantages and disadvantages

    Advantages: factory method pattern allows the system to introduce products without modifying the factory case

    Disadvantages: customers may simply to create a specific Concrete Product object, you have to create a subclass of Creator

3 applicability

    When a class does not know about the objects it must create.

    When a class to the class of the object to make it creates.

    When a delegate a class to create the object function to multiple help sub class one, and I hope you will be a help subclass is the agent in this information localization when


<?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 defining attributes and abstract method
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();
}

// operations
class OperationAdd extends 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('+',5,6);
echo $obj-> getResult();
?> 


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Darren at October 27, 2013 - 9:26 PM