单例类如何使用接口? [英] How can a singleton class use an interface?
本文介绍了单例类如何使用接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在许多地方读过单身人士可以使用的界面。一些我怎么也无法理解这一点。
I read at many places that singletons can use interfaces. Some how I am unable to comprehend this.
推荐答案
每个类都可以实现一个接口,而Singleton只是一个正常确保除了它可能实现的其他业务逻辑之外的任何时间点只存在其中一个实例的类。
这也意味着Singleton至少有2个责任,这不是好的OO设计,因为班级应该只有1个责任并确保他们擅长这个责任,但这是另一个讨论。
Every class can implement an interface, and a Singleton is just a "normal" class that makes sure that only one instance of it exists at any point in time apart from the other business logic it may implement. This also means that a Singleton has at least 2 responsibities and this is not good OO design as classes should only have 1 responsibility and make sure they are good at that responsibility, but that is another discussion.
这篇关于单例类如何使用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文