What is the design pattern? I need two modes in my application, edit and view

If I need two modes in an application, which design pattern should I use so that I can prevent ugly conditional code? Currently an MVC application, but I don't want to have conditional code in my controllers, and I don't want two controllers for each view to not need it.

Any suggestions?

+2


source to share


7 replies


A different subclass for each implementation with common functionality either in a common superclass or using the Template Method Template .



+4


source


A state pattern perhaps ?



+2


source


Abstract Factory or Proxy . Your controller will contain some kind of Factory or Proxy instance that is used to retrieve the "mode" and act on it accordingly.

+1


source


It's hard to say for sure without additional information, but I would suggest a strategy template. You can use the same controller and simply change the strategy object to make the desired change in behavior.

Here's an article you might find helpful: http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html

0


source


take a look at the JSR-168 portlet, java portlet and its referenced implementation, it should look like what you are trying to achieve.

0


source


The appropriate place for such a solution is the MVC controller. I would recommend that you write it there first. If it's really repetitive, it might be easy to figure out how to clean it up: you can move the conditional logic to the base class, or, depending on the language, deal with some kind of filter. You can also create multiple "factories" for views that understand the "mode" of your application. Architecturally, however, it's all in the controller.

You are right not to want this in view. That would be pretty messy. You probably want two versions of views, one for "viewing" and one for "editing".

After all, that's what controllers are for. Good luck!

0


source


In the CafeTownsend demo with PureMVC, there is a similar situation where there are two different views and two separate proxies. You absolutely don't need any conditional code for this. I don't know what technology and programming language you are using, but in Flex this would be a ViewStack with ListView and EditView as children:

The matching mediator is registered on demand when the view is created. You can check out other implementations using the previous link.

0


source







All Articles