Identifying which pattern fits better.

Posted by Daniel Grillo on Programmers See other posts from Programmers or by Daniel Grillo
Published on 2011-03-02T20:03:47Z Indexed on 2011/03/03 15:33 UTC
Read the original article Hit count: 241

Filed under:

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?

© Programmers or respective owner

Related posts about design-patterns