设为首页 加入收藏

TOP

空对象模式---函数需要返回一个无意义的对象或者对象引用的探讨
2014-11-23 22:53:54 来源: 作者: 【 】 浏览:3
Tags:对象 模式 --- 函数 需要 返回 一个 意义 或者 引用 探讨

最近在程序时,遇到一种情况,以前也有遇到过,不过采用另外一种方法绕过了。具体问题如下:

const Layer& Frame::GetLayer(uint32 layerIndex) const
{
if(CheckLayerIndexValid(layerIndex))
{
return m_layers[layerIndex];
}

return ; // what how
}


在这种情况下,我们可以改为返回指针:

const Layer* Frame::GetLayer(uint32 layerIndex) const
{
if(CheckLayerIndexValid(layerIndex))
{
return m_layers[layerIndex];
}

return 0;
}

或者作为参数返回:

bool Frame::GetLayer(uint32 layerIndex, Layer &layer) const
{
if(CheckLayerIndexValid(layerIndex))
{
layer = m_layers[layerIndex];
return true;
}

return false;
}

但如果我们想保持第一种函数形式,只是想返回对象的引用怎么办?
我想到空对象模式,当需要一个无意义的对象时,可以返回事先确定好的空对象,类似代码如下:

#include

template
class Nullable
{
protected:
Nullable(){ }
virtual ~Nullable(){ }

public:
static boost::shared_ptr& NullPtr()
{
return ms_nullPtr;
}

bool IsNull()
{
return this == ms_null;
}

private:
static T* Null()
{
if(0 == ms_null)
{
ms_null = new T;
}

return ms_null;
}

private:
static T *ms_null;
static boost::shared_ptr ms_nullPtr;
};

template
T* Nullable::ms_null = 0;
template
boost::shared_ptr Nullable::ms_nullPtr(Nullable::Null());


class Layer : public Nullable
{
public:
Layer(){ }
~Layer(){ }
public:
int m_id;
};

当在上述情况时,我们可以返回 *Layer::NullPtr(),但这样就必须在获得函数返回值后检查是否IsNull()以确定该对象是否有意义,略显麻烦。而且在返回空对象而非引用时,

IsNull()也不再有效。

我的例子举得不是太合适,现在这个例子layerIndex确实应该有效,但如果:某个Manager存了一系列以id为键值的layer,需要根据id查找相应的layer,此时查不到也是正常的,此时不应该是错误或异常。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1029 Ignatius and the Princ.. 下一篇HDU 1280 前m大的数

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: