2

I'm developing a software to program a device.

I have some commands like Reset, Read_Version, Read_memory, Write_memory, Erase_memory.

Reset and Read_Version are fixed. They don't need parameters.

Read_memory and Erase_memory need the same parameters that are Length and Address.

Write_memory needs Lenght, Address and Data.

For each command, I have the same steps in sequence, that are something like this sendCommand, waitForResponse, treatResponse.

I'm having difficulty to identify which pattern should I use. Factory, Template Method, Strategy or other pattern.

Edit

I'll try to explain better taking in count the given comments and answers.

I've already done this software and now I'm trying to refactoring it. I'm trying to use patterns, even if it is not necessary because I'm taking advantage of this little software to learn about some patterns. Despite I think that one (or more) pattern fits here and it could improve my code.

When I want to read version of the software of my device, I don't have to assembly the command with parameters. It is fixed. So I have to send it. After wait for response. If there is a response, treat (or parse) it and returns.

To read a portion of the memory (maximum of 256 bytes), I have to assembly the command using the parameters Len and Address. So I have to send it. After wait for response. If there is a response, treat (or parse) it and returns.

To write a portion in the memory (maximum of 256 bytes), I have to assembly the command using the parameters Len, Address and Data. So I have to send it. After wait for response. If there is a response, treat (or parse) it and returns.

I think that I could use Template Method because I have almost the same algorithm for all. But the problem is some commands are fixes, others have 2 or 3 parameters.

I think that parameters should be passed on the constructor of the class. But each class will have a constructor overriding the abstract class constructor. Is this a problem for the template method? Should I use other pattern?

6
  • 3
    You'll use many design patterns. Why ask for "best"? Provide an overview of your design, so that we can comment on the design and how it uses various design patterns.
    – S.Lott
    Commented Mar 2, 2011 at 20:09
  • 5
    If you have trouble finding a pattern that fits, there isn't one... stop wasting your time searching, and save future maintainers from cursing your name for choosing some wholly weird design just to have patterns.
    – user7043
    Commented Mar 2, 2011 at 20:10
  • I'm with @S.Lott. I don't see a concise enough problem to identify a single Design Pattern to use. Commented Mar 2, 2011 at 20:10
  • 1
    Sounds like "Click button 1, send command A. Fill in text field, click button 2, send command B.", etc. You don't need a design pattern for that. You might not even need comments in most of the code if you don't complicate it further. It could be even simpler if you're not using a GUI. Commented Mar 2, 2011 at 20:13
  • @S.Lott I've edited my question. Commented Mar 3, 2011 at 12:56

8 Answers 8

1

If all your commands can be handled using the same algorithm, then it should work with the Template Method. Ie. delegate the actual steps to command subclasses.

Regarding your question about commands having different arguments in the constructor. Object construction is not an issue with the Template pattern (as it is a "behavioural" pattern and not "creational". Use a command factory for that.

3

You don't need a pattern for everything! If a pattern doesn't fit then don't use one! Just figure out what you need to do and find the simplest method to do it.

2

Right now, it's fashionable to say things like "patterns aren't a silver bullet" without any kind of rationalization. It's a copout - patterns do have a use.

In this instance, from your description, your situation smells like template method + regular object oriented design might be a suitable first plan of attack.

2
  • +1 for reminding that patterns exist for good practical reasons. Commented Mar 2, 2011 at 21:41
  • Agreed. Patterns usually help extract what you are doing into something testable, replaceable, and readable. But don't get carried away with abstraction... "the most simple way of doing it but no simpler" I believe someone put it. Commented Mar 3, 2011 at 1:44
1

Using a pattern isn't always the correct answer. Yea it's cool to try to use patterns but when you think of the pattern as a hammer don't make every problem the nail.

1

Just because you can is not a good reason to do something. Use an existing pattern if it exists, but don't shoehorn a solution into an existing pattern. However, if you have a lot of similar things, do generalized the common functionality.

In my first job, I build a lot of COBOL programs which ran in limited memory. To do this I used two mainline procedures. Most programs used the simple form:

  • initialize (open files, variables, and buffers)
  • process (do whatever the program was supposed to do)
  • cleanup (write end of reports, close files)

Reports which required data sorts consisted of a longer mainline:

  • initialize (open files, variables, and buffers)
  • extract data (feed sort route data lines with reversed keys)
  • sort data
  • generate report (read sorted data and output the report)
  • cleanup (write end of reports, close files)

Setting up conventions and generalizations can make your coding faster to develop. More importantly it will usually be simpler to find bugs. If you violate conventions, it may be more difficult to find where you introduced the bug.

In your case you can deal with the problem either synchronously or asynchronously. The synchronous method you would be do_command( returns response ). The asynchronous methods are send_command( returns commandSent) and handleInterrupt( receives response ). The asynchronous option is more difficult to deal with as you need to have somewhere for handleInterrupt to output the results. You also man need exclusion semaphores to prevent sending commands while a prior command is in progress. The do_command or send_command methods would map one to one to your commands. handleInterrupt would need to know or derive what kind of data to expect.

0

It's hard to say without viewing the code... But this sounds more like refactoring than patterns.

Read memory, erase memory, and write are all memory operations so they could have a shared "MemoryArea" class/struct (that is both an address and a length). Write would take an additional parameter of Data.

Send, wait, treat are commands. It may be appropriate to use a command pattern. It may also be appropriate to have a common interface or Template Method pattern on these classes so the common functions can be called sequentially from a higher layer. You may also look at strategy pattern... but it doesn't really fit here.

You may use a factory that constructs a commonly interfaced class... again hard to say.

I would recommend the head first patterns book.

0

I read Design Patterns: Elements of Reusable Object-Oriented Software and learned a lot from it. Still, I don't start a program by thinking of patterns that may apply. When I start a program, I don't think about the final form of the program. Instead, I think about the first unit test I should write.

For your program, I might first write a test for reading the device version. After getting that to work, I might write another unit test to write memory, then read it back and make sure the data read matched the data written. If you refactor the code as you go, at some point you may see a that a particular design pattern could help you reduce code duplication. Or you may not. That's fine. Using OO patterns doesn't necessarily make code better.

You should think of design patterns as solutions to code maintenance problems. When a problem appears, if no good solution is obvious to you, refer to the book and see if some patterns match your problem.

For example, it may be the case that supporting a new program subcommand required changing many classes. If I were faced with this problem, I would think about my ideal way to support a new command. I might decide that it would be best if could support a new subcommand by implementing one new class. If I wasn't sure how to make that happen, I could scan the patterns in the book and perhaps I would decide that a combination of the Factory and Command patterns would work.

0

You don't use patterns... patterns use your code to manifest themselves.

...ok, ok, too philosophical. :)

Don't go into a design problem thinking about what pattern to use. Start designing and doing some rough coding and at some point you may see some common patterns start to emerge - at that point, take a little while to think about how your application may need to evolve in the future. After all, this is what most patterns are about in the end: making good, decoupled software that can adapt easily.

Design your application in such a way that it's flexible along that axis, and you'll likely find yourself implementing something that looks like a standard GoF pattern. Reference the pattern to see if it really does apply, and to make sure you implement it properly. Afterwards, call out the pattern you're using in design documents and/or comments. After all, the biggest win of patterns is that they form a common language.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.