如何在控制器中实现策略模式
我正在尝试为我的程序的控制器组件实现策略模式。
我有不同的视图状态,例如 CreateViewState,您可以在其中在空白画布上创建项目,因此它需要诸如 createInput()
、addToModel()
和 之类的方法东西。
我拥有的另一个状态是 EditViewState,您可以在其中编辑之前添加的项目。您选择一个输入,然后更改其颜色或大小或其他任何内容。因此控制器需要的方法是 selectInputAtLocation()
或 changeColor(java.awt.Color color)
或 changeSize(int size)
...
我的方法是创建一个 IController 接口,该接口具有常见的方法,如repOK()、toString() 和一些类似 thisWasTheLocation(int x, int y) 的方法,视图静态调用这些方法来传递屏幕上按下的位置到 控制器。
然而,对于完成他们的特定工作,我没有他们所拥有的通用方法。我正在考虑在界面中放置一个 doStuff(Item item)
方法,然后在其中执行控制器逻辑,以便客户端代码可以使用 IController.getInstance().doStuff(item)
code> 在整个代码中。据我预测,如果我调用控制器具有的特定方法(editController 的 changeColor()
,createController 的 createInput()
),我将需要强制转换控制器。
如果我在界面中创建一个 doStuff()
方法,那么我需要实现很多 if 语句,特别是对于 editController(它有超过 7-8 个我没有提到的方法)。
应该如何设计整个系统?
PS:控制器
是单例,因此getInstance()
I am trying to implement a strategy pattern for the controller component of my program.
I have different view states such as CreateViewState in which you can create items on a blank canvas, so it needs methods like, createInput()
, addToModel()
and stuff
.
Another state that I have is the EditViewState in which you can edit the previously added items. You select an input and then change its color or size or whatever. So methods that controller needs is selectInputAtLocation()
, or changeColor(java.awt.Color color)
or changeSize(int size)
...
The way I go about it is I created an IController interface that has common methods like repOK(), toString() and a couple more like thisWasTheLocation(int x, int y) which the view calls statically to pass the location pressed on the screen to the controller.
However for doing their specific job I don't have a common method that they have. I was thinking about putting a doStuff(Item item)
method in the interface and then do the controller logic in there so client code can use IController.getInstance().doStuff(item)
in the entire code. As far as I can predict I will need to cast the controller if I call the specific methods that they have (changeColor()
for editController, createInput()
for createController).
If I create a doStuff()
method in the interface, then I need to implement a lot of if-statements especially for the editController (it has more than 7-8 methods that I did not mention).
How should go about designing this whole system?
P.S: Controllers
are singletons therefore the getInstance()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从你的描述来看很难说,但听起来你正在编写自己的 MVC 框架。为什么不使用 Spring、Struts 或类似的?目前尚不清楚您的域模型有多复杂。
简而言之,您的问题应该如何设计整个系统?可能最好的回答是使用许多现有的 Java MVC 框架之一
良好的设计鼓励您转移任何棘手的业务逻辑(
doStuff()
)从控制器中出来并进入服务层。您的代码将更容易测试,并且不依赖于您的控制器。当您声明控制器需要的方法是 selectInputAtLocation()、changeColor(java.awt.Color color) 或 changeSize(int size)... 在我看来,您正在增加复杂性,其中不需要。
以 Struts 为例,您的 HTTP 表单 POST 将包含用户已编辑的数据,并且您的控制器可以在
ActionForm
bean 中使用这些数据。然后,您在决定调用哪个服务方法之前验证/清理此用户数据(类似于您所询问的策略模式)。关于您的控制器设计,您可以让每个域模型类有一个控制器类,如下所示
或者您可以为每个域对象有多个控制器类,以便每个类中只有一个方法,如下所示
所选的 MVC 框架将具有配置确定调用哪些方法等。
It is difficult to say from your description, but it sounds like you're writing your own MVC framework. Why not use Spring, Struts, or similar? It's not clear how complex your domain model is.
In brief your question How should go about designing this whole system? is probably best answered by Use one of the many existing Java MVC frameworks
Good design encourages you to move any tricky business logic (
doStuff()
) out of your Controllers and into the Service layer. Your code will be easier to test and not tied to your Controllers.When you state So methods that controller needs is selectInputAtLocation(), or changeColor(java.awt.Color color) or changeSize(int size)... this sounds to me like you're adding complexity where it is not needed.
Using Struts as an example your HTTP Form POST would contain the data that the user has edited and this is available to your Controller in an
ActionForm
bean. You then validate/sanitise this user data before making the decision on which Service method to call (similar to the Strategy pattern you are asking about).Regarding your Contoller design you could have one have one Controller class per domain model class like this
Or you could have multiple Controller classes for each domain object, so that there is only one method in each class like the following
The chosen MVC framework will have configuration to determine which methods get called, etc.