当您使用生产者/消费者关系设计两个类时,如何避免循环依赖?这里ListenerImpl需要对Broadcaster的引用才能注册/取消注册,而Broadcaster需要一个引用回侦听器才能发送消息。此示例使用Java,但它可以应用于任何OO语言。
public interface Listener {
void callBack(Object arg);
}
public class ListenerImpl implements Listener {
public ListenerImpl(Broadcaster b) { b.register(this); }
public void callBack(Object arg) { ... }
public void shutDown() { b.unregister(this); }
}
public class Broadcaster {
private final List listeners = new ArrayList();
public void register(Listener lis) { listeners.add(lis); }
public void unregister(Listener lis) {listeners.remove(lis); }
public void broadcast(Object arg) { for (Listener lis : listeners) { lis.callBack(arg); } }
}
答案 0 :(得分:8)
我不认为这是一种循环依赖。
听众不依赖。
ListenerImpl依赖于Listener和Broadcaster
广播公司取决于听众。
Listener
^ ^
/ \
/ \
Broadcaster <-- ListenerImpl
所有箭头在Listener处结束。没有周期。所以,我觉得你没事。
答案 1 :(得分:7)
任何OOP语言?好。这是CLOS的十分钟版本。
(defclass broadcaster ()
((listeners :accessor listeners
:initform '())))
(defgeneric add-listener (broadcaster listener)
(:documentation "Add a listener (a function taking one argument)
to a broadcast's list of interested parties"))
(defgeneric remove-listener (broadcaster listener)
(:documentation "Reverse of add-listener"))
(defgeneric broadcast (broadcaster object)
(:documentation "Broadcast an object to all registered listeners"))
(defmethod add-listener (broadcaster listener)
(pushnew listener (listeners broadcaster)))
(defmethod remove-listener (broadcaster listener)
(let ((listeners (listeners broadcaster)))
(setf listeners (remove listener listeners))))
(defmethod broadcast (broadcaster object)
(dolist (listener (listeners broadcaster))
(funcall listener object)))
(defclass direct-broadcaster (broadcaster)
((latest-broadcast :accessor latest-broadcast)
(latest-broadcast-p :initform nil))
(:documentation "I broadcast the latest broadcasted object when a new listener is added"))
(defmethod add-listener :after ((broadcaster direct-broadcaster) listener)
(when (slot-value broadcaster 'latest-broadcast-p)
(funcall listener (latest-broadcast broadcaster))))
(defmethod broadcast :after ((broadcaster direct-broadcaster) object)
(setf (slot-value broadcaster 'latest-broadcast-p) t)
(setf (latest-broadcast broadcaster) object))
Lisp> (let ((broadcaster (make-instance 'broadcaster)))
(add-listener broadcaster
#'(lambda (obj) (format t "I got myself a ~A object!~%" obj)))
(add-listener broadcaster
#'(lambda (obj) (format t "I has object: ~A~%" obj)))
(broadcast broadcaster 'cheezburger))
I has object: CHEEZBURGER
I got myself a CHEEZBURGER object!
Lisp> (defparameter *direct-broadcaster* (make-instance 'direct-broadcaster))
(add-listener *direct-broadcaster*
#'(lambda (obj) (format t "I got myself a ~A object!~%" obj)))
(broadcast *direct-broadcaster* 'kitty)
I got myself a KITTY object!
Lisp> (add-listener *direct-broadcaster*
#'(lambda (obj) (format t "I has object: ~A~%" obj)))
I has object: KITTY
不幸的是,Lisp通过消除对它们的需求来解决大多数设计模式问题(例如你的问题)。
答案 2 :(得分:4)
与Herms的回答相反,我做看到一个循环。它不是依赖循环,它是一个引用循环:LI持有B对象,B对象持有(一个数组)LI对象。他们不容易释放,需要注意确保他们在可能的情况下自由。
一种解决方法就是让LI对象为广播公司提供WeakReference。从理论上讲,如果广播公司已经离开,那么无论如何都没有注销,所以你的注销将只是检查是否有广播公司注销,如果存在则这样做。
答案 3 :(得分:0)
我不是java开发者,但是像这样:
public class ListenerImpl implements Listener {
public Foo() {}
public void registerWithBroadcaster(Broadcaster b){ b.register(this); isRegistered = true;}
public void callBack(Object arg) { if (!isRegistered) throw ... else ... }
public void shutDown() { isRegistered = false; }
}
public class Broadcaster {
private final List listeners = new ArrayList();
public void register(Listener lis) { listeners.add(lis); }
public void unregister(Listener lis) {listeners.remove(lis); }
public void broadcast(Object arg) { for (Listener lis : listeners) { if (lis.isRegistered) lis.callBack(arg) else unregister(lis); } }
}
答案 4 :(得分:0)
使用弱引用来打破循环。
请参阅this answer。
答案 5 :(得分:0)
这是Lua中的一个示例(我在这里使用我自己的Oop lib,请参阅代码中对'Object'的引用。)
与Mikael Jansson的CLOS示例一样,您可以直接使用函数,无需定义侦听器(请注意使用'...',它是Lua的varargs):
Broadcaster = Object:subclass()
function Broadcaster:initialize()
self._listeners = {}
end
function Broadcaster:register(listener)
self._listeners[listener] = true
end
function Broadcaster:unregister(listener)
self._listeners[listener] = nil
end
function Broadcaster:broadcast(...)
for listener in pairs(self._listeners) do
listener(...)
end
end
坚持你的实现,这是一个可以用任何动态语言编写的例子:
--# Listener
Listener = Object:subclass()
function Listener:callback(arg)
self:subclassResponsibility()
end
--# ListenerImpl
function ListenerImpl:initialize(broadcaster)
self._broadcaster = broadcaster
broadcaster:register(this)
end
function ListenerImpl:callback(arg)
--# ...
end
function ListenerImpl:shutdown()
self._broadcaster:unregister(self)
end
--# Broadcaster
function Broadcaster:initialize()
self._listeners = {}
end
function Broadcaster:register(listener)
self._listeners[listener] = true
end
function Broadcaster:unregister(listener)
self._listeners[listener] = nil
end
function Broadcaster:broadcast(arg)
for listener in pairs(self._listeners) do
listener:callback(arg)
end
end