Big talk design mode the first chapter---calculator simple Factory mode PHP implementation

Source: Internet
Author: User

1<?PHP2 Abstract classOperation {3     Private $_numbera= 0;4     Private $_numberb= 0;5 6      Public function__get ($number) {7         if(isset($this-$number)) {8             return $this-$number;9}Else {Ten             Echo"The number name is ' _numbera ' and ' _numberb '"; One             Exit(); A         } -     } -  the      Public function__set ($number,$value) { -         if(isset($this-$number)) { -             $this-$number= (float)$value; -}Else { +             Echo"The number name is ' _numbera ' and ' _numberb '"; -             Exit(); +         } A     } at  -     Abstract functionGet_result (); - } -  -  - classOperation_addextendsOperation { in      Public functionGet_result () { -         $result= 0; to         $result=$this->_numbera +$this-_numberb; +         return $result; -     } the } *  $ classOperation_subextendsOperation {Panax Notoginseng      Public functionGet_result () { -         $result= 0; the         $result=$this->_numvera-$this-_numverb; +         return $result; A     } the } +  - classOperation_mulextendsOperation { $      Public functionGet_result () { $         $result= 0; -         $result=$this->_numbera *$this-_numberb; -         return $result; the     } - }Wuyi  the classOperation_divextendsOperation { -      Public functionGet_result () { Wu         $result= 0; -         if($this->_numberb = = 0) { About             Throw New Exception(' ACI yukata 暟 Juan Trajectory divisions Juan? ')); $         } -         $result=$this->_numbera/$this-_numberb; -         return $result; -     } A } +  the  - classOperationfactory { $      Public Static functionCreate_operate ($operate) { the         $oper=NULL; the         Switch($operate) { the          Case' + ': the             $oper=NewOperation_add (); -              Break; in          Case‘-‘: the             $oper=Newoperation_sub (); the              Break; About          Case‘*‘: the             $oper=NewOperation_mul (); the              Break; the          Case‘/‘: +             $oper=NewOperation_div (); -              Break; the         }Bayi         return $oper; the     } the } -  -  the $oper= Operationfactory::create_operate ('/')); the $oper->_numbera = 10; the $oper->_numberb = 20; the Echo $oper->get_result ();

Maintainable, reusable, scalable, flexible

Association: When a class ' knows ' another class, it can be associated (association)

Aggregation: Represents a weak ' owning ' relationship, which reflects that a object can contain a B object, but the B object is not part of a object.

Combination: is in a strong ownership relationship, embodies the strict part and the whole relationship, part and the whole life cycle.

Big talk design mode the first chapter---calculator simple Factory mode PHP implementation

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.