user167471
user167471

Reputation: 11

How to design/implement the command pattern when commands have variable number of parameters?

I am constructing a diagram editor in WPF on Windows 7. Notwithstanding that I am on the verge of learning significant design techniques (TDD, Prism, MVVM, dependency injection), though I understand a handful of established design patterns, here is my question:

As a whole, the commands will have different numbers and type combination of parameters. (To be clear, each command has a fixed set of parameters) For example, the following, all of which can be performed with the mouse :

Command Create New Node : parameter = location for new node (Point)

Command Move Node to new position : parameters= Node (graphNode), new location (Point)

Command make an edge connecting two nodes: parameters= From Node(graphNode), To Node(graphNode), Type of Edge (GraphEdgeType)

How ought I apply either the factory or abstract factory pattern to best encapsulate such commands?

What is the preferred way for the client to pass these parameters to the Command executive?

(I have hunted here and elsewhere but not found questions or answers so explicitly framed, and ready to be redirected to something I couldn’t find:-)

[EDIT] I have not been explicit enough:

IF I make a CommandFactory to return Commands, should it be passed commandType (an Enum, say) and a parameter set object ... or should it only be passed the commandType, so that the client subsequently primes the command with parameters?

Upvotes: 0

Views: 426

Answers (2)

user167471
user167471

Reputation: 11

I suspect that this https://cuttingedge.it/blogs/steven/pivot/entry.php?id=91 (which I found at Command Pattern : How to pass parameters to a command?) is what I am looking for even though I do not yet understand it - it likely transcends any of the melange of techniques I was envisaging but could not express.

Upvotes: 1

brunnerh
brunnerh

Reputation: 184607

How ought I apply either the factory or abstract factory pattern to best encapsulate such commands?

What are you talking about? Just pass in a class with properties for all the "parameters" as parameter.

(If anything this sounds like you need a state machine.)

Upvotes: 0

Related Questions