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