消除GWT ActivityMapper样板 [英] Eliminating GWT ActivityMapper boilerplate

查看:153
本文介绍了消除GWT ActivityMapper样板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GWT Activities和Places框架来构建我的应用程序,并且它转换得很好。有一件事让我感到困扰的是, ActivityMapper 实现是(1)接收应用程序中的所有视图(2)包含一个巨大的if / else块,用于实例化基于收到的地方。它只会随着视图数量的增加而变得更糟。

I am using the GWT Activities and Places framework to structure my application and it is turning out nicely. One thing that annoys me though is that the ActivityMapper implementation is (1) receiving all the views in the application (2) contains a giant if/else block for instantiating activities based on the received place. It will only get worse as the number of views increases.

我已经在使用杜松子酒,但我看不出在这里如何使用它。

I am already using Gin but I don't see how I can use it here.

如何减少或消除我的 ActivityMapper

推荐答案

目前还没有一个好的答案。我有一些代码生成方案,但目前它们都在白板上涂鸦。对于杜松子酒用户来说,它看起来像一个地方范围可能是方便的。

There isn't a great answer yet. I have code generation schemes in mind, but it's all scribbles on white boards at the moment. For Gin users it seems like a Place Scope might be handy.

Re:if / else级联,一种常见的方法是让您的Place对象实现访问者模式。例如。假设你已经为你的活动设置了AssistedInject(并且原谅了马虎的注入,它只是一个草图)。

Re: the if / else cascade, one common approach is to make your Place objects implement the visitor pattern. E.g. let's assume you have AssistedInject set up for your activities (and forgive the sloppy field injection, it's just a sketch).

class BasePlace extends Place {
    <T> T acceptFilter(PlaceFilter filter);
}

interface PlaceFilter<T> {
  T filter(FooPlace place);
  T filter(BarPlace place);
  T filter(BazPlace place);
}

public class MainActivities implements ActivityMapper {
  @Inject FooFactory fooMaker;
  @Inject BarFactory barMaker;
  @Inject BazFactory bazMaker;

  public Activity getActivity(PlaceChangeEvent e) {
     return ((BasePlace)e.getPlace()).acceptFilter(
       new PlaceFilter<Activity>() {
         Activity filter(FooPlace place) {
           return fooMaker.create(place);
         }
         Activity filter(BarPlace place) {
           return barMaker.create(place);
         }
         Activity filter(BazPlace place) {
           return bazMaker.create(place);
         }
       })         
   }
}

这篇关于消除GWT ActivityMapper样板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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