执行函数调用顺序 [英] enforcing function calling order

查看:77
本文介绍了执行函数调用顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


只是想知道是否有一些标准的方法来实现调用函数调用中的订单
。初始化一些物体时通常需要



例如


A级

{

void setFunc1(int a);

void setFunc2(int a);

void setFunc3(int a);
强制执行:

setFunc1" before before" setFunc2" before before" setFunc3


快速浏览某种形式的解决方案,在内部存储一些标志

并提出异常。但有没有人试过

一些特别优雅的解决方案达到了同样的目的?


提前致谢,

ddtbhai

解决方案

dd ***** @ gmail。 com 写道:


大家好,


只是想知道是否有一些标准 '调用函数调用函数时执行订单的方法。初始化一些物体时通常需要



例如


A级

{

void setFunc1(int a);

void setFunc2(int a);

void setFunc3(int a);
强制执行:

setFunc1" before before" setFunc2" before before" setFunc3


快速浏览某种形式的解决方案,在内部存储一些标志

并提出异常。但有没有人试过

一些特别优雅的解决方案,达到了同样的目的?



为什么你不能在构造函数中初始化?将构造函数抛出后会更好,并且永远不会有无效对象的机会

浮动(参见 http://www.artima.com/intv/goldilocks3.html)


干杯! --M


dd ***** @ gmail。 com 写道:


大家好,


只是想知道是否有一些标准 '调用函数调用函数时执行订单的方法。初始化一些物体时通常需要



例如


A级

{

void setFunc1(int a);

void setFunc2(int a);

void setFunc3(int a);
强制执行:

setFunc1" before before" setFunc2" before before" setFunc3



如果有一些原因导致无法从

构造函数调用这些例程 - 那么下一个最佳解决方案是将所有三个私有

然后声明一个公共函数调用预期中的三个私有

方法。订单。


毕竟,如果对A类的客户来说不明显。哪个方法

必须在另一个方法之前调用,那么应该没有要求

必须在另一个之前调用。毕竟,客户如何知道所需订单是否存在?任何

程序员在他们的程序中应该想要的最后一件事就是一组不成文的

要求,每个工作代码的人都只需要

知道。


Greg




Greg写道:

dd*****@gmail.com 写道:
< blockquote class =post_quotes>
大家好,


只是想知道是否有一些标准方法来执行
强制执行调用函数的顺序。初始化一些物体时通常需要



例如


A级

{

void setFunc1(int a);

void setFunc2(int a);

void setFunc3(int a);
强制执行:

setFunc1" before before" setFunc2" before before" setFunc3



如果有一些原因导致无法从

构造函数调用这些例程 - 那么下一个最佳解决方案是将所有三个私有

然后声明一个公共函数调用预期中的三个私有

方法。订单。


毕竟,如果对A类的客户来说不明显。哪个方法

必须在另一个方法之前调用,那么应该没有要求

必须在另一个之前调用。毕竟,客户如何知道所需订单是否存在?任何

程序员在他们的程序中应该想要的最后一件事就是一组不成文的

要求,每个工作代码的人都只需要

知道。


Greg



嗨Greg,M,


我试图避免的*主要*问题是Greg提到的你只知道和b $ b综合症。在我的例子中,每个函数都有一个

参数,但这也可以是任意长的。假设每个集合

函数各有6个参数,那么一个函数可以命令

内部3个调用将最终得到18个参数。


虽然没有什么不对的,但它肯定不是很好的b $ b美元!


这就是为什么我在寻找一种可以保证订单的方法

的调用,而几乎没有被班级的最终用户注意到。


Best,

ddtbhai


Hello folks,

Just wanted to know if there are some ''standard'' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

At a quick glance some form of solution that stores some flags
internally and raises exceptions come to mind. But has anyone tried
some particularly elegant solution that achieves the same ends?

Thanks in advance,
ddtbhai

解决方案

dd*****@gmail.com wrote:

Hello folks,

Just wanted to know if there are some ''standard'' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

At a quick glance some form of solution that stores some flags
internally and raises exceptions come to mind. But has anyone tried
some particularly elegant solution that achieves the same ends?

Why can''t you do initialize in the constructor? It''s far better to have
the constructor throw and never have the chance of an invalid object
floating around (cf. http://www.artima.com/intv/goldilocks3.html).

Cheers! --M


dd*****@gmail.com wrote:

Hello folks,

Just wanted to know if there are some ''standard'' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

If there is some reason why these routines cannot be called from the
constructor - then the next best solution is to make all three private
and then declare one public function that calls the three private
methods in the "expected" order.

After all, if it is not obvious to the client of class "A" which method
has to be called before another, then there should be no requirement
that one has to be called before another. After all, how is the client
expected to know that a required order exists? The last thing any
programmer should want in their program is a set of unwritten
requirements that everyone working on the code is just expected to
"know".

Greg



Greg wrote:

dd*****@gmail.com wrote:

Hello folks,

Just wanted to know if there are some ''standard'' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3


If there is some reason why these routines cannot be called from the
constructor - then the next best solution is to make all three private
and then declare one public function that calls the three private
methods in the "expected" order.

After all, if it is not obvious to the client of class "A" which method
has to be called before another, then there should be no requirement
that one has to be called before another. After all, how is the client
expected to know that a required order exists? The last thing any
programmer should want in their program is a set of unwritten
requirements that everyone working on the code is just expected to
"know".

Greg

Hi Greg, M,

The *main* issue that i was trying to avoid is the "you just know"
syndrome as Greg mentioned. In my example, each function has a single
argument, but this can also be arbitrarily long. Say each of the set
functions have 6 arguments each, then a single function that can order
the 3 invocations internally will end up with 18 arguments.

While there is nothing really wrong about that, it certainly isn''t
pretty !

Which is why i was looking for an approach which can guarantee an order
of invocation, while barely being noticed by the end user of the class.

Best,
ddtbhai


这篇关于执行函数调用顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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