简体   繁体   中英

Hiding methods from the GUI

I am implementing the observer pattern in java to separate my model classes from the GUI. The problem is that, every time the update-method is called, the model-instance is given as a parameter. That means, the GUI can call methods on the model-objects which should not be called from the GUI. How to avoid that the GUI can manipulate the model objects?

In order to achieve that, you need to introduce another layer that will take a model object and convert it into DTO and return to GUI and vice versa. Kind of Data access design pattern used in many projects.

For more information visit this link

Hope this gives you the idea.

  • Create an Interface for the model class that only defines the methods that the GUI can call (ie a subset of the methods currently implemented by the model class that should only be seen by the GUI)
  • Have the model class implement this new Interface, which should be trivial since the methods are already implemented there.
  • In the methods where the model is passed, change the parameter type from the class of your model to the new interface that the model now implements

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