Adapter: XML Exercise
In Need Of An Adapter
This exercise is all about cleaning up the code smell,
Occurs when the interfaces of two classes are different and yet the classes are quite similar. If you can find the similarities between the two classes, you can often refactor the classes to make them share a common interface [Fowler 85, Kerievsky 43]
The
XMLBuilder
features logic to construct a tree structure using instances of TagNode
.The DOMBuilder
contains the exact same tree-construction logic, only it uses instances of classes that
implement the Element
interface (from the Document Object Model).There is a great deal of duplicated tree-construction code in each of the builders because TagNode
and Element
don't
share a common interface. Understood. If
TagNode
and Element
shared a common interface, the builder logic would become identical, thereby allowing it to be shared in a builder superclass. Sounds like a job for the Adapter pattern. (Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |