首页 \ 问答 \ 如何在PHP中实现完整的Observer模式(How to implement a full Observer pattern in PHP)

如何在PHP中实现完整的Observer模式(How to implement a full Observer pattern in PHP)

观察者设计模式是松散耦合对象的解决方案,因此它们可以一起工作。 在PHP中,只需使用两个类即可轻松实现。

基本上,你有一个能够通知和更新观察者状态变化列表的主题。

我试图解决的问题是知道如何处理提醒观察者关于他们正在观察的对象的不同状态。

例如,假设我们有一个文件上传类,我们附加了一个日志类,websockets类和一个图像大小调整类。 每个正在观看的课程都希望了解上传过程中的不同事件。

这个文件上传类可能有三个地方需要通知监听某些事件发生的类。

  • 上传时出错(警报记录类)
  • 上传成功(提醒websockets类)
  • 上传成功并且是图像文件(警报图像调整大小类)

这是一个非常基本的例子,但是你如何处理不同观察者可能需要知道的多个事件? 仅仅调用notifyObservers()是不够的,因为每个观察者都需要知道它被通知了什么。

一个想法是,我可以用电话说明正在观察什么类型的事件:

$this->notifyObservers('upload.error', this);

但是,这意味着我将不得不向观察者自己添加自定义切换,以了解如何处理不同的事件。

function observe($type, $object)
{
    if($type === 'upload.error') $this->dosomething();
    elseif($type === 'something.else') $this->otherthing();
    ...etc...
}

我发现这非常丑陋,因为它开始将观察者与他们正在观察的班级相连。

然后,如果我只是通知观察者而不传递任何关于发生什么事件的信息 - 他们必须猜测自己发生了什么,这意味着如果()检查更多。


An Observer Design Pattern is the solution to loosely coupling objects so they can work together. In PHP you can easily implement this using just two classes.

Basically, you have a subject which is able to notify and update a list of observers of its state changes.

The problem I'm trying to solve is to know how to handler alerting the observers about different states of the object they are watching.

For example, lets say we have a file upload class to which we attach a logging class, websockets class, and a image resize class. Each of these classes that are watching want to know about different events in the upload process.

This file upload class might have three places where it needs to notify the classes listening that something has happend.

  • Error With Upload (alert logging class)
  • Upload success (alert websockets class)
  • Upload success and is image file (alert image resize class)

This is a very basic example, but how do you handle multiple events that different observers may need to know about? Calling notifyObservers() alone wouldn't be enough since each observer needs to know what it is being notified about.

One thought is that I could state with the call what type of event is being observed:

$this->notifyObservers('upload.error', this);

However, that would mean I would have to add custom switching to the observers themselves to know how to handle different events.

function observe($type, $object)
{
    if($type === 'upload.error') $this->dosomething();
    elseif($type === 'something.else') $this->otherthing();
    ...etc...
}

I find that very ugly as it starts to couple the observers back to the class they are observing.

Then again, if I just notify Observers without passing any information about what event just happens - they have to guess themselves what is going on which means more if() checks.


原文:https://stackoverflow.com/questions/6374294
更新时间:2022-06-10 12:06

最满意答案

在PyCharm中添加远程解释器时,必须设置正确的python二进制文件的路径:

在此处输入图像描述

写那里你的anaconda安装的路径。 因此,在您的情况下,Python解释器路径类似于:/ your_install_path / anaconda / bin / python3


You have to set up the path to the correct python binaries, when you add the remote interpreter in PyCharm:

enter image description here

Write there the path to your anaconda installation. So in your case the Python interpreter path is something like: /your_install_path/anaconda/bin/python3

相关问答

更多

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)