有没有推荐使用GWP中的观察者模式使用GWT? [英] Is there a recommended way to use the Observer pattern in MVP using GWT?

查看:154
本文介绍了有没有推荐使用GWP中的观察者模式使用GWT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用GWT根据MVP模式实现用户界面,但对如何继续操作有疑问。

I am thinking about implementing a user interface according to the MVP pattern using GWT, but have doubts about how to proceed.

这些是(一些)我的目标:

These are (some of) my goals:


  • 演示者对UI技术一无所知(即不使用com.google。*)

  • 该视图对演示者一无所知(不确定如果我想要它是模型不可知的,那么)

  • 该模型什么也不知道视图或演示者( ...显然)

我将在视图和演示者之间放置一个界面,并使用Observer模式来解耦两者:视图生成事件,演示者会收到通知。

I would place an interface between the view and the presenter and use the Observer pattern to decouple the two: the view generates events and the presenter gets notified.

令人困惑的是,GWT不支持java.util.Observer和java.util.Observable。这表明我所做的不是推荐的方法,就GWT而言,这引起了我的疑问:使用GWT实现MVP的建议方法是什么,具体考虑到上述目标?你如何做?

What confuses me is that java.util.Observer and java.util.Observable are not supported in GWT. This suggests that what I'm doing is not the recommended way to do it, as far as GWT is concerned, which leads me to my questions: what is the recommended way to implement MVP using GWT, specifically with the above goals in mind? How would you do it?

推荐答案

程序结构



我是怎么做到的 Eventbus 允许演示者(扩展抽象类 Subscriber )订阅属于不同模块的事件在我的应用程序每个模块对应于我的系统中的组件,每个模块都有事件类型,演示者,处理程序,视图和模型。

Program Structure

This is how I did it. The Eventbus lets presenters (extending the abstract class Subscriber) subscribe to events belonging to different modules in my app. Each module corresponds to a component in my system, and each module has an event type, a presenter, a handler, a view and a model.

订阅类型为 CONSOLE 的所有事件的演示者将收到从该模块触发的所有事件。对于更精细的方法,您可以随时让演示者订阅特定的事件,例如 NewLineAddedEvent 或类似的东西,但对我来说,我发现在一个模块上处理它水平够好。

A presenter subscribing to all the events of type CONSOLE will receive all the events triggered from that module. For a more fine-grained approach you can always let presenters subscribe to specific events, such as NewLineAddedEvent or something like that, but for me I found that dealing with it on a module level was good enough.

如果您希望您可以将演示者的救援方法调用为异步,但到目前为止,我发现自己很少需要这样做。我想这取决于你的确切需要。这是我的 EventBus

If you want you could make the call to the presenter's rescue methods asynchronous, but so far I've found little need to do so myself. I suppose it depends on what your exact needs are. This is my EventBus:

public class EventBus implements EventHandler 
{
    private final static EventBus INSTANCE = new EventBus();
    private HashMap<Module, ArrayList<Subscriber>> subscribers;

    private EventBus()  
    { 
      subscribers = new HashMap<Module, ArrayList<Subscriber>>(); 
    }

    public static EventBus get() { return INSTANCE; }

    public void fire(ScEvent event)
    {
        if (subscribers.containsKey(event.getKey()))
            for (Subscriber s : subscribers.get(event.getKey()))
                s.rescue(event);
    }

    public void subscribe(Subscriber subscriber, Module[] keys)
    {
        for (Module m : keys)
            subscribe(subscriber, m);
    }

    public void subscribe(Subscriber subscriber, Module key)
    {
        if (subscribers.containsKey(key))
            subscribers.get(key).add(subscriber);
        else
        {
            ArrayList<Subscriber> subs = new ArrayList<Subscriber>();
            subs.add(subscriber);
            subscribers.put(key, subs);
        }
    }

    public void unsubscribe(Subscriber subscriber, Module key)
    {
        if (subscribers.containsKey(key))
            subscribers.get(key).remove(subscriber);
    }

}

处理程序附加到组件,负责将本地GWT事件转换为专为我的系统而设的事件。下面的处理程序通过将它们包装在一个自定义的事件中来处理 ClickEvents ,并将它们在 EventBus 上为订阅者发布处理。在某些情况下,处理程序在触发事件之前执行额外检查是有意义的,有时甚至在决定天气或不发送事件之前。处理程序中的操作在将处理程序添加到图形组件时给出。

Handlers are attached to components, and are responsible for transforming native GWT events into events specialised for my system. The handler below deals with ClickEvents simply by wrapping them in a customised event and firing them on the EventBus for the subscribers to deal with. In some cases it makes sense for the handlers to perform extra checks before firing the event, or sometimes even before deciding weather or not to send the event. The action in the handler is given when the handler is added to the graphical component.

public class AppHandler extends ScHandler
{
    public AppHandler(Action action) { super(action); }

    @Override
    public void onClick(ClickEvent event) 
    { 
         EventBus.get().fire(new AppEvent(action)); 
    }

动作是一个枚举表示在我的系统中数据操纵的可能方式。每个事件都使用 Action 进行初始化。演示者使用该动作来确定如何更新视图。动作 ADD 的事件可能会使主持人向菜单添加新按钮,或向网格添加新行。

Action is an enumeration expressing possible ways of data manipulation in my system. Each event is initialised with an Action. The action is used by presenters to determine how to update their view. An event with the action ADD might make a presenter add a new button to a menu, or a new row to a grid.

public enum Action 
{
    ADD,
    REMOVE,
    OPEN,
    CLOSE,
    SAVE,
    DISPLAY,
    UPDATE
}

处理程序触发的事件看起来有点像这样。请注意事件如何为消费者定义界面,这将确保您不要忘记实施正确的救援方法。

The event that's get fired by the handler looks a bit like this. Notice how the event defines an interface for it's consumers, which will assure that you don't forget to implement the correct rescue methods.

public class AppEvent extends ScEvent {

    public interface AppEventConsumer 
    {
        void rescue(AppEvent e);
    }

    private static final Module KEY = Module.APP;
    private Action action;

    public AppEvent(Action action) { this.action = action; }

演示者订阅属于不同模块的事件,然后在触发时将其拯救。我还让每个演示者为它的视图定义一个界面,这意味着演示者不会知道关于实际graphcal组件的任何内容。

The presenter subscribes to events belonging to diffrent modules, and then rescues them when they're fired. I also let each presenter define an interface for it's view, which means that the presenter won't ever have to know anything about the actual graphcal components.

public class AppPresenter extends Subscriber implements AppEventConsumer, 
                                                        ConsoleEventConsumer
{
    public interface Display 
    {
        public void openDrawer(String text);
        public void closeDrawer();
    }

    private Display display;

    public AppPresenter(Display display)
    {
        this.display = display;
        EventBus.get().subscribe(this, new Module[]{Module.APP, Module.CONSOLE});
    }

    @Override
    public void rescue(ScEvent e) 
    {
        if (e instanceof AppEvent)
            rescue((AppEvent) e);
        else if (e instanceof ConsoleEvent)
            rescue((ConsoleEvent) e);
    }
}

每个视图都有一个 HandlerFactory 负责为每个视图创建正确类型的处理程序。每个工厂都使用 Module 实例化,它用于创建正确类型的处理程序。

Each view is given an instance of a HandlerFactory that is responsible for creating the correct type of handler for each view. Each factory is instantiated with a Module, that it uses to create handlers of the correct type.

public ScHandler create(Action action)
{
  switch (module)
  {
    case CONSOLE :
      return new ConsoleHandler(action);

该视图现在可以自由添加不同类型的处理程序,而不必知道确切的实施细节。在这个例子中,所有的视图需要知道的是, addButton 按钮应该链接到与动作 ADD 。这个行为将由主持人决定抓住事件。

The view is now free to add handlers of different kind to it's components without having to know about the exact implementation details. In this example, all the view needs to know is that the addButton button should be linked to some behaviour corresponding to the action ADD. What this behaviour is will be decided by the presenters that catch the event.

public class AppView implements Display

   public AppView(HandlerFactory factory)
   {
       ToolStripButton addButton = new ToolStripButton();
       addButton.addClickHandler(factory.create(Action.ADD));
       /* More interfacy stuff */  
   }

   public void openDrawer(String text) { /*Some implementation*/ }
   public void closeDrawer() {  /*Some implementation*/ }



示例



考虑一个简化的Eclipse,其中有一个类层次结构在左边,一个文本区域的代码在右边,一个菜单栏在顶部。这三个将是三个不同的观点,三个不同的主持人,因此他们将组成三个不同的模块。现在,完全可能的是,文本区域将需要根据类层次结构中的变化进行更改,因此,文本区域主持人不仅可以在文本区域内订阅被触发的事件,还可以订阅事件被从类层次结构中被解雇。我可以想像这样(每个模块将会有一组类 - 一个处理程序,一个事件类型,一个演示者,一个模型和一个视图):

Example

Consider a simplified Eclipse where you have a class hierarchy to the left, a text area for code on the right, and a menu bar on top. These three would be three different views with three different presenters and therefore they'd make up three different modules. Now, it's entirely possible that the text area will need to change in accordance to changes in the class hierarchy, and therefore it makes sense for the text area presenter to subscribe not only to events being fired from within the text area, but also to events being fired from the class hierarchy. I can imagine something like this (for each module there will be a set of classes - one handler, one event type, one presenter, one model and one view):

public enum Module 
{
   MENU,
   TEXT_AREA,
   CLASS_HIERARCHY
}

现在考虑我们希望在从层次结构视图中删除类文件时正确更新视图。这应该导致对gui的以下更改:

Now consider we want our views to update properly upon deletion of a class file from the hierarchy view. This should result in the following changes to the gui:


  1. 类文件应该从类层次结构中删除

  2. 如果类文件被打开,因此在文本区域中可见,它应该被关闭。

两个主持人,控制树视图的人和控制文本视图的两个演示者都将订阅从 CLASS_HIERARCHY 模块。如果事件的操作是 REMOVE ,则两个前导者都可以采取适当的操作,如上所述。控制层次结构的演示者大概也会向服务器发送一条消息,确保删除的文件被实际删除。该设置允许模块通过监听从事件总线触发的事件来对其他模块中的事件做出反应。没有什么联系,交换意见,演讲者或处理者是完全无痛的。

Two presenters, the one controlling the tree view and the one controlling the text view, would both subscribe to events fired from the CLASS_HIERARCHY module. If the action of the event is REMOVE, both preseneters could take the appropriate action, as described above. The presenter controlling the hierarchy would presumably also send a message to the server, making sure that the deleted file was actually deleted. This set-up allows modules to react to events in other modules simply by listening to events fired from the event bus. There is very little coupling going on, and swapping out views, presenters or handlers is completely painless.

这篇关于有没有推荐使用GWP中的观察者模式使用GWT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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