如何在不构造对象的情况下使用Facade.Instance方法?
我最近刚刚完成了一个关于软件模式的单元,现在正在尝试理解 PureMVC 框架。然而,有一件事让我难住了,这对这里的专家来说很简单。
我正在尝试创建单例 Facade 类的实例。在构造函数中,注释指出:
这个IFacade实现是一个Singleton,所以你不应该直接调用构造函数,而应该调用静态Singleton Factory方法Facade.Instance
当Facade对象还没有被创建时,你怎么能调用实例方法呢?
Facade.Instance 方法如下所示:
public static IFacade Instance
{
get
{
if (m_instance == null)
{
lock (m_staticSyncRoot)
{
if (m_instance == null) m_instance = new Facade();
}
}
return m_instance;
}
}
I only recently completed a unit on software patterns and am now attempting to comprehend the PureMVC framework. One thing has got my stumped however, something which is simple to the gurus here.
I'm attempting to create an instance of the singleton Facade class. In the constructor, the comments state:
This IFacade implementation is a Singleton, so you should not call the constructor directly, but instead call the static Singleton Factory method Facade.Instance
How can you call the instance method when the Facade object has not even been created?
The Facade.Instance method looks like this:
public static IFacade Instance
{
get
{
if (m_instance == null)
{
lock (m_staticSyncRoot)
{
if (m_instance == null) m_instance = new Facade();
}
}
return m_instance;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在访问静态属性。静态属性是类定义的一部分,而不是类实例的一部分。要访问静态成员(属性、字段、方法),只需使用类名点成员:
You are accessing a static property. Static properties are part of the class definition, not class instances. To access a static member (property, field, method), simply use the class name dot member: