观察者设计 - 如何访问method.invoke范围有多大? [英] Observer design -- how to access method.invoke scope?

查看:109
本文介绍了观察者设计 - 如何访问method.invoke范围有多大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个Java观察者/可观察的设置中,我打开一些外地Observer.update(如事件ID)的对象参数中,以确定如何处理可观察到的通知​​。

i currently have a Java Observer/Observable setup in which i switch on some field within the Object parameter of Observer.update (e.g. event id) to determine how to handle an Observable notification.

这将创建详细的code这样的:

this creates verbose code like:

public void update (Observable o, Object arg) {
    if (arg instanceof Event) {
        switch (((Event)arg).getID()) {
            case EVENT_TYPE_A:
                // do stuff...
                break;
            case EVENT_TYPE_B:
                // do stuff...
                break;
            case EVENT_TYPE_C:
                // do stuff...
                break;
        }
    }
}

从ActionScript背景的,这种感觉,而不是传递一个观察的实例不必要的冗长,我...,我想preFER传递一个回调方法直接由观测(更确切地说叫,通过一个子类)。不过,我并不清楚如何确定哪个方法被调用的对象(类实例的拥有的方法)。

coming from an ActionScript background, this feels unnecessarily verbose to me...instead of passing an instance of an Observer, i'd prefer to pass a callback method to be called directly by the Observable (more specifically, by a subclass). however, i'm not clear how to determine the object on which the method should be invoked (the class instance that 'owns' the method).

我可以通过一个参考实例封闭的方法,但这种气味像坏OOP。

i could pass a reference to the instance enclosing the method, but this smells like bad OOP.

我是找错了树?或者是有一个干净的方式来实现这一目标?

am i barking up the wrong tree? or is there a clean way to achieve this?

推荐答案

一个更清洁的实施将涉及去除该事件是否可以通过观察者/可观察的处理逻辑,实际观察/观察的本身。它看起来好像动作已经离开了有关Observer模式一个有趣的想法。观察(没有双关语,意):

A cleaner implementation would involve removing the logic of whether the event can be handled by the observer/observable, to the actual observer/observable itself. It appears as if ActionScript has left you with a funny idea about the Observer pattern. Observe (no-pun-intended):

public interface Observer{

  public void update( Event arg );
}

public class Event{

  public int ID;
}

public Button implements Observer{

  public void update ( Event arg ){

     switch (arg.ID){

       case 1:  //Buttonsy handle events of type 1
         //do something useful;
         break;
       default:
         System.out.println("Buttons don't handle events of ID: " + arg.ID);
         break;
     }
  }
}

public ProgressBar implements Observer{

  public void update ( Event arg ){

     switch (arg.ID){

       case 2: //ProgressBars handle events of type 2 and 3
         //do something useful;
         break;
       case 3:
         //do something else useful;
         break;
       default:
         System.out.println("Progress bars don't handle events of ID: " + arg.ID);
         break;
     }
  }
}


public class Subject{

 private ArrayList<Observer> allUIControls;

 public registerControl( Observer control ){

   allUIControls.add( control );
 }

 public void updateControls ( Event arg ) {

   foreach ( Observer control in allUIControls ){

     //pass the event to each UI control, and let the EVENT decide if it can understand the Event.ID
     //its not the job of Subject to decide if the Observer is fit to handle the event. THIS IS NOT THE OBSERVER pattern.
     control.update( arg );
   }
 }
}

这篇关于观察者设计 - 如何访问method.invoke范围有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆