Singleton类,使用另一个类? [英] Singleton class, use another class?

查看:63
本文介绍了Singleton类,使用另一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我创建了一个Singleton类,以便在我的移动应用程序中提供一些数据库功能。我有一个名为Utility的公共类,它对数据执行各种操作。在我的Singleton

类中使用实用程序类是否可以?这里有一些代码可以让我更清楚我想做什么 -


命名空间MyApp

{

class SingletonConnection

{

私有volatile静态SingletonConnection实例;


私有效用实用程序;


私有SingletonConnection()

{

}


公共静态SingletonConnection GetInstance()

{

if(instance == null)

{

instance = new SingletonConnection();

}

返回实例;

}


public void DoStuff()

{

utility = new Utility();


utility.DoStuff();

}

}

class Utility

{

public Utility()

{

}


public void DoStuff()

{


}

}

}


感谢您的任何提示,

Dav

解决方案

当然。为什么不行?

-


Chris Tacke,eMVP

加入嵌入式开发者社区
http://community.opennetcf.com

" ; davebythesea" < da ********** @ discuss.microsoft.com写信息

news:24 ****************** **************** @ microsof t.com ...





我创建了一个Singleton类,以便在我的移动应用程序中提供一些数据库功能。我有一个名为Utility的公共类,它对数据执行各种操作。可以在我的

Singleton

类中使用实用程序类吗?这里有一些代码可以让我更清楚我想做什么 -


命名空间MyApp

{

class SingletonConnection

{

私有volatile静态SingletonConnection实例;


私有效用实用程序;


私有SingletonConnection()

{

}


公共静态SingletonConnection GetInstance()

{

if(instance == null)

{

instance = new SingletonConnection();

}

返回实例;

}


public void DoStuff()

{

utility = new Utility();


utility.DoStuff();

}

}

class Utility

{

public Utility()

{

}


public void DoStuff()

{


}

}

}


感谢您的任何提示,

Dav



11月19日上午7:49,davebythesea

< davebythe ... @ discussion.microsoft.comwrote:





我创建了一个Singleton类,以便在我的移动应用程序中提供一些数据库功能。我有一个名为Utility的公共类,它对数据执行各种操作。在我的Singleton

类中使用实用程序类是否可以?这里有一些代码可以让我更清楚我想做什么 -


命名空间MyApp

{

class SingletonConnection

{

私有volatile静态SingletonConnection实例;


私有效用实用程序;


私有SingletonConnection()

{

}


公共静态SingletonConnection GetInstance()

{

if(instance == null)

{

instance = new SingletonConnection();

}

返回实例;

}


public void DoStuff()

{

utility = new Utility();


utility.DoStuff();

}

}


class Utility

{

公共效用()

{

}


public void DoStuff()

{


}

}


}


感谢您提供任何建议,

Dav



是的,可以使用Utility类(或其他任何类

重要)如果它有助于你实现单例。


如你所知,你的单例不是线程安全的。我将删除

volatile修饰符,并使用

锁定构造包装GetInstance方法的内容。更好的是,使用声明创建一个新的SingletonConnection

实例并使用GetInstance方法

只返回引用。或者,如果您不需要任何花哨的初始化或多态,那么只需创建一个静态类。


因为DoStuff()创建一个新的实例每一次,为什么不直接把这个作为局部变量?除非我在使用中遗漏了一些东西,否则这个

也可以像静态方法一样实现。这里有很多取决于实用程序本身是否是线程安全的
...


请注意,您的SingletonConnection不保证是单例。

首先,您可能想要密封它,然后第一次访问时有一个竞争条件

;如果两个线程通过空检查你可以获得
得到2.除非与初始化一个巨大的成本相关联$
SingletonConnection(ctor没有指出) ,那么也许

只需使用:


private static readonly SingletonConnection instance = new

SingletonConnection();

public static SingletonConnection实例{get {return instance;}}

当然,除非单例实现特定的基础/

界面,否则你也许只能用一个静态类来获得

静态方法(等等)。

我是否错过了重点?


Marc


Hi,

I have created a Singleton class to provide some database functionality in
my mobile application. I have a public class called Utility which performs
various operations on data. Is it ok to use the utility class in my Singleton
class? Here is some code to hopefully make it clearer what i want to do -

namespace MyApp
{
class SingletonConnection
{
private volatile static SingletonConnection instance;

private Utility utility;

private SingletonConnection()
{
}

public static SingletonConnection GetInstance()
{
if (instance == null)
{
instance = new SingletonConnection();
}
return instance;
}

public void DoStuff()
{
utility = new Utility();

utility.DoStuff();
}
}

class Utility
{
public Utility()
{
}

public void DoStuff()
{

}
}
}

Thanks for any tips,
Dav

解决方案

Sure. Why wouldn''t it be ok?
--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
"davebythesea" <da**********@discussions.microsoft.comwrote in message
news:24**********************************@microsof t.com...

Hi,

I have created a Singleton class to provide some database functionality in
my mobile application. I have a public class called Utility which performs
various operations on data. Is it ok to use the utility class in my
Singleton
class? Here is some code to hopefully make it clearer what i want to do -

namespace MyApp
{
class SingletonConnection
{
private volatile static SingletonConnection instance;

private Utility utility;

private SingletonConnection()
{
}

public static SingletonConnection GetInstance()
{
if (instance == null)
{
instance = new SingletonConnection();
}
return instance;
}

public void DoStuff()
{
utility = new Utility();

utility.DoStuff();
}
}

class Utility
{
public Utility()
{
}

public void DoStuff()
{

}
}
}

Thanks for any tips,
Dav



On Nov 19, 7:49 am, davebythesea
<davebythe...@discussions.microsoft.comwrote:

Hi,

I have created a Singleton class to provide some database functionality in
my mobile application. I have a public class called Utility which performs
various operations on data. Is it ok to use the utility class in my Singleton
class? Here is some code to hopefully make it clearer what i want to do -

namespace MyApp
{
class SingletonConnection
{
private volatile static SingletonConnection instance;

private Utility utility;

private SingletonConnection()
{
}

public static SingletonConnection GetInstance()
{
if (instance == null)
{
instance = new SingletonConnection();
}
return instance;
}

public void DoStuff()
{
utility = new Utility();

utility.DoStuff();
}
}

class Utility
{
public Utility()
{
}

public void DoStuff()
{

}
}

}

Thanks for any tips,
Dav

Yes, it is okay to use the Utility class (or any other class for that
matter) if it helps your implementation of the singleton.

As written your singleton is not thread-safe. I would remove the
volatile modifier and wrap the contents of the GetInstance method with
a lock construct. Better yet, create a new SingletonConnection
instance inline with the declaration and have the GetInstance method
just return the reference. Or if you don''t require any fancy
initialization or polymorphism then just create a static class.


Since DoStuff() creates a new instance each time, why not just hold
this as a local variable? Unless I missed something in the usage, this
could also be implemented just as a static method. A lot here depends
on whether utility is itself thread-safe...

Note that your SingletonConnection is not guaranteed to be singleton.
First, you might want to seal it, and second there is a race-condition
on the first access; if two threads get past the null check you could
get 2. Unless there is a huge cost associated with initializing a
SingletonConnection (which the ctor doesn''t indicate), then perhaps
just use:

private static readonly SingletonConnection instance = new
SingletonConnection();
public static SingletonConnection Instance {get {return instance;}}

Of course, unless the singleton is implementing a specific base /
interface, you might be able to get away with just a static class with
static methods (etc).
Did I miss the point?

Marc


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

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