标签:
1 <?php 2 abstract class Operation { 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 { 10 echo "the two number name is ‘_numberA‘ and ‘_numberB‘"; 11 exit(); 12 } 13 } 14 15 public function __set($number, $value) { 16 if (isset($this->$number)) { 17 $this->$number = (float) $value; 18 } else { 19 echo "the two number name is ‘_numberA‘ and ‘_numberB‘"; 20 exit(); 21 } 22 } 23 24 abstract function get_result(); 25 } 26 27 28 class Operation_add extends Operation { 29 public function get_result() { 30 $result = 0; 31 $result = $this->_numberA + $this->_numberB; 32 return $result; 33 } 34 } 35 36 class Operation_sub extends Operation { 37 public function get_result() { 38 $result = 0; 39 $result = $this->_numverA - $this->_numverB; 40 return $result; 41 } 42 } 43 44 class Operation_mul extends Operation { 45 public function get_result() { 46 $result = 0; 47 $result = $this->_numberA * $this->_numberB; 48 return $result; 49 } 50 } 51 52 class Operation_div extends Operation { 53 public function get_result() { 54 $result = 0; 55 if ($this->_numberB == 0) { 56 throw new Exception(‘闄ゆ暟涓嶈兘涓?‘); 57 } 58 $result = $this->_numberA / $this->_numberB; 59 return $result; 60 } 61 } 62 63 64 class OperationFactory { 65 public static function create_operate($operate) { 66 $oper = null; 67 switch ($operate) { 68 case ‘+‘: 69 $oper = new Operation_add(); 70 break; 71 case ‘-‘: 72 $oper = new Operation_sub(); 73 break; 74 case ‘*‘: 75 $oper = new Operation_mul(); 76 break; 77 case ‘/‘: 78 $oper = new Operation_div(); 79 break; 80 } 81 return $oper; 82 } 83 } 84 85 86 $oper = OperationFactory::create_operate(‘/‘); 87 $oper->_numberA = 10; 88 $oper->_numberB = 20; 89 echo $oper->get_result();
可维护、可复用、可扩展、灵活
关联: 当一个类‘知道’另一个类的时候,可以用关联(association)
聚合: 表示一种弱的‘拥有’关系,体现的是A对象可以包含B对象,但B对象不是A对象的一部分。
组合:是以一种强的拥有关系,体现了严格的部分和整体的关系,部分和整体的生命周期一样。
标签:
原文地址:http://www.cnblogs.com/wy0314/p/4768109.html