Java(FX) - 只允许1个类从单例类调用方法 [英] Java(FX) - Only allow 1 class to call a method from a singleton class

查看:312
本文介绍了Java(FX) - 只允许1个类从单例类调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个项目,我正在使用单例类来更改用户的视图。此DisplayManager(单例)具有类似 addView(视图视图) replaceView(视图视图)的方法。但是它还有一个名为 displayRootView()的方法,它只能被调用一次(在init期间)并且只能被调用一个类,即扩展Application类的StartUp类。 / p>

关于如何阻止使用单例的其他类调用 displayRootView()方法的任何想法?



我已经考虑过StackTrace,但这似乎并不理想。我可能通过在StartUp类上使用标记界面将其与其余类别分开?



任何建议都会非常感激。

解决方案

呃,这很难阻止某些类调用你的方法,因为它打破了一些核心的OOP原则。该方法不应该关心调用它。它是关注点的基本分离 - 你的方法应该明确约定它的作用,而不是当时JVM的状态。



考虑以下问题:




  • 如果你继承 StartUp 会怎样?例如,要分离桌面,移动和Web平台?

  • 如何在不涉及 StartUp 的情况下对此方法进行单元测试?

  • 如果你需要另一层抽象会怎么样?

  • 如果和何时添加代理(通过AOP或Spring代理)怎么样?

  • 如果您需要从 Timer 调用该方法,会发生什么?它仍将从 StartUp 类源调用(并且是正确的),但它不会出现在堆栈跟踪中。



以及其他此类注意事项。



抛出某种异常(如 IllegalStateException 或自定义异常),以防方法被调用第二次绝对有效恕我直言。



这看起来你可能想要代码上的静态检查,而不是代码内或运行时检查。我不认为向Findbugz或PMD添加自定义规则以查找方法的所有直接调用并检查调用类(如果从其他位置调用构建失败),那将非常困难。但我不认为这样的检查实际上是有用的。



最后,你需要合法使用方法以外的方法。所说的课程,而不是有人在被警告并且已经创建了适当的Javadoc后会不小心使用它。


I'm currently working on a project where I'm using a singleton class to change the view for the user. This DisplayManager (the singleton) has methods like addView(View view) and replaceView(View view). But it also has a method called displayRootView() which may only be called once (during init) and by only 1 class, i.e. the StartUp class that extends the Application class.

Any idea as to how I can prevent other classes who use the singleton from calling the displayRootView() method?

I already considered StackTrace, but that doesn't seem to be ideal. I though maybe by using a tagging interface on the StartUp class to seperate it from the rest?

Any suggestions would be greatly appreciated.

解决方案

Uh, this is difficult to prevent certain classes from calling your method, since it breaks some core OOP principles. The method shouldn't care who calls it. It's basic separation of concerns - your method should have a clear contract about what it does, not about what's the state of the JVM at the time.

Consider these questions:

  • What will happen if you subclass StartUp? For example to separate Desktop, Mobile and Web platforms?
  • How will you unit test this method without involving StartUp?
  • What happens if you need another layer of abstraction there?
  • What about if and when you add proxies (Via AOP or Spring proxies)?
  • What happens if you need to call the method from a Timer? It's still going to be called from StartUp class source (and be correct), but it won't appear in the stack trace.

And other such considerations.

Throwing some kind of exception (like IllegalStateException, or a custom one), in case the method is called a second time is absolutely valid IMHO.

This looks like you may want static checks on your code, not in-code or runtime checks. I don't think it would be terribly difficult to add a custom rule to Findbugz or PMD to find all direct invocations of a method and check the calling class (and fail the build if it's called from other places). But I don't think such a check is actually useful.

In the end, there are a lot more chances that you will need a legit usage of the method outside the said class, than there is that someone will accidentally use it incorrectly after they have been warned and appropriate Javadoc has been created.

这篇关于Java(FX) - 只允许1个类从单例类调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆