如何在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
最满意答案
在PyCharm中添加远程解释器时,必须设置正确的python二进制文件的路径:
写那里你的anaconda安装的路径。 因此,在您的情况下,Python解释器路径类似于:/ your_install_path / anaconda / bin / python3
相关问答
更多-
pycharm打开设置python[2023-03-12]
看你以前是否装过pycharm。 上面个选项的意思是导入配置文件,所以你要选择你本机的配置文件。 下面个选项,do not import setting。 意思是不导入本机上的配置文件并使用pycharm官方默认的设置(后面可以改的) 因为pycharm可以根据你喜欢的设置来设置,然后把各个设置导出成一个配置文件,然后下次在其他电脑安装时就可以直接导入配置文件,不用手动去改那些设置就可以用你以前喜欢的设置了。 但是第一次安装肯定没有配置文件,或者你不想导入配置文件,就选不导入就可以进入软件了 -
在linux系统中 启动配置网卡的界面用什么命令[2022-02-09]
setup -
Tensorflow和Pycharm(Tensorflow and Pycharm)[2022-02-15]
解决方案是: 从控制台运行PyCharm。 要么 将环境变量添加到IDE设置:LD_LIBRARY_PATH = / usr / local / cuda / lib64:$ LD_LIBRARY_PATH The solution is: Run PyCharm from the console. OR add the environment variable to the IDE settings: LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PA ... -
您的终端环境中的PYTHONPATH不包含'my_module'。 配置PYTHONPATH以包含包含模块的目录 它在pycharm中起作用,因为它为你自动设置路径。 了解模块搜索路径 The PYTHONPATH in your terminal environment doesn't contain 'my_module'. Configure the PYTHONPATH to include the directory containing your module It works in pycha ...
-
在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: Write there the ...
-
“Pip”在命令提示符中识别,但在PyCharm终端中未识别('Pip' recognized in Command Prompt but not in PyCharm terminal)[2022-04-13]
回答我自己的问题:当事情没有意义时,请尝试将其关闭再打开。 问题已通过重启修复。 Answer to my own question: when things don't make sense, try turning it off and on again. Problem was fixed with a restart. -
什么是差异b / w在pycharm和终端运行python代码?(what is difference b/w run python code in pycharm and terminal?)[2022-08-23]
也许这段代码在不同的环境中运行,在Pycharm中你可以通过virtualenv设置单独的环境来运行你的项目 Maybe this code ran in different environments, in Pycharm you can set separate environment by virtualenv for running your project -
如果您使用的是Python 3,那么您应该使用python3 manage.py check 。 使用虚拟环境可能更好,在这种情况下,您可以在运行python manage.py check之前激活虚拟环境。 导入在Python 3 shell中失败,因为您尚未设置DJANGO_SETTINGS_MODULE环境变量(有关详细信息,请参阅文档 )。 最简单的解决方法是使用Django shell,它会为您处理这个问题。 $ python3 manage.py shell >>> import imageki ...
-
当我尝试在IDE之外激活相同的venv时,我遇到了许可问题 这个问题很可能是关于权限访问虚拟环境文件的。 使用ls -al查看访问权限和user:group所有权(如果您使用Mac或Linux),请在此处查看更多信息: https://linux.die.net/man/1/ls https://linux.die.net/man/1/chmod https://linux.die.net/man/1/chown 我认为它与已经在IDE内运行的venv有关 绝对不会,您可以根据需要多次激活它。 我需要运行相 ...
-
Pygame文档建议您使用Python 3.6.1或更高版本 ,所以我建议您使用最新的非beta版本。 另外,这个版本还没有提供一些pygame轮子。 python安装后,请确保它已添加到您的PATH变量中,并尝试使用此命令安装Pygame,因为您在Windows上: py -m pip install -U pygame --user 如果您收到PermissionError,请以管理员身份运行命令提示符。 Pygame docs recommends that you use Python versi ...