Skip to Content.
Sympa Menu

gang-of-4-patterns - [gang-of-4-patterns] mvc/command question

gang-of-4-patterns AT lists.cs.illinois.edu

Subject: Design Patterns discussion

List archive

[gang-of-4-patterns] mvc/command question


Chronological Thread 
  • From: Ray Tayek <rtayek AT attbi.com>
  • To: gang-of-4-patterns AT cs.uiuc.edu
  • Subject: [gang-of-4-patterns] mvc/command question
  • Date: Tue, 27 May 2003 17:34:56 -0700
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns/>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

hi, writing a small app to view and manipulate an image in java. trying to use command with mvc.

i made all of the requests that the use could do into public static final enums using the singleton pattern sorta like jsr 201 at http://jcp.org/aboutJava/communityprocess/jsr/tiger/enum.html. i currently have two concrete subclasses of the command interface D4 (corresponding to the operations like rotation that are part of the symmetries of a sqaure http://mathworld.wolfram.com/FiniteGroupD4.html) and Gui (which has things like scale, offset, help about etc..).

the abstract base class for the request interface just an ordinal (unique over all instances of all subclasses of request), a code, a label, and a description (for tool tips and help). since this stuff is all const (i.e. the set of things that the user can do), i thought it reasonable to make them all enums that are subclasses of the request interface. so far so good.

i also have a command line controller and view (i find this helps isolate core code in the right place).

wiring the requests to the gui wigits was easy but maybe not necessary as they could have worked like the command line interface (which uses a factory method that is a switch on the "code" or the enum to get the request as an object - the code is the command line code that the user would type).

all was fine until i added the command pattern (using an implemenatation like in grand 1, but was thinking of using one more like command processor in posa 1 which prompted the earlier post for source code). (need undo and redo). also some of the command may need to be persisted (to annotate the image).

the receiver is the model. and currently has many methods: ececuteD4(D4 d4), scale(float scaleFactor), offset(Point point), etc. chances are there will only ever be one model. worst case there would be more than one instance, but that is not a problem, since each command has that as it's receiver.

and when the user types a command or clicks on a wigit, i need to construct a command object for that request. ok, i have a command factory, with some additional logic for the case where i need to supply some parameters (i.e. scale to 150%). so it's a little messy but ok (maybe this is an interpreter or a small language)

now the view needs a hint for the update , so he wants the original request or command or something, so it looks like i should maybe pass the command along to *each* method call on the model (i.e. model.d4(D4 d4, Command command) and model.scale(1.5, Command command)). or have an execute in the model (the commands execute method just calls model.execute(this), which require an ugly switch in the model (which does not seem to be where this belongs).

so i am thinking of passing the command to each model method that does something, so that update() in the view gets an exact hint of what changed. (and of course there is another ugly switch or a bunch of instanceof's).

so command.execute() does a model.execute(this) which has a switch (or mode.method(parms, this) and update() gets a command (or request) and has a switch.

model.execute(this) requires the mode to know about the commands, which sounds like a bad dependency. seems like command should be dependent on the model and not the other way around.

i don't like all the switches, any way to get rid of these switches?

am i on the right try track or very confused.

any pointers will be appreciated.

thanks

---
ray tayek http://tayek.com/ actively seeking mentoring or telecommuting work
vice chair orange county java users group http://www.ocjug.org/
hate spam? http://samspade.org/ssw/




  • [gang-of-4-patterns] mvc/command question, Ray Tayek, 05/27/2003

Archive powered by MHonArc 2.6.16.

Top of Page