lundi 30 novembre 2015

Which design pattern could be used for a shape editor? Visitor pattern in use for now

In hindsight of my question from earlier about the usage of the visitor pattern for a shape editor I came to the conclusion that I have to break the design rules of it.
Mainly because I need to update my Shape fields, so essentially I need to pass arguments back to the visitor.

Right now I am population the UI fields with the variables of the ShapeObject.

public class Editor implements ShapeVisitor{

    private Shape shape;

    @Override
    public Foo visit(CircleObject circle) {
          // populate fields
          shape = new CircleObject();
    }

    @Override
    public void visit(RectangleObject rectangle) {
          // populate fields
           shape = new RectangleObject();
    }


    public void setComponent(JsonArray arguments){
      Element element = getFromJson(arguments);
      element.getAttrs().accept(this);
    }
}

Somewhere in my code I have a Save Button which should update the ShapeObject.

        @Override
        public void buttonClick(ClickEvent event) {
                    element.setAttrs(shape);    
        }

Basically what I am doing is that I create a new Instance of a ShapeObject and update the fields there. Then I pass it to the element back via element.setAttrs(shape).


Essentially I would not need the visitor pattern at all, because I could achieve the same with the instanceof operator in the setComponent method. I am trying my best to avoid this operator, because in the near future I will have way more ShapeObjects then these two. I am not really sure if this is the approach I should take or maybe there is a even better one for a custom shape editor.

Best regards.

Aucun commentaire:

Enregistrer un commentaire