"Simple Factory mode" a simple calculator

Source: Internet
Author: User
Tags mul

For a programmer who knows nothing about design patterns, maintenance costs are a deadly problem, so from now on, I'm going to start working hard, designing patterns, and documenting my learning outcomes.

Production class

    1. Abstract class Oper Abstraction classes
    2. Class Add Plus
    3. Class Sub minus
    4. Class Mul Multiply
    5. Class Div in addition to
    6. Class Operate Calculation

 abstract  class   Oper { public      $num 1  = 0;  public   $num 2  = 0;  abstract  public  function   GetResult ();}  
class extends Oper {    publicfunction  getresult () {        $result$this- >NUM1 +$this-num2;         return $result ;    }}

class extends Oper {    publicfunction  getresult () {        if($this Num2==0) {            throw_exception (' denominator cannot be zero ');        }         $result $this->num1/$this, num2;         return $result ;    }}

classOperate { Public functionOper$operate){        Switch($operate){             Case' + ':return NewAdd ();  Break;  Case‘-‘:return NewSub ();  Break;  Case‘*‘:return NewMul ();  Break;  Case‘/‘:return NewDiv ();  Break; default:Throw_exception (' Symbol not supported ');  Break; }    }}

"Simple Factory mode" a simple calculator

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.