Extensible Design for the Output Method of PrintShape #3
YongJin-Cho
started this conversation in
ADRs
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Extensible Design for the Output Method of
PrintShape(see PrintShape)Problem Statement
The
PrintShapefunction currently supports a limited set of output methods. To enhance its versatility, we need to extend its capabilities to include various output methods such as:By designing the output methods to be easily extendable, future modifications and maintenance will be more straightforward, ensuring that new output methods can be added with minimal changes to the existing codebase.
Design Candidates
Candidate 1:
PrintShapeprovidessetOutputandsetCharactersIn this design, the
PrintShapeclass is responsible for setting the output method and the characters used for printing the shape. The class will have the following methods:setOutput(output_stream): Sets the output method (e.g., console, string, file).setCharacters(characters): Sets the characters used to represent the shape.classDiagram class PrintShape { +print(size) // Prints the shape to the specified output_stream with characters +setOutput(output_stream) +setCharacters(characters) }Candidate 2:
PrintShapecreates a boolean image and prints it via theOutputinterfaceIn this design, the
PrintShapeclass creates a boolean image of the shape and delegates the printing to anOutputinterface. TheOutputinterface will have a method to print the shape, andStreamOut, which extends theOutputinterface, will provide methods to set the output method and characters:Output.print(boolean_image): Prints the boolean image of the shape.StreamOut.setOutput(output_stream): Sets the output method.StreamOut.setCharacters(characters): Sets the characters used to represent the shape.classDiagram class PrintShape { -output +print(size) // creates boolean image and prints it via output } class Output { <<interface>> +print(boolean_image) } class StreamOut { +setOutput(output_stream) +setCharacters(characters) +print(boolean_image) } PrintShape ..> Output Output <|.. StreamOutPros and Cons
Candidate 1:
PrintShapeprovidessetOutputandsetCharactersPros:
Cons:
PrintShapeis responsible for both shape creation and output handlingPrintShapeclass)Candidate 2:
PrintShapecreates a boolean image and prints it via theOutputinterfacePros:
OutputinterfaceCons:
Comparison Table
PrintShapeprovidessetOutputandsetCharactersPrintShapecreates a boolean image and prints viaOutputinterfacePrintShapeOutputinterfaceDesign Outcome
After evaluating the design candidates, Candidate 2 has been selected as the preferred approach. By having
PrintShapecreate a boolean image and delegating the printing responsibility to theOutputinterface, we achieve:This design allows us to easily implement all the required output methods (string printing, file printing, BMP file printing) by creating different implementations of the
Outputinterface without modifying the corePrintShapelogic.The modular architecture will make it straightforward to add more output formats in the future, such as SVG, HTML, or console output with ANSI colors, by simply creating new implementations of the
Outputinterface.Beta Was this translation helpful? Give feedback.
All reactions