简体   繁体   English

从GUI隐藏方法

[英]Hiding methods from the GUI

I am implementing the observer pattern in java to separate my model classes from the GUI. 我正在用Java实现观察者模式,以将模型类与GUI分开。 The problem is that, every time the update-method is called, the model-instance is given as a parameter. 问题在于,每次调用update-method时,都会将model-instance作为参数给出。 That means, the GUI can call methods on the model-objects which should not be called from the GUI. 这意味着,GUI可以在模型对象上调用不应从GUI调用的方法。 How to avoid that the GUI can manipulate the model objects? 如何避免GUI可以操纵模型对象?

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. 为了实现这一点,您需要引入另一层,它将使用一个模型对象并将其转换为DTO并返回到GUI,反之亦然。 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) 为模型类创建一个接口,该接口仅定义GUI可以调用的方法(即模型类当前实现的方法的子集,只能由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 在传递模型的方法中,将参数类型从模型的类更改为模型现在实现的新接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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