samedi 4 septembre 2021

How to design objects to be used by different programs?

Let's suppose:

  • We have a bunch of Shape classes
  • Each shape has a public Draw() method and knows how to draw itself
  • We have a program whose only task is to load and display the shapes from file, not change them
  • We have a tool used for editing Shape classes visually, meaning a Shape class will be drawn to the screen, the user can select it and edit its data
  • The user can serialize the shape to a file and the program that displays the shapes should be able to deserialize the shape to look the same as the user saved it from the tool
  • Shapes contain non-public fields that determine the behavior of public methods, e.g. a Color field that tells the Draw() method what color to draw the shape in
  • It is fields like this that can be edited by the tool, but should not be visible to the program that displays the shapes

Therefore, the question is how to implement the shapes?

  • Having them implemented in a shared library that could be used by both programs would break encapsulation, but would have a single implementation.
  • Having one implementation for the editor, and another implementation for the program that draws the shapes, both implementations should have standardized de/serialization (same members) so they can be de/serialized properly. This fixes the encapsulation issue but we would have to maintain two implementations.

How should I design those shape objects, if I don't want the drawing program to know they can be edited at all?

Currently I am leaning towards the second option. Is there a third option I'm not seeing? What would you advice and why?

Aucun commentaire:

Enregistrer un commentaire