doumianfeng6979 2018-02-19 16:05
浏览 33

在不使用它的情况下获得相同结果时,设计模式Observer的效用是什么?

I'm aware of the use of the pattern observer but for the moment I'm seeing that it's a useless layer to do operations on observer objets while you can do the same operation without the need to use this design pattern !

Can somebody correct me if I'm wrong and explain to be using a concret example the utility and the importance of this design pattern ?

I can see that it offers more code organisation but when should I use it ?

For example here's an implementation of the Observer pattern in PHP :

Here's the observed or the subject class

  // Dès que cet attribut changera on notifiera les classes observatrices.
protected $name;

public function attach(SplObserver $observer)
{$this->observers[] = $observer;
  }

  public function detach(SplObserver $observer)
  {
    if (is_int($key = array_search($observer, $this->observers, true)))
   {
  unset($this->observers[$key]);
    }
  }

  public function notify()
  {
     foreach ($this->observers as $observer)
    {
         $observer->update($this);
    }
  }

  public function getName()
  {
    return $this->name;
}

  public function setNom($name)
  {
    $this->name = $name;
    $this->notify();
  }
}

Here are the observers containing the actions made when they're notified :

<?php
class Observer1 implements SplObserver
{
  public function update(SplSubject $obj)
  {
    echo 'Observer1 has been notified! Nouvelle valeur de l\'attribut <strong>nom</strong> : ', $obj->getName();
  }
}

 class Observer2 implements SplObserver
{
  public function update(SplSubject $obj)
  {
    echo 'Observer2 a has been notified! Nouvelle valeur de l\'attribut <strong>nom</strong> : ', $obj->getName();
  }
}

And here's how we use them

$o = new Observee;

$o->attach(new Observer1); // Adding an observer.

$o->attach(new Observer2); // Adding another observer.

$o->setNom('Victor'); // We mmofidy this attribute and the observers have taken the speficified action

Now as you can see I don't need to implement the two functions notify() and update() to make the observers take some action.

I don't see which problem does this Design Pattern responds to .

  • 写回答

1条回答 默认 最新

  • duanli9930 2018-02-20 15:59
    关注

    The Observer pattern's Intent is as follows:

    Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

    It's not so explicit that the problem is one of maintainability, but part of the reason for Observer is to decouple subjects from its observers. This is important in layered systems where you don't want stable classes depending on unstable classes (dependency in the wrong direction).

    The classic use for Observer is model-view separation. The View (GUI) tends to be unstable. That is, it require lots of changes to the code over time, because:

    • the human users are never happy with the design,
    • there are newer technologies that improve the "view" such as video recognition, voice recognition (Siri, OK Google, etc.), haptics, 3D goggles, etc.
    • there's a need for implementations of a "view" on different platforms, mobile, etc.

    The Model code in these systems doesn't want to know (be coupled to) all these details, since you'll likely have to change the Model code, too, when your users ask you to change the View. That makes your system harder to maintain.

    enter image description here

    So, Observer works by providing to the Model a very stable interface, called Observer which won't change, even though the software that implements it (the views) can change a lot. I like to think of the Observer interface as a friendly-looking (stable) mask behind which all the crazy ever-changing views hide behind. It keeps the Model classes happy by insulating them from the changes.

    enter image description here

    Yes, it's an additional layer, but it's a stable layer to the model that still allows updates to be communicated to views (Observers) despite their changing.

    TL;DR The Observer interface protects the Subject classes from any changes in the classes implementing Observer. This makes your code easier to maintain.

    The best way to appreciate Observer is not to use it. You can live with fixing your models (Subjects in the Observer pattern) each time the views (Observers) change. If you feel the pain of those changes each time, you may appreciate the pattern. This is IMO one of the biggest challenges to learning the GoF patterns (the maintenance suffering required to truly appreciate their benefit).

    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析