静态类与单身人士(第2页) [英] Static classes vs Singletons (pt 2)

查看:78
本文介绍了静态类与单身人士(第2页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继其他讨论之后,我只需要检查一下

,并参考处理静态字段中的资源。


我有一个全球可访问的持久性框架。在Delphi中,我
将使用一类静态方法来强制执行单例,并且我添加了

静态字段来保存数据库连接等内容。


这在Delphi中运行良好,因为我们有单元初始化/定稿

部分可以作为静态构造函数/析构函数,并且

确定性定稿,我们可以模拟在

定稿静态析构函数和清理资源存在于应用程序关闭。


现在,铭记,如果我使用''正确''单身模式,我是

总是通过静态方法访问静态字段,我看不出任何

使用我自己的类之间的区别静态方法访问静态

字段,并使用Singleton模式。


我的问题是,静态字段指向的实例是否得到

垃圾收集?


我一直认为是垃圾这发生在整理代码的一部分时,

在应用程序退出时执行。


Joanna


-

Joanna Carter

顾问软件工程师

解决方案



现在,请记住,如果我使用''正确的''Singleton模式,我总是通过静态方法访问静态字段,我可以我看到使用我自己的静态方法来访问
静态
字段和使用Singleton模式之间的区别。


即使它们看起来很相似也不一样,如果你使用静态

类你不创建类的实例,例如,你不能将
传递给该类的实例作为方法的参数。


使用单例创建一个实例,之后有没有

它和另一个不是单身的另一个类的实例之间的区别。唯一的区别是单身人士班没有公共

construtor你可以使用

静态方法/财产100%控制施工过程。

你也可以轻易地摧毁&随意重新创建实例:


//不是最好的方法来实现一个单

类单

{

静态单件theInstance = null;


公共单件GetIt

{

get

{

if(theInstance == null)

theInstance = new singleton();

返回实例;

}


//移除实例,当再次调用该属性时,它将是

re-instanciated

public void重置()

{

theInstance = null;

}


}


你不能用静态类做到这一点

我的问题是,静态字段指向的实例是否得到垃圾收集?




所有你要做的就是将它指定为null:


static DataSet ds = new DataSet();

>
void清洁()

{

ds = null;

}

$ b $烦明智之后,它将被保留,直到AppDomain处于活动状态。


干杯,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局




现在,请记住,如果我使用''正确的''Singleton模式,我总是通过静态访问静态字段方法,我看不出使用我自己的静态方法来访问
静态
字段和使用Singleton模式之间的区别。


即使它们看起来很相似也不一样,如果你使用静态

类你不创建类的实例,例如,你不能将
传递给该类的实例作为方法的参数。


使用单例创建一个实例,之后有没有

它和另一个不是单身的另一个类的实例之间的区别。唯一的区别是单身人士班没有公共

construtor你可以使用

静态方法/财产100%控制施工过程。

你也可以轻易地摧毁&随意重新创建实例:


//不是最好的方法来实现一个单

类单

{

静态单件theInstance = null;


公共单件GetIt

{

get

{

if(theInstance == null)

theInstance = new singleton();

返回实例;

}


//移除实例,当再次调用该属性时,它将是

re-instanciated

public void重置()

{

theInstance = null;

}


}


你不能用静态类做到这一点

我的问题是,静态字段指向的实例是否得到垃圾收集?




所有你要做的就是将它指定为null:


static DataSet ds = new DataSet();

>
void清洁()

{

ds = null;

}

$ b $烦明智之后,它将被保留,直到AppDomain处于活动状态。


干杯,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


Ignacio Machin(.NET / C#MVP)" < ignacio.machin AT dot.state.fl.us> a
$ b $bécritdansle message de news:#o ************** @ TK2MSFTNGP12.phx.gbl ...

//取出例如,当再次调用的属性将是
重新实例化
公共无效复位()
{
theInstance = NULL;
}


你不能用静态类来做那个


我不想用静态类做那个我请记住,他们应该在应用程序运行的任何时候都可以使用

我的问题是,做静态字段指向的实例是否收集了垃圾?



所有你要做的就是将它指定为null:

静态DataSet ds = new DataSet();

void Clean()
{
ds = null;
}




在这种情况下,我也可以在课堂上调用静态方法来释放

。静电场;效果是一样的。

我所要做的就是在

main()方法结束时调用''finaliser''静态方法应用程序。

否则将被保留,直到应用程序域被激活。


。如果,这样,你的意思是当程序退出,然后应该没有

问题。


我的问题是:如果你有静态字段需要处理资源

和那些字段旨在持续应用程序的整个运行,我是否需要

才能明确敲定它们或者应用程序关闭会为我做这个吗?


- -

Joanna Carter

顾问软件工程师


Following on from the other discussion, I have to just check something out
with reference to disposal of resources held in static fields.

I have a Persistence Framework that is ''globally accessible''. In Delphi, I
would use a class of static methods to enforce the singleton, and I added
static fields to hold things like the database connections, etc.

This worked fine in Delphi because we have unit initialisation/finalisation
sections that can act as static constructors/destructors, and with
deterministic finalisation, we could simulate a static destructor in the
finalisation and clean up resources there on application closedown.

Now, bearing in mind that if I use the ''proper'' Singleton pattern, I am
always accessing a static field through a static method, I can''t see any
difference between using my own class of static methods to access the static
fields, and using the Singleton pattern.

My question is, do the instances pointed to by static fields ever get
garbage collected ?

I have always assumed that this happened as part of the tidy-up code that
got executed when the application quit.

Joanna

--
Joanna Carter
Consultant Software Engineer

解决方案

Hi,

Now, bearing in mind that if I use the ''proper'' Singleton pattern, I am
always accessing a static field through a static method, I can''t see any
difference between using my own class of static methods to access the
static
fields, and using the Singleton pattern.
Hi, It''s not the same even as they looks much alike, if you use static
class you do not create an instance of the class , you cannot for example
pass around an instance of that class as a parameter to methods.

With a singleton you create an instance and after that there is no
difference between it and an instance of another class that is not
singleton. The only difference is that the singleton class has no public
construtor and you have 100% control of the construction process using the
static method/Property .
Also you could easily allow to destroy & recreate the instance at will:

// NOT THE BEST WAY TO IMPLEMENT a singleton
class singleton
{
static singleton theInstance = null;

public singleton GetIt
{
get
{
if ( theInstance == null )
theInstance = new singleton();
return theInstance;
}

//Remove the instance, when called again the property it will be
re-instanciated
public void Reset()
{
theInstance = null;
}

}

you cannot do that with a static class
My question is, do the instances pointed to by static fields ever get
garbage collected ?



All you have to do is assign it to null :

static DataSet ds = new DataSet();

void Clean()
{
ds = null;
}

Otherwise it will be kept until the AppDomain be active.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Hi,

Now, bearing in mind that if I use the ''proper'' Singleton pattern, I am
always accessing a static field through a static method, I can''t see any
difference between using my own class of static methods to access the
static
fields, and using the Singleton pattern.
Hi, It''s not the same even as they looks much alike, if you use static
class you do not create an instance of the class , you cannot for example
pass around an instance of that class as a parameter to methods.

With a singleton you create an instance and after that there is no
difference between it and an instance of another class that is not
singleton. The only difference is that the singleton class has no public
construtor and you have 100% control of the construction process using the
static method/Property .
Also you could easily allow to destroy & recreate the instance at will:

// NOT THE BEST WAY TO IMPLEMENT a singleton
class singleton
{
static singleton theInstance = null;

public singleton GetIt
{
get
{
if ( theInstance == null )
theInstance = new singleton();
return theInstance;
}

//Remove the instance, when called again the property it will be
re-instanciated
public void Reset()
{
theInstance = null;
}

}

you cannot do that with a static class
My question is, do the instances pointed to by static fields ever get
garbage collected ?



All you have to do is assign it to null :

static DataSet ds = new DataSet();

void Clean()
{
ds = null;
}

Otherwise it will be kept until the AppDomain be active.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> a
écrit dans le message de news: #o**************@TK2MSFTNGP12.phx.gbl...

//Remove the instance, when called again the property it will be
re-instanciated
public void Reset()
{
theInstance = null;
}

}

you cannot do that with a static class
I would not want to do that with the static classes I have in mind, they
should be available at all times that the app is running.

My question is, do the instances pointed to by static fields ever get
garbage collected ?



All you have to do is assign it to null :

static DataSet ds = new DataSet();

void Clean()
{
ds = null;
}



In that case, I could just as well call a static method on my class to free
off the connections held by the static fields; the effect would be the same.
All I have to do is to call a ''finaliser'' static method at the end of the
main() method of the app.
Otherwise it will be kept until the AppDomain be active.



If, by this, you mean when the application quits, then there should be no
problem.

My question was : If you have static fields that need Disposal of resources
and those fields are intended to last the entire run of the app, do I need
to explicitly finalise them or will the app closing do this for me ?

--
Joanna Carter
Consultant Software Engineer


这篇关于静态类与单身人士(第2页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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