单个对象实例技术 [英] Single Object Instance Techniques

查看:47
本文介绍了单个对象实例技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会有人写:


class SomeThing //类中充斥着非域单实例代码:(

{

私人:

SomeThing();

静态SomeThing * pInstance_;

公开:


static SomeThing * getInstance()

{

if(!pInstance_)

{

pInstance_ = new SomeThing();

}

返回pInstance_;

}

};

SomeThing * SomeThing :: pInstance_ = 0;


而不是:


类SomeThing //类不受单实例问题的影响: )

{

public:

SomeThing();

};


SomeThing * GetSomeThing()//单实例存取函数

{

静态SomeThing * thing = new SomeThing();

返回东西;

}


???


这是因为第二个例子只能解决问题但是没有那个人不会在其他地方打电话给构造函数的b $ b那就不是了。

单实例技术有助于解决初始化顺序问题,而第二种形式对于它来说非常有用。我不认为单实例控件

属于域类。 ''SingleInstancer''应该是模板类。

你有什么看法?


John

Why would anyone write:

class SomeThing // class littered with non-domain single-instancing code :(
{
private:
SomeThing();
static SomeThing* pInstance_;
public:

static SomeThing* getInstance()
{
if(!pInstance_)
{
pInstance_ = new SomeThing();
}
return pInstance_;
}
};
SomeThing* SomeThing::pInstance_ = 0;

instead of this:

class SomeThing // class unencumbered by single-instancing concerns :)
{
public:
SomeThing();
};

SomeThing* GetSomeThing() // single-instancing accessor function
{
static SomeThing* thing = new SomeThing();
return thing;
}

???

Is it because the second example only solves the problem but doesn''t ensure
that someone won''t call the constructor elsewhere? That''s it isn''t it.
Single-instance techniques help with the initialization order problem, and
the second form works great for that. I don''t think single instance control
belongs in a domain class. ''SingleInstancer'' should be a template class.
What''s your opinion?

John

推荐答案

没有优势。如果我看到那些代码,我猜有人来自一个面向对象语言的
,并且不知道直接的

命名空间。这显然只是试图只允许一个

类的实例,但它实际上并没有强制执行它。替代方案,可以使用仅具有静态成员函数的
的结构。


另一个缺点是它使类不可能

可以更快地分配到堆栈中。

There is no advantage. If I saw that code I would guess someone came
from an object oriented only language, and didn''t know of direct
namespaces. It''s clearly an attempt to allow only one instance of the
class, but it doesn''t actually enforce it. Alternative, a struct with
only static member functions could have been used.

Another disadvantage is that it makes it impossible for the class to
be allocated on the stack faster.


JohnQ写道:
JohnQ wrote:

为什么会不会有人写:


class SomeThing //类中充斥着非域单实例代码

:(

{

私人:

SomeThing();

静态SomeThing * pInstance_;
Why would anyone write:

class SomeThing // class littered with non-domain single-instancing code
:(
{
private:
SomeThing();
static SomeThing* pInstance_;



我没有手中的设计模式这本书。在Singleton下,上面的代码是否显示

,或者下面这个正确的代码?

I don''t have the book Design Patterns to hand. Under Singleton, does it show
the above code, or this correct code below?


SomeThing * GetSomeThing()//单实例访问函数

{

static SomeThing * thing = new SomeThing();

返回的东西;

}
SomeThing* GetSomeThing() // single-instancing accessor function
{
static SomeThing* thing = new SomeThing();
return thing;
}



如果你必须单身人士这样做。


然而,Singleton是一个非常可滥用的模式,它可以实现猖獗的
耦合。 实例化一次动机与

不一样,每个人都喜欢大胖子动机。后者是

反模式!

If you must Singleton, do it like that.

However, Singleton is a very abusable pattern when it enables rampant
coupling. The "instantiate once" motivation is not the same as the
"available to everyone like a big fat global" motivation. The latter is an
antipattern!


...''SingleInstancer''应该是模板类。
... ''SingleInstancer'' should be a template class.



这样的模板通常是与Singleton合作的结果。我不认为它解决了如何防止模板化类型的二次实例化问题。


更进一步,如果你有单元测试,你需要进行二次设置,那么你可以为你的测试假冒物品!


单身人士应该受到高度怀疑。


-

Phlip
http://www.greencheese.us/ZeekLand < - 不是博客!!!

Such a template is often the result of a junior effort with Singleton. I
don''t think it solves the problem how to prevent secondary instantiation of
the templated type.

Furtherless, if you have unit tests, you _need_ secondary instatiation, so
you can fake objects for your tests!

Singletons should be treated with high suspicion.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


单实例类与之相矛盾OOP的整个想法。

你实际上是在创建一个对象,并且使用对象语法,只是为了它的命名空间。我推荐使用一个实际命名空间和一个

程序范例。

Single-instance classes are contradictory to entire idea of OOP.
You''re essentially making an object, and using object syntax, just for
its namespace. I recommend instead using an actualy namespace and a
procedural paradigm.


这篇关于单个对象实例技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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