Solved

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

Posted on 2012-03-12
5
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

724 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