Shm*_*opy 8 java model-view-controller circular-dependency
我需要视图来保存对控制器的引用,因为它需要将控制器注册为事件监听器.我需要控制器来保存对视图的引用,因为在按钮单击时,我需要能够在列表中获取所选文件.(我有一个文件列表和一个"添加群集"按钮,所以当点击按钮时我需要获取所选文件)
所以总之我有:
Controller controller(view);
View view(controller);
Run Code Online (Sandbox Code Playgroud)
我确定这里有一些不好的设计,我只是想不通如何避免它..
一种可能的解决方案:
addView(View view)方法addController(Controller controller)方法。 我不确定您使用的Java技术,但在GWT应用程序中 - 并且使用MVP模式 - 不需要View来引用Controller:Controller(或Presenter)与View之间的所有通信是通过View实现的接口完成的.在您的特定情况下,您的代码应如下所示:
定义显示界面:
public interface Display {
public void registerEventListener(Listener aListener)
public List getSelectedFiles ()
}
Run Code Online (Sandbox Code Playgroud)
让View实现该接口:
public class View implements Display{
//The method implementations
}
Run Code Online (Sandbox Code Playgroud)
并在控制器中进行所有必要的绑定:
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1672 次 |
| 最近记录: |