PHP calculator instance based on factory mode, php factory calculator instance

Source: Internet
Author: User

PHP calculator instance based on factory mode, php factory calculator instance

The example in this article describes the calculator implemented by PHP Based on the factory mode. Share it with you for your reference. The details are as follows:

abstract class Calculator{ private $number1; private $number2; public $result; /**  * @return the $number2  */ public function getNumber2() {  return $this->number2; } /**  * @param field_type $number2  */ public function setNumber2($number2) {  $this->number2 = $number2; } /**  * @return the $number1  */ public function getNumber1() {  return $this->number1; } /**  * @param field_type $number1  */ public function setNumber1($number1) {  $this->number1 = $number1; } abstract function get_result(); }class Add extends Calculator{  public function get_result($number1,$number2)  {   return $number1+$number2;  }}class Sub extends Calculator{  public function get_result($number1,$number2)  {   return $number1-$number2;  }}class Mul extends Calculator{  public function get_result($number1,$number2)  {   return $number1*$number2;  }}class Div extends Calculator{  public function get_result($number1,$number2)  {   return $number1/$number2;  }}class Factory{  public function Building($notes)  {   if($notes=="+")   {    $add=new Add();    return $add;   }   elseif ($notes=="-")   {    $sub=new Sub();    return $sub;   }   elseif($notes=="*")   {    $mul=new Mul();    return $mul;   }   else   {    $div=new Div();    return $div;   }  }}

I hope this article will help you with php programming.

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.