处理懒惰对象的好习惯是什么? [英] What is a good idiom for handling a lazy object?

查看:54
本文介绍了处理懒惰对象的好习惯是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理懒惰对象的好习惯是什么?我看到2个好的

的可能性。还有什么评论吗?这里的人们使用哪种方式?

(1)


class Thing {

public:

Thing(double x,double y):x(x),y​​(y),calculated(false){}

double operator()()const {

如果(!计算)计算();

返回z;

}

私人:

double x;

加倍;

可变bool计算;

可变双z;

void calculate(){z = X + Y; }

};

(2)


class回答{

虚拟〜答案() ;

};


类懒惰:公开回答{

公开:

懒惰(双x,双y):x(x),y​​(y){}

私人:

朋友类Eval;

double x;

加倍;

};


等级评估:公开回答{

public:

Eval(const Lazy& lazy):z(计算(懒惰))

double operator()()const {return z; } $ / $
私有:

double z;

static void calculate(const Lazy& lazy){return lazy.x + lazy.y; }

};


class Thing {

public:

Thing(double x,double) y):answer(new Lazy(x,y)){}

double operator()()const {

const Lazy * lazy = dynamic_cast< const Lazy *> ;(answer.get());

if(lazy){

answer.reset(new Eval(* lazy));

}

const Eval * eval = static_cast< const Eval *>(answer.get());

断言(eval);

return(* eval)();

}

private:

mutable boost :: shared_ptr< Answer>回答;

};

What is a good idiom for handling a lazy object? I see 2 good
possibilities. Any more, any comments? Which way do people here use?
(1)

class Thing {
public:
Thing(double x, double y) : x(x), y(y), calculated(false) { }
double operator()() const {
if (!calculated) calculate();
return z;
}
private:
double x;
double y;
mutable bool calculated;
mutable double z;
void calculate() { z = x+y; }
};
(2)

class Answer {
virtual ~Answer();
};

class Lazy : public Answer {
public:
Lazy(double x, double y) : x(x), y(y) { }
private:
friend class Eval;
double x;
double y;
};

class Eval : public Answer {
public:
Eval(const Lazy& lazy) : z(calculate(lazy))
double operator()() const { return z; }
private:
double z;
static void calculate(const Lazy& lazy) { return lazy.x+lazy.y; }
};

class Thing {
public:
Thing(double x, double y) : answer(new Lazy(x, y)) { }
double operator()() const {
const Lazy * lazy = dynamic_cast<const Lazy *>(answer.get());
if (lazy) {
answer.reset(new Eval(*lazy));
}
const Eval * eval = static_cast<const Eval *>(answer.get());
assert(eval);
return (*eval)();
}
private:
mutable boost::shared_ptr<Answer> answer;
};

推荐答案

Siemel Naran写道:
Siemel Naran wrote:
什么处理懒惰对象是一个很好的习惯用法?我看到了两个很好的可能性。还有什么评论吗?这里有人用哪种方式?

(1)

类Thing {
公开:
东西(双x,双y):x( x),y(y),计算(假){}
double operator()()const {
if(!calculated)calculate();
return z;
私有:
双x;
双y;
可变bool计算;
可变双z;
void calculate(){z = x + Ÿ; }
};

(2)

类答案{
虚拟〜答案();
};
<懒惰:公开回答{
公开:
懒惰(双x,双y):x(x),y​​(y){}
私人:
朋友class Eval;
double x;
double y;
};

class Eval:public回答{
public:
Eval(const懒惰和懒惰):z(计算(懒惰))
double operator()()const {return z;私有:
双z;
静态void计算(const懒惰和懒惰){return lazy.x + lazy.y; }
};

类Thing {
public:
Thing(double x,double y):answer(new Lazy(x,y)){}
double operator()()const {
const Lazy * lazy = dynamic_cast< const Lazy *>(answer.get());
if(lazy){
回答。 reset(new Eval(* lazy));
}
const Eval * eval = static_cast< const Eval *>(answer.get());
断言(eval);
return(* eval)();
}
私有:
可变的boost :: shared_ptr<答案>回答;
};
What is a good idiom for handling a lazy object? I see 2 good
possibilities. Any more, any comments? Which way do people here use?
(1)

class Thing {
public:
Thing(double x, double y) : x(x), y(y), calculated(false) { }
double operator()() const {
if (!calculated) calculate();
return z;
}
private:
double x;
double y;
mutable bool calculated;
mutable double z;
void calculate() { z = x+y; }
};
(2)

class Answer {
virtual ~Answer();
};

class Lazy : public Answer {
public:
Lazy(double x, double y) : x(x), y(y) { }
private:
friend class Eval;
double x;
double y;
};

class Eval : public Answer {
public:
Eval(const Lazy& lazy) : z(calculate(lazy))
double operator()() const { return z; }
private:
double z;
static void calculate(const Lazy& lazy) { return lazy.x+lazy.y; }
};

class Thing {
public:
Thing(double x, double y) : answer(new Lazy(x, y)) { }
double operator()() const {
const Lazy * lazy = dynamic_cast<const Lazy *>(answer.get());
if (lazy) {
answer.reset(new Eval(*lazy));
}
const Eval * eval = static_cast<const Eval *>(answer.get());
assert(eval);
return (*eval)();
}
private:
mutable boost::shared_ptr<Answer> answer;
};




我只想知道看过这篇文章。我这是一个很好的问题,但是我需要更多的背景知识。我在lazy

对象上做了一个谷歌,发现了这个: http://c2.com/cgi/wiki?LazyObject

我知道Stroustrup在TC ++ PL(SE)中涵盖类似于此的内容,我希望

能够很快重新审视这个话题。它可能会帮助我评估你的

代码,如果你用自己的语言解释一下你的设计是什么
目标是什么。

- -

STH

哈顿定律:只有一个不可侵犯的法律

KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com

Mozilla: http://www.mozilla.org



I just want to acknowledge having looked at this post. I this is a good
question, but my requier a bit more background. I did a google on lazy
object, and found this: http://c2.com/cgi/wiki?LazyObject

I know Stroustrup covers something similar to this in TC++PL(SE), I hope to
be able to revisit the topic soon. It would probably help me evaluate your
code if you explained just a bit more in your own words what your design
objectives are.
--
STH
Hatton''s Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org


" Siemel Naran" <硅********* @ REMOVE.att.net>写道...
"Siemel Naran" <Si*********@REMOVE.att.net> wrote...
处理懒惰对象的好习惯是什么?我看到了两个很好的可能性。还有什么评论吗?这里有人用哪种方式?

(1)

类Thing {
公开:
东西(双x,双y):x( x),y(y),计算(假){}
double operator()()const {
if(!calculated)calculate();
return z;
私有:
双x;
双y;
可变bool计算;
可变双z;
void calculate(){z = x + Ÿ; }


void calculate()const {z = x + y; calculated = true; }

};

(2)

类答案{
虚拟〜答案();
}; <懒惰:公开回答{
公开:
懒惰(双x,双y):x(x),y​​(y){}
私人:<友情课Eval;
双x;
双y;
};

类Eval:public回答{
public:
Eval(const Lazy& lazy):z(calculate(lazy))
double operator()()const {return z;私有:
双z;
静态void计算(const懒惰和懒惰){return lazy.x + lazy.y; }
};

类Thing {
public:
Thing(double x,double y):answer(new Lazy(x,y)){}
double operator()()const {
const Lazy * lazy = dynamic_cast< const Lazy *>(answer.get());
if(lazy){
回答。 reset(new Eval(* lazy));
}
const Eval * eval = static_cast< const Eval *>(answer.get());
断言(eval);
return(* eval)();
}
私有:
可变的boost :: shared_ptr<答案>回答;
};
What is a good idiom for handling a lazy object? I see 2 good
possibilities. Any more, any comments? Which way do people here use?
(1)

class Thing {
public:
Thing(double x, double y) : x(x), y(y), calculated(false) { }
double operator()() const {
if (!calculated) calculate();
return z;
}
private:
double x;
double y;
mutable bool calculated;
mutable double z;
void calculate() { z = x+y; }
void calculate() const { z = x+y; calculated = true; }
};
(2)

class Answer {
virtual ~Answer();
};

class Lazy : public Answer {
public:
Lazy(double x, double y) : x(x), y(y) { }
private:
friend class Eval;
double x;
double y;
};

class Eval : public Answer {
public:
Eval(const Lazy& lazy) : z(calculate(lazy))
double operator()() const { return z; }
private:
double z;
static void calculate(const Lazy& lazy) { return lazy.x+lazy.y; }
};

class Thing {
public:
Thing(double x, double y) : answer(new Lazy(x, y)) { }
double operator()() const {
const Lazy * lazy = dynamic_cast<const Lazy *>(answer.get());
if (lazy) {
answer.reset(new Eval(*lazy));
}
const Eval * eval = static_cast<const Eval *>(answer.get());
assert(eval);
return (*eval)();
}
private:
mutable boost::shared_ptr<Answer> answer;
};




我不知道为什么你需要做这么长时间。此外,

我总是被告知,如果你需要使用dynamic_cast,你应该接近你的设计,它可能有太多的相互依赖。


Victor



I am not sure why you need to make such a long way around it. Besides,
I was always told that if you need to use dynamic_cast, you should re-
approach your design, it may have too many interdependencies.

Victor


" Steven T. Hatton" <苏****** @ setidava.kushan.aa>在消息新闻中写道:28-
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message news:28-
我只是想知道看过这篇文章。我这是一个很好的问题,但我需要更多的背景。我在lazy
对象上做了一个谷歌,发现了这个: http:// c2.com/cgi/wiki?LazyObject

对,这很好地描述了它。

我知道Stroustrup在TC ++ PL中涵盖类似的内容SE),我希望
能够很快重新审视这个话题。如果你用你自己的语言解释你的设计目标是什么,它可能会帮助我评估你的代码。
I just want to acknowledge having looked at this post. I this is a good
question, but my requier a bit more background. I did a google on lazy
object, and found this: http://c2.com/cgi/wiki?LazyObject
Right, that describes it well.
I know Stroustrup covers something similar to this in TC++PL(SE), I hope to be able to revisit the topic soon. It would probably help me evaluate your
code if you explained just a bit more in your own words what your design
objectives are.




我创建了一个Select对象。选项1是为构造函数打开

SQL连接,运行查询等。选项2是为构造函数只需要存储函数参数的
。只有当我们开始使用

选择:: for_each或其他什么时,才打开连接,进行查询等。

这样我们只在真正打开连接时需要它。另外,我们

最小化连接数,因为我们不想临时选择

对象,例如从函数返回的对象以打开连接。


我写的另一个例子,


类收入

{

public:

...


double total()const

{

if(!m_hastotal)

{

m_total =

d_salary

+ d_interest + d_dividends

+ d_shortterm + d_longterm

;

m_hastotal = true;

}

返回m_total;

}

私人:

双d_salary;

双d_interest;

双d_dividends;

double d_shortterm;

double d_longterm;

mutable bool m_hastotal;

mutable double m_total;

这样一来第二次调用income.total()不需要做任何

计算。并不是说增加5个双打也是一个大问题,但是我只是在这里玩耍。



I have created a Select object. Option 1 is for the constructor to open the
SQL connection, run the query, etc. Option 2 is for the constructor to just
store the function argument parameters. Only when we start iterating with
Select::for_each or whatever, then open the connection, do the query, etc.
This way we only open the connection when we really need it. Also, we
mininimize the number of connections, because we don''t want temporary Select
objects such as those returned from functions to open a connection.

As another example I have written,

class Income
{
public:
...

double total() const
{
if (!m_hastotal)
{
m_total=
d_salary
+d_interest+d_dividends
+d_shortterm+d_longterm
;
m_hastotal=true;
}
return m_total;
}
private:
double d_salary;
double d_interest;
double d_dividends;
double d_shortterm;
double d_longterm;
mutable bool m_hastotal;
mutable double m_total;
This way a second call to income.total() doesn''t have to do any
calculations. Not that adding 5 doubles is a big deal anyway, but I was
just playing around here.


这篇关于处理懒惰对象的好习惯是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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