0
我想在我的应用程序中只使用超图的一个实例,以便我无法访问它并从我的应用程序中的任何类(添加顶点,添加hyperEdge等)中对其进行变形。
有可能吗?
我尝试使用单例模式,但据我所知在this question这不是一个好的选择。
我想在我的应用程序中只使用超图的一个实例,以便我无法访问它并从我的应用程序中的任何类(添加顶点,添加hyperEdge等)中对其进行变形。
有可能吗?
我尝试使用单例模式,但据我所知在this question这不是一个好的选择。
这个答案是相当不错的enter link description here,我可能会赞成的接口和实现,但你仍然可以使用单例,例如:
public interface GraphSource {
public Graph getGraph();
}
public enum DefaultGraph implements GraphSource {
INSTANCE;
private Graph<String,String> graph;
{
graph = new SparseGraph<String,String>();
}
@SuppressWarnings("rawtypes")
@Override
public Graph getGraph() {
return graph;
}
}
public class MyClass {
public MyClass(GraphSource source) {
Graph myGraph = source.getGraph();
}
}
但我会避免:
public class MyClass {
GraphSource source = DefaultGraph.INSTANCE;
public MyClass(GraphSource source) {
Graph myGraph = source.getGraph();
}
}
因为它很难残缺或模拟图形进行测试。对于大型应用程序,我会考虑使用Inversion of Control,但如果您不熟悉该应用程序,这对于小型应用程序来说可能是过度的。
为什么辛格尔顿不是这个的好选择?你能否更新链接? – CodeBlue 2013-04-25 17:17:00
这不是说Singleton本身就是一个不好的选择 - 它代表的是什么。在整个应用程序中都有可访问的概念意味着它本质上是一个全局变量。当出现问题并且你必须调试你的程序时,你现在必须检查你的程序的_all_,因为任何类都可能修改了你的图。您可能想重新考虑程序的每个部分实际上是否需要访问您的图形。 – sdasdadas 2013-05-08 13:59:32