Solved

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

Posted on 2012-03-12
5
382 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
Comment Utility
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
Comment Utility
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
Comment Utility
Do you see one of those that speaks to your question?
0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
>> 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
Comment Utility
Visitor pattern:

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now