首页 \ 问答 \ 观察者设计模式问题(observer design pattern question)

观察者设计模式问题(observer design pattern question)

我正在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');
    }

i am creating a oop system in php and would like to implement more observer patterns into it as i have heavy coupling between my classes that i wish to reduce.

my question is this. in relation to best practice in design for this pattern is it ok for one class to add an observer to another class, that class is working with. or should i keep the observer adding to the top most level of the chain?

example: (assume other methods called, but not included in the class, exist but are not important for this example.)

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');

Or alternatively if both methods are wrong i am very interested in your input.

would this example be the most ideal by removing dependency between orderitem and 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');
    }

原文:https://stackoverflow.com/questions/7568660
更新时间:2022-03-31 19:03

最满意答案

这个问题有很多深入的答案,但我认为的3个主要区别是1.按钮 - 具有文本的按钮的3d渲染。 非常简单易用。 2. ImageButton - 3d渲染按钮但不使用文本而是使用图像。 3. ImageView - 平面图像。

ImageButton和Button具有按下的,选定的,聚焦的,未选择的状态,可以向用户传达按钮的不同状态。

我不相信使用一个vs,另一个有任何性能优势,但肯定有UI和UX使用每个的原因。 ImageView我认为只显示图像会很好。 用户不会真正知道他们可以在图像上c ..但是对于按钮对应的用户被训练他们应该舔他们。


There are a lot of in-depth answers to the question but the 3 main differences in my opinion are 1. button - 3d rendering of a button that has text. Very simple easy to use. 2. ImageButton - 3d rendeirng of a button but instead of text you are using an image. 3. ImageView - a flat image.

ImageButton and Button have the pressed,selected, focused, unselected states that can convey to the user the different states of the button.

I don't believe there is any performance benefits of using one vs, the other but there are definitely UI and UX reasons to use each one. ImageView I would assume would be good to display just the image. Users won't really know that they can cick on the Image.. but with the button counterparts users are trained that they should cick on them.

相关问答

更多
  • 您应该为可能的屏幕尺寸设计布局和图像,并将它们放在各自的文件夹中,如下所示。 res/layout/my_layout.xml // layout for normal screen size ("default") res/layout-small/my_layout.xml // layout for small screen size res/layout-large/my_layout.xml // layout for large screen size res/layout-xla ...
  • 这可能仅仅涵盖了部分差异,实际上看看Android Source树可以准确地看到发生了什么。 ImageButtons具有推送状态,其中可点击的图像不会。 您也无法为ImageButton调用setText,您可以使用常规按钮。 他们都是从观点出发,但是看下面的延伸链可能有所帮助。 java.lang.Object ↳ android.view.View ↳ android.widget.ImageView ↳ android.widget.ImageButton 与 java.lan ...
  • 没有差别,除了默认风格。 ImageButton的默认值为非空值。 编辑 : ImageButton.onSetAlpha()方法总是返回false, scaleType设置为center并且它总是充当可聚焦的。 这是ImageButton的默认样式: