观察者模式 - 何时
在我的工作场所,我们一直在争论是否使用观察者模式来解决其中一个问题。我不知何故闻到了“过度使用”的味道,但对想法持开放态度。所以要求是
我们有一个对象层次结构 ->一个订单和订单中的多个行项目。取消订单时,所有订单项都需要取消。
为此,我们创建了一个 OrderCancel 类(它是观察者模式习惯用法中的主题)和 LineItemCancel 类(它是观察者)。我们还有一个带有 cancelOrders(List Orders) 方法的 OrderManager 类,该方法实例化 OrderCancel 和相应的 LineItemCancel 对象,然后将它们全部注册到 OrderCancel 中。代码如下。
public class OrderManager {
public void cancelOrders(List<Order> orders){
for(Order order :orders){
OrderCancel orderCancel = new OrderCancel(order);
Listener listener = new LineItemCancel(order);
orderCancel.addListeners(listener);
orderCancel.cancel();
}
}
}
public class OrderCancel implements Subject {
private List<Listener> listeners = new ArrayList<Listener>();
private Order order;
public OrderCancel(Order order) {
this.order = order;
}
@Override
public void addListeners(Listener listener) {
listeners.add(listener);
}
@Override
public void notifyListeners() {
for(Listener listener : listeners){
listener.update();
}
}
public void cancel() {
notifyListeners();
cancelOrder();
}
private void cancelOrder() {
}
}
public class LineItemCancel implements Listener {
private Order order;
public LineItemCancel(Order order) {
this.order = order;
}
@Override
public void update() {
cancelLineItem();
}
private void cancelLineItem() {
}
}
我确信这是不正确的使用。但我无法说服这个班级的设计师。我试图弄清楚自己这是否正确,因为设计师是工作中的建筑师之一。
期待听到您的想法。
We have been arguing back and forth at my work place about the use of the Observer pattern for one of the problems. I somehow smell "overuse" but am open to ideas. So the requirement is
We have a hierarchy of objects -> an order and multiple line items in the order. When the order is cancelled, all the line items need to cancelled.
To do this, we have created a OrderCancel class which is the Subject in the Observer pattern idiom and LineItemCancel class which is the Observer. We also have a OrderManager class with a cancelOrders(List orders) method which instantiates the OrderCancel and the corresponding LineItemCancel objects and then registers them all in the OrderCancel. The code is as follows.
public class OrderManager {
public void cancelOrders(List<Order> orders){
for(Order order :orders){
OrderCancel orderCancel = new OrderCancel(order);
Listener listener = new LineItemCancel(order);
orderCancel.addListeners(listener);
orderCancel.cancel();
}
}
}
public class OrderCancel implements Subject {
private List<Listener> listeners = new ArrayList<Listener>();
private Order order;
public OrderCancel(Order order) {
this.order = order;
}
@Override
public void addListeners(Listener listener) {
listeners.add(listener);
}
@Override
public void notifyListeners() {
for(Listener listener : listeners){
listener.update();
}
}
public void cancel() {
notifyListeners();
cancelOrder();
}
private void cancelOrder() {
}
}
public class LineItemCancel implements Listener {
private Order order;
public LineItemCancel(Order order) {
this.order = order;
}
@Override
public void update() {
cancelLineItem();
}
private void cancelLineItem() {
}
}
I am convinced this is improper usage. But I am not able to convince the designers of this class. I am trying to figure out myself if this is right as the designer is one of the architects at work.
Looking forward to hear your thoughts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
观察者模式只有在减少耦合时才有用。在这个例子中我没有看到任何耦合的减少,所以我想说它是过度使用的。
The Observer pattern is only useful when it reduces coupling. I don't see any reduction of coupling in this example so I would say it is overuse.
我同意@Pace,绝对不会减少耦合,绝对是过度使用。我的问题是,在您的示例中,最简单的方法是在您取消订单时让订单取消其自己的 LineItems;有充分的理由不为您的应用程序这样做吗?
I agree w/ @Pace, definitely doesn't reduce coupling, definitely overuse. My question is that in your example, the simplest approach semas to be to have the Order cancel its own LineItems when you cancel it; is there a good reason not to do that for your app?