As the object's creation mode, the factory method is used instead of the new operation.
The simple factory model belongs to the creation mode, also called the Static factory method mode, but does not belong to one of the 23 gof design patterns. A simple factory model is a factory object that determines which product class instances are created.
<?php/* * Factory class, which contains the factory method, in place of the new operation, is determined by the parameters which object to create */class operator{public $a, $b, $oper;p ublic function __construct ($a, $b, $oper) {$this->a = $a; $this->b = $b; $this->oper = $oper;} Public Function GetResult () {switch ($this->oper) {Case 1: $model = new Add ($this->a, $this->b); Break;case 2: $ Model = new Jian ($this->a, $this->b) break;case 3: $model = new Cheng ($this->a, $this->b); Break;case 4: $model = new Chu ($this->a, $this->b); return $model->result ();}} /* Abstract class, its subclasses must implement the arithmetic method */abstract class Poper{public $a, $b;p ublic function __construct ($a, $b) {$this->a = $a; $this->b = $b;} Abstract function result ();} Subclass, responsible for specific business implementation class Add extends Poper{public function result () {return $this->a+ $this->b;}} Class Jian extends Poper{public function result () {return $this->a-$this->b;}} Class Cheng extends Poper{public function result () {return $this->a* $this->b;}} Class Chu extends Poper{public function result () {if ($this->b ==0) {return ' divisor cannot be 0 ';} Return$this->a/$this->b;}}? >
Simple Factory mode of PHP design mode