Solved

Design pattern for scenario where we need to expand functionality in future.

Posted on 2012-03-12
5
389 Views
Last Modified: 2012-03-28
I have a simple question related to Design Patterns. Suppose I have a class of TOY and object of this class moves based on instructions. Currently this class support only two types of instructions i.e. MoveForward and TurnLeft. In future there will be more type of instructions (like TurnRight, MoveBackwards etc).  I need to implement this functionality using design pattern so that in future it would be easier to expand functionality. Which design pattern is suitable for this and why?
0
Comment
Question by:tony_techtoneic
  • 3
5 Comments
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 500 total points
ID: 37715165
in your case it seems appropriate to have an array of Command (http://www.codeproject.com/Articles/15207/Design-Patterns-Command-Pattern) objects so every new command is just a new item in the array and it knows who to execute itself
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37715177
There are so many design patterns to choose from...

Essential Software Design Patterns
http://wwwipd.ira.uka.de/~tichy/patterns/overview.html

Command (Command Processor)

Purpose: separate composition of a request from the time it is executed.
Flexibility: multiple commands; add functionality such as undo/redo, scheduling.

Implementation: encapsulate command with additional state (the object(s) on which to operate); add command processor.


Chain of Responsibility

Purpose: pass a request down a chain of objects until an object handles it.
Flexibility: a) decoupling: handler of request is not known a priori (determined dynamically); b) extensibility: add new handlers.

Implementation: common interface for handlers, delegation along chain.


Visitor

Purpose: add new operations to stable class hierarchy without changing classes.
Flexibility: a) decouple operations that exist in variants for many classes from those classes; b)extensibility: add new operations.

Implementation: common accept interface in classes; methods for all classes in visitor.

Variations:

Default Visitor
Extrinsic Visitor
Acyclic Visitor
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37715187
Do you see one of those that speaks to your question?
0
 
LVL 22

Expert Comment

by:ambience
ID: 37720529
>> In future there will be more type of instructions (like TurnRight, MoveBackwards etc).

This is a fairly vague specification, exactly what constitutes an instruction? By instructions do you mean a method (a code construct) defined on the TOY class or object?

If you are considering a data-driven design where for example instructions are read from files then you can design the system like a virtual machine. The interpreter pattern would be a good choice, along with the command pattern.

However, it would be important to consider how TurnLeft works internally. Does it utilize micro-instructions internally? Does it use a language such that it is possible to write instructions in that language? An example would explain

TurnLeft.txt
setrotationaxis Z
rotate -90 // think of it as assembly

TurnRight() could then be implemented in terms of the micro language. I have deliberately used TurnLeft.txt as a filename to give a hint that the instructions are external and the TOY object is flexible because any new instruction can be defined in data with using only the micro-language.

You can find numerous examples on how to use Interpretter pattern and/or implement simple stack machines or virtual machines.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37722585
Visitor pattern:

Purpose: add new operations to stable class hierarchy without changing classes.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question