首页 >php教程 >PHP源码 >PHP计算器(工厂模式)

PHP计算器(工厂模式)

PHP中文网
PHP中文网原创
2016-05-25 17:13:521194浏览

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
			{
				$p=new Div();
				return $p;
			}
		}
	}

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn