难以保留需要由不同包访问的方法的默认可见性
我正在开发一个扫雷器。我有 3 个包裹。 1.前端 2.后端 3.mineSweeperControl。
mineSweeperControl 包含一个实现 ActionListener 的 ActionSplicer 类。在 frontEnd 中,我有一个 JBUTTONS 数组和一个 ActionSplicer 数组对象,使得 splicerobj[i][j] 监听 Button[i][j]。(一对一对应)
backEnd 包含一个对象数组,使得object[i][j]具有button[i][j]的背景详细信息,例如MineValue,isCellEmpty,isCellFlagged等...
方法doBackgroundAction(i,j){.. } 在类 BackEndManager 中定义。 在ActionSplicer的actionPerformed中,我调用了doBackgroundAction(i,j),这样前景的任何变化也会影响背景。
我的问题出在哪里?
doBackGroundAction(i,j) 需要公开,因为它被调用 在不同的包中。
但我不希望任何方法公开,因为它可能会降低灵活性,然后任何人都可以更改属性的值。
我无法扩展 BackEndManager 类,因为我正在前端创建 ActionSplicer 对象数组。
因此,我需要一些关于声明 doBackGroundAction(i,j) 的指导。 在某些不可避免的情况下将方法声明为公共是否是正确的方法? 或者我如何更改我的设计以保留具有默认可见性的方法。
I am developing a MineSweeper. In that I have 3 packages. 1.frontEnd 2.backEnd 3.mineSweeperControl.
mineSweeperControl contains an class ActionSplicer which implements ActionListener.In frontEnd I have an array of JBUTTONS and an array of ActionSplicer objects, such that splicerobj[i][j] listens to button[i][j].(one to one correspondence)
backEnd contains an array of objects such that object[i][j] has the background details of button[i][j] such as MineValue,isCellEmpty,isCellFlagged etc...
Method doBackgroundAction(i,j){..} is defined in class BackEndManager.
In actionPerformed of ActionSplicer, I call doBackgroundAction(i,j) so that the any change in foreground will also affect the background.
Where my problem is?
The doBackGroundAction(i,j) needs to be public since it is called
in different package.
But I don't want any method to be public, since it may reduce the flexibility and then any person can change the values of attributes.
I cannot extend the class BackEndManager since I am creating an array of ActionSplicer objects in frontEnd.
Hence I need some sort of guidance about declaring doBackGroundAction(i,j).
Whether it is right way to declare the methods as public in some non-avoidable situvation?
Or How can I change my design to retain the method with default visibility.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了不将该方法公开为公共,您可以使用如下方法:
然后,每当您需要该方法时,创建 BackEndManager 的子类(因为受保护的允许子类访问该方法):
这将保持初始类完整并允许您公开一个新的方法方法通过子类“隐藏” BackEndManager 类的真实内部。
编辑:我刚刚看到这一行:
“我无法扩展 BackGroundManager 类,因为我正在前端创建 ActionSplicer 对象数组。”
如果
BackGroundManager
你的意思是BackEndManager
那么我的想法没有用。您能把这一点说得更清楚吗?据我了解ActionSplicer
位于mineSweeperControl
包中,而frontEnd
是另一个包,那么与BackEndManager
有何联系?In order to not expose that method as public you could have something like this:
Then whenever you need the method create a subclass of BackEndManager (since protected allows subclasses to access the method):
This keeps the initial class intact and lets you expose a new method through the subclass that "hides" the real internals of the
BackEndManager
class.Edit: I just saw this line:
"I cannot extend the class BackGroundManager since I am creating an array of ActionSplicer objects in frontEnd."
If by
BackGroundManager
you meanBackEndManager
then my idea is not useful. Can you please make this point more clear? From what I understandActionSplicer
is in the packagemineSweeperControl
andfrontEnd
is another package so what is the connection withBackEndManager
?