python中的观察者可观察类

2024-06-09 12:24:38 发布

您现在位置:Python中文网/ 问答频道 /正文

如何用Python编写一个带有观察者/可观察设施的程序,就像Java一样? 我会用Java编写如下内容。

import java.util.Observable;
import java.util.Observer;


public class ObservDemo extends Object {
  MyView view;
  MyModel model;

  public ObservDemo() {

    view = new MyView();
    model = new MyModel();
    model.addObserver(view);

  }

  public static void main(String[] av) {
    ObservDemo me = new ObservDemo();
    me.demo();
  }

  public void demo() {
    model.changeSomething();
  }

  /** The Observer normally maintains a view on the data */
  class MyView implements Observer {
    /** For now, we just print the fact that we got notified. */
    public void update(Observable obs, Object x) {
      System.out.println("update(" + obs + "," + x + ");");
    }
  }

  /** The Observable normally maintains the data */
  class MyModel extends Observable {
    public void changeSomething() {
      // Notify observers of change
      setChanged();
      notifyObservers();
    }
  }
}

(此代码取自以下链接 http://www.java2s.com/Code/Java/Design-Pattern/AsimpledemoofObservableandObserver.htm

我如何在Python中完成这样的事情?


Tags: theimportviewnewmodeljavapublicclass
1条回答
网友
1楼 · 发布于 2024-06-09 12:24:38

首先,正如Martijn Pieters所说,Python不是Java。 这意味着您可能不需要整个观察者/观察到的模式,但可以将其简化为一个更简单的版本。最后,我将展示一些pythonesque,但是为了保持非常基本的java实现,您可以尝试以下方法:

class Observer(object):
    def notify(self,*args,**kwargs):
        print args,kwargs

class Target(object):
    def __init__(self,*observers):
        self.observes = observers

    #this notify for every access to the function
    def event(self,data):
        for obs in self.observes:
            obs.notify('event',data)
        print "event with",data

t = Target(Observer())
t.event(1)
#('event', 1) {}
#event with 1

否则,您可以使用decorator实现它,这非常类似:

def observe(f):
    def decorated(self,*args,**kwargs):
        for obs in self.observes:
            obs.notify('event',*args,**kwargs)
        return f(self,*args,**kwargs)
    return decorated

class Target(object):
    def __init__(self,*observers):
        self.observes = observers

    @observe
    def otherevent(self,data):
        print "other event with",data

现在,所有这些方法都有效,但它们不是很Python。用pythonic方法实现这样的东西的最佳方法是实现一个包装器,它检查属性访问并调用回调函数(可以是观察者的notify函数,但它是一种更通用的方法)

class Wrapper(object):
    def __init__(self,wrapped,*callbacks):
        self.wrapped = wrapped
        self.callbacks = callbacks

    def __getattr__(self,name):
        res = self.wrapped.__getattribute__(name)
        if not callable(res):
            return res
        def wrap(*args,**kwargs):
            for c in self.callbacks:
                c(self.wrapped,f,*args,**kwargs)
            return res(*args,**kwargs)
        return wrap

    def __str__(self):
        return self.wrapped.__str__()

#in this example I will keep a record of each call performed on a list
called = []
#this is the list
a = []
#and this is the wrapped list
w = Wrapper(a,lambda f,v,ign: called.append((f,v)) )
#I append an element to the wrapper
w.append(1)
#and I can see that it modify the original list
print a
#the print of the wrapped is well behaved, having defined the __str__ function
print w
#and we can see which function we called and which were the parameters
print called

这种方法稍微复杂一些,因为您必须手动重定向所有的magic方法,但是更强大的是允许将观察者模式实现到任何类型的对象,附加到任何兼容的函数,而不需要指定观察者的泛型类。有很多方法可以自动重定向所有的神奇函数调用,但这有点复杂,只会混淆要点

在python中工作时,越快忘记java,它就越有趣。我建议你读这篇文章:

http://dirtsimple.org/2004/12/python-is-not-java.html

祝你工作顺利!

相关问题 更多 >