2011-09-27 88 views
1

我在php中創建了一個oop系統,並且希望實現更多的觀察者模式,因爲我希望減少我的類之間的重度耦合。觀察員設計模式問題

我的問題是這樣的。 關於這種模式設計的最佳實踐是可以的,一個類添加一個觀察者到另一個類,該類正在使用。 或者我應該讓觀察者加入鏈的最頂層?

例如:(假設其他的方法調用,但不包含在類中,的確存在,但不是在這個例子中很重要的。)

class orderItem extends observable { 
    public function pick($qty, $user){ 
      $this->setUser($user); 
      $position = new position($this->getPositionID()); 
      $position->addObserver(new ProductObserver()); // is this the best option ? ? 
      $position->setQty($position->getQty() - $qty); 
      $position->save(); 
      $this->notify(self::EVENT_PICK); // notify observers 
    } 
} 

class orderProductObserver implements observer { 
    public function update($orderitem){ 
      $position = new position($orderitem->getPositionID()); 
      $product = new product($position->getProductID()); 
      if($product->getQty() < $product->getMinimum()) { 
       $alert = new minProductAlert($product); 
      } 
    } 
} 

class ProductObserver implements observer { 
    public function update($position){ 
      $product = new product($position->getProductID()); 
      if($product->getQty() < $product->getMinimum()) { 
       $alert = new minProductAlert($product); 
      } 
    } 
} 

$order = new orderItem(123); 
$order->addObserver(new orderProductObserver()); // or is this the best option ?? 
$order->pick(2, 'bill'); 

或者相反,如果這兩種方法都錯了我在你的輸入很感興趣。

這個例子是否會通過消除orderitem和position之間的依賴關係變得最理想?

class OrderItem extends Observable { 
     public function pick($qty, $user){ 
       $this->setUser($user); 
       $this->setPickedQty($qty); 
       $this->save(); 
       $this->notify(self::EVENT_PICK); // notify observers 
     } 
    } 

    class OrderItemPickObserver implements Observer { 
     public function update($orderitem){ 
       $position = new Position($orderitem->getPositionID()); 
       $position->addObserver(new ProductPositionObserver()); 
       $position->setQty($position->getQty() - $orderItem->getPickedQty()); 
       $position->save(); 
     } 
    } 

    class ProductPositionObserver implements Observer { 
     public function update($position){ 
       $product = new product($position->getProductID()); 
       if($product->getQty() < $product->getMinimum()) { 
        $alert = new minProductAlert($product); 
       } 
     } 
    } 
    $pickQty = 2; 
    $orderitem = new OrderItem(123); 
    $position = new Position($orderitem->getPositionID()); 
    if($position->getQty() >= $pickQty) 
    { 
      $orderitem->addObserver(new OrderItemPickObserver()); // or is this the best option ?? 
      $orderitem->pick($pickQty, 'bill'); 
    } 

回答

0

第二個例子看起來不錯,但我不確定是否在OrderItemPickObserver類的更新方法中創建了新的Position對象。相反,我建議將Position對象保留爲OrderItem類的屬性,以便您可以從外部對其進行設置。

class OrderItem extends Observable { 
     private $_position; 
     public function setPosition($position){ 
       $this->_position = $position; 
     } 
     public function getPosition(){ 
       return $this->_position; 
     } 
    } 

然後更新OrderItemPickObserver類:

class OrderItemPickObserver implements Observer { 
     public function update($orderitem){ 
       $position = $orderitem->getPosition()); 
       $position->setQty($position->getQty() - $orderItem->getPickedQty()); 
       $position->save(); 
     } 
    } 

而且調用代碼:

$orderitem = new OrderItem(123);  
$position = new Position(); 
$position->addObserver(new ProductPositionObserver()); 
$orderitem->setPosition($position); 

這樣你可以斷開OrderItemPickObserverPosition類。

編輯:

如果你的業務邏輯不允許你有一個Position對象OrderItem類,你可以在同一移動OrderItemPickObserver,因爲這是實際使用Position對象的類。

class OrderItemPickObserver implements Observer { 
      private $_position; 
      function __construct($position){ 
        $this->_position = $position; 
      } 

      public function update($orderitem){ 
        $position = $this->_position; 
        $position->setId($orderitem->getPositionID()); 
        $position->setQty($position->getQty() - $orderItem->getPickedQty()); 
        $position->save(); 
      } 
     } 

而且調用代碼:

$orderitem = new OrderItem(123);  
$position = new Position(); 
$position->addObserver(new ProductPositionObserver()); 
... 
... 
$orderitem->addObserver(new OrderItemPickObserver($position)); 
+0

我並未之前提及但OrderItem的表示下列MySQL錶行 列包括 positionID:參考位置正在編輯 數量 qtypicked 用戶 當其構造的對象它抓住MySQL的行數據和存在作爲(用於驗證和記錄特定的方法) 我將如何添加從類外的位置刪除依賴性,但仍然保持鏈接的業務邏輯? – user966936

+0

ha ..或者我可以將對象的創建與注入所需對象的工廠類分離。 – user966936

+0

或者,如果適合,可以在EDIT中使用該方法。 – Vikk