Simple Factory mode of PHP design mode

Source: Internet
Author: User

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

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.