简体   繁体   中英

Access level to elements in visitor pattern

In the visitor pattern , i want the client to only have access to the getters of the elements, while the visitors should have access to getters and setters. How would you implement it?

I don't want the visitors in the same package as the model (there are a lot of classes already). I was thinking about introducing IWriteable interface which contains setters and accept methods. Is there a better way?

在此处输入图像描述

Thanks

@Angel O'Sphere:

The package would contains models, visitors and factories all that ~2x (interfaces and impls). I had some thought about rogue programmer too, that's why I asked. Another approach would be:

public class ModelImpl implement IRead {
  @Override
  public Foo getFoo() {...}

  private void setFoo(Foo f) {...}

  public void accept(Visitor v) {
    v.visit(new ModelEditor());
  }

  private class ModelEditor implement IWrite {
    @Override
    public void setFoo(Foo f) {
      ModelImpl.this.setFoo(f);
    }
  }
}

But this approach has many drawbacks and is cumbersome without generative techniques:o

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM