我需要视图来保存对contoller的引用,因为它需要将控制器注册为事件侦听器。我需要contoller来持有对视图的引用,因为在单击按钮时,我需要能够在列表中获得选定的文件。(我有一个文件列表和一个'Add cluster‘按钮,所以当点击这个按钮时,我需要获取选定的文件)
所以简而言之,我有:
Controller controller(view);
View view(controller);我肯定这里有一些糟糕的设计,我只是想不出如何避免它..
发布于 2012-06-03 03:31:16
一种可能的解决方案是:
addView(View view)方法addController(Controller controller)方法。中设置引用变量,因此
发布于 2012-06-03 04:21:08
我不确定您使用的是什么Java技术,但在使用MVP模式的GWT应用程序-and中-视图不需要引用控制器:控制器(或展示器)和视图之间的所有通信都是通过视图实现的接口进行的。在您的特定情况下,您的代码应该如下所示:
定义显示界面:
public interface Display {
public void registerEventListener(Listener aListener)
public List getSelectedFiles ()
}让View实现该接口:
public class View implements Display{
//The method implementations
}并在控制器中进行所有必要的绑定:
public class Controller{
private Display view;
public Controller(){
//Or use some DI technology
this.view = new View();
//Get a Listener implementation, maybe an Anonymous Inner Class
this.view.registerEventListener(getListener());
}
public void processFiles(){
List files = view.getSelectedFiles();
//Do the processing here
}
}发布于 2012-06-03 03:35:02
我相信你可以使用封装,也可以使用惰性的初始化过程。我确信在初始化控制器的同时,您不需要View,反之亦然。如果没有,您也可以将上面的答案与bean的lazy init属性结合使用。
https://stackoverflow.com/questions/10865289
复制相似问题