Originally posted by Buvana Ramkumar:
Instead of that use the Strategy Pattern.
How? Where? Here is the
intent of the "Strategy" pattern
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
The original problem statement is something like:
I need to create XML style B data from XML style A data. I already have code that creates Style X objects from XML style A data.
How does a family of interchangeable algorithms help?
This problem reminds me of the Copy program example given in the discussion of the
dependency inversion principle (DIP) - only in this case we are not copying but transforming.
So there are three main parts:
Reader for Style X objects Transformer of Style X objects to Style Y objects Writer for Stype Y objects The transformer pulls the objects out of the Reader, collects as many as are necessary and then writes one or a cluster of Style Y objects out to the Writer. In this case we should have most of the code for the XML style A to Style X Object Reader. This list already shows a separation of concerns: Reader is responsible for reconstituting the Style X objects, the Transformer is responsible for transforming objects and the Writer is responsible serializing Style Y Objects.
Program to an interface, not an implementation suggests that we design a "Style X Object Reader" interface to be used by the Transformer and to be implemented by the concrete "XML style A to Style X Object Reader" and that we design a "Style Y Object Writer" interface to be used by the Transformer and to be implemented by the concrete "Style X Object to XML style B Writer". The details of these interfaces depend on the actual structure of the object graphs of the Style X and Y Objects. Now repeated application of various relevant OO design principles will guide you during the refinement of your design. In the end you will probably end up with a concrete XmlAObjXReader (implements ObjXReader) which is configured with one or more input files, a concrete ObjYXmlBWriter (implements ObjYWriter) which is configured with one or more output files - both of which are then
dependency injected into a concrete ObjXObjYTransformer as ObjXReader and ObjYWriter respectively.
Don't overemphasize object-oriented design patterns - there isn't a pattern for every problem - object-oriented design principles are a much more valuable and a more broadly applicable tool and all OO design patterns are based on object-oriented design principles.
See also this topic:
Design Patterns... [ May 06, 2008: Message edited by: Peer Reynders ]