Here's one of my experience on Design Patterns:
Strategy Pattern - I used strategy pattern in this scenario: "In version 6.0 of product, their is a table of Characters where each entry in table is
Char1 Char2 Value eg. A B 30
Actually each entry in this table specifies amount of space to keep between a pair of characters when these characters are rendered on the screen. Now, this was in our legacy system. In the next version 6.5 of product, the format changed to
Wide_Char1 Wide_Char2 Value1 Value2 eg. A B 30 40
Here each entry specifies amount of spacing in Horizontal writing mode and Vertical writing mode and the format of characters changed to 2- byte instead of earlier 1-byte.
Now, in yet next version 7.0 of product, the format changed. And so, was done in latest version 8.0.
So, I had 3 legacy formats of tables which were stored in the document. And, when they were read in latest version, I had to convert them to latest format.
I encapsulated the conversion code into different strategy objects - Convert6_0To8_0Strategy, Convert6_5To8_0Strategy, Convert7_0To8_0Strategy each inheriting from base class IBaseConvertStrategy
Also, I created a factory method that would encapsulate the creation of strategy object based on document version being opened.
The client code which looked like this before I refactored the code:
if (docVer == 6_0) { Conversion code for 6_0 to 8_0
} else if (docVer == 6_5) { Conversion code for 6_5 to 8_0
} else if ... ...
Also, their was a case of downsaving the current doc to legacy doc. So, this involved conversion from 8.0 to 7.0, 8.0 to 6.5 etc. So, the code was much more complicated and involved lot more details than this.
I refactored the code to put it into the above mentioned structure. After this the client code looked like this:
.... IBaseStrategy baseStrategy = CreateConvertStrategy(docVer); baseStrategy.Convert(); ....
As you can see the conversion logic is encapsulated in the Strategy objects. The client code is insulated from that.Now, suppose that the format again changes from 8.0 to 9.0. The client code won't change in this case. Only new strategy objects would be created. And this is a very big possibility that this would change again. :):):)
Any feedback on this ??????
Thursday, May 15, 2008
Subscribe to:
Posts (Atom)