请帮助Singleton。 [英] Singleton help please.

查看:89
本文介绍了请帮助Singleton。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个单身,因为它似乎是我问题的最佳解决方案。


我有一个我想要的对象在程序启动时初始化

然后我只想把它称为静态函数....所以我想

创建该对象的实例。我需要将参数发送到

在单例中正确初始化我的对象。这就是我开始遇到问题的原因。我开始怀疑这是不好的风格。


这是最好的方式吗?


V.

解决方案


我正在尝试建立一个单身人士,因为它似乎是我最好的解决方案问题。

我有一个对象,我想在程序启动时初始化
然后我只想把它称为静态函数....所以我想
创建该对象的实例。我需要发送参数以便在单例中正确初始化我的对象。这就是我开始遇到问题的地方。我开始怀疑这是不好的风格。




Singleton ==总是相同的对象,总是相同的实例。如果你想要

那么,使用两种静态方法:


====================


公共类SingleClass {


私有SingleClass实例;


public static SingleClass InitObject(< ;参数>){

if(this.instance == null)

this.instance = new SingleClass(....);


返回this.instance

}


public static SingleClass GetInstance(){

if(this.instance != null)

返回this.instance;

else

返回null;

}


private SingleClass(...){


// Init

}

}


======================


问候,


Frank Eller
www.frankeller.de


VidalSasoon< vi ********* @bootbox.net>写道:

我正在尝试设置一个单例,因为它似乎是我问题的最佳解决方案。

我有一个我想要的对象在程序启动时初始化
然后我只想把它称为静态函数....所以我想创建该对象的实例。我需要发送参数以便在单例中正确初始化我的对象。这就是我开始遇到问题的地方。我开始怀疑这是不好的风格。

这是最好的方式吗?




嗯,你可以适应正常的单身人士模式有一个方法

来设置实例(带参数),以及一个返回

实例的属性,如果它已被设置并抛出异常,否则。


(如果调用两次,set方法会抛出异常。)


为了真正的线程安全,你应该确保'这是一个记忆障碍

在套装和每次获得之间,但是如果没有

,你很可能会很好。如果你想要它,只需使用一个简单的锁定设置

和get就可以很好地完成这个操作,并且不会花太多钱,除非

你''获取实例*加载*次。


另一种选择是使用正常的单例模式(参见
http://www.pobox.com/~skeet/csharp/singleton.html )但是

初始化程序从任何需要的地方获取参数。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


Frank Eller [MVP]< fe **** **@frankeller.de>写道:

我有一个对象,我想在程序启动时初始化
然后我只想把它称为静态函数...所以我想创建该对象的实例。我需要发送参数以便在单例中正确初始化我的对象。这就是我开始遇到问题的地方。我开始怀疑这是不好的风格。
Singleton ==总是相同的对象,总是相同的实例。如果您想要
,请使用两种静态方法:




< snip>

public static SingleClass GetInstance(){
if(this.instance!= null)
返回this.instance;

返回null;
}




这不会编译,因为这个在静态方法中不存在。即使

它确实如此,if子句有点多余,不是吗?以上是

完全相当于


public static SingleClass GetInstance()

{

return this.instance;

}


(当然还没编译)。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件


I am trying to set up a singleton since it seems like the best
solution to my problem.

I have a an object that I want to initialize when the program starts
then I just want to call it like a static function.... so i want to
create instances of that object. I need to send parameters to
initialize my object properly in the singleton. that is where i am
starting to have problems. I starting to wonder if this is bad style.

is this the bast way to go?

V.

解决方案

Hi,

I am trying to set up a singleton since it seems like the best
solution to my problem.

I have a an object that I want to initialize when the program starts
then I just want to call it like a static function.... so i want to
create instances of that object. I need to send parameters to
initialize my object properly in the singleton. that is where i am
starting to have problems. I starting to wonder if this is bad style.



Singleton == always the same object, always the same instance. If you want
that, use two static methods:

====================

public class SingleClass {

private SingleClass instance;

public static SingleClass InitObject( <parameters> ) {
if ( this.instance == null )
this.instance = new SingleClass( .... );

return this.instance
}

public static SingleClass GetInstance() {
if ( this.instance != null )
return this.instance;
else
return null;
}

private SingleClass( ... ) {

// Init
}
}

======================

Regards,

Frank Eller
www.frankeller.de


VidalSasoon <vi*********@bootbox.net> wrote:

I am trying to set up a singleton since it seems like the best
solution to my problem.

I have a an object that I want to initialize when the program starts
then I just want to call it like a static function.... so i want to
create instances of that object. I need to send parameters to
initialize my object properly in the singleton. that is where i am
starting to have problems. I starting to wonder if this is bad style.

is this the bast way to go?



Well, you could adapt the normal singleton pattern by having a method
to set the instance (with parameters), and a property which returns the
instance if it''s been set up and throws an exception otherwise.

(The set method would throw an exception if called twice.)

For true thread-safety, you should make sure there''s a memory barrier
between the set and every get, but chances are you''ll be fine without
it. If you want it though, just using a simple lock for both the set
and the get would do the trick nicely, and won''t cost too much unless
you''re fetching the instance *loads* of times.

Another alternative is to use the normal singleton pattern (see
http://www.pobox.com/~skeet/csharp/singleton.html) but make the
initializer fetch the parameters from wherever it needs to.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Frank Eller [MVP] <fe******@frankeller.de> wrote:

I have a an object that I want to initialize when the program starts
then I just want to call it like a static function.... so i want to
create instances of that object. I need to send parameters to
initialize my object properly in the singleton. that is where i am
starting to have problems. I starting to wonder if this is bad style.
Singleton == always the same object, always the same instance. If you want
that, use two static methods:



<snip>
public static SingleClass GetInstance() {
if ( this.instance != null )
return this.instance;
else
return null;
}



This won''t compile, as "this" doesn''t exist in a static method. Even if
it did, the if clause is somewhat redundant, is it not? The above is
exactly equivalent to

public static SingleClass GetInstance()
{
return this.instance;
}

(which still doesn''t compile, of course).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于请帮助Singleton。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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