欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP基于工厂模式实现的计算器实例

程序员文章站 2022-09-01 22:38:05
本文实例讲述了php基于工厂模式实现的计算器。分享给大家供大家参考。具体如下: abstract class calculator { private...

本文实例讲述了php基于工厂模式实现的计算器。分享给大家供大家参考。具体如下:

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;
   }
  }
}

希望本文所述对大家的php程序设计有所帮助。