samedi 27 juin 2015

Design/Patterns - should I use interfaces or abstract classes?

I have following problem: I am creating an aplication for creating UML diagrams. Right now just to simplify everything I assume only couple of available diagram elements:

  1. class
  2. interface
  3. generalization
  4. interface implementation
  5. association
  6. aggregation

I decided to create one common abstract class for all of that elements:

abstract DiagramElement which has 2 subclasses also abstract:

  1. DiagramRelation
  2. DiagramObject

Nextly DiagramRelation has 4 subclasses:

  1. Generalization
  2. InterfaceImplementation
  3. Assosication
  4. Aggregation

And DiagramObject has 2 subclasses:

  1. Interface
  2. Class

I really wanted to post a picture so it would be all much more simplier but I don't have enough reputation points so sorry.

I came across following problem: each of this element have a different visual representation, ie: interface has only methods etc so each of this element will need to be show differently - I don't want to use multiple "if" instructions for it.

I use WPF and I decided that every control will place it's contest into StackPanel which will be placed inside MyContextControl (inherits after ContextControl and add interface atribute):

public interface IDiagramElementDraw
{
    public StackPanel Draw();
}

public MyContextControl : ContextControl
{
    private IDiagramElementDraw _drawingInterface;
    private StackPanel context;
    public DrawControl()
    {
        context = _drawingInterface.Draw();
    }
} 

But I don't know which class should implement IDiagramElementDraw interface - I don't want to implement it at the level of Interface, Class, Generalization etc classes because I want them to only represent logical information about each element.

I would appreciate any help, feel free to post complete different solution - this one may be completely wrong, this was just my idea.

Aucun commentaire:

Enregistrer un commentaire