我需要代表吗? [英] Do I need Delegates?

查看:86
本文介绍了我需要代表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计,


我对如何实施以下解决方案感到困惑。


我有一系列处理步骤,每个其中包含类似的功能

(表格等)。因此,我从

创建了一个基类,Step和子类,用于特定步骤。 Step类有一个方法,Execute(),它可以返回

成功或失败。


我有一个Step Driver,它实例化了第一步,调用它的Execute()

方法,然后,根据状态,运行适当的下一步。


我怎么知道下一个approriate一步是?我可以通过构建一些可用于决定

要做什么的数据结构将其嵌入到

步骤驱动程序中。但这很笨拙。


事实上,每一步都应该知道。成功的下一步是什么,以及下一步失败的原因是什么? Execute()方法,而不是返回

成功或失败,应该只返回适当的下一步。


我的初步尝试,不足为奇,立即用完了所有可用内存的b
。更晚的attmpts导致编译器错误。


我需要一些帮助来实现以下(伪)代码。谢谢

预先提出所有建议!


/ Joel Finkel
fi **** @ sd-il.com


class步骤:IDisposable {


字符串名称;

Step SuccessStep; //这是成功的

步骤NextStep; //处理失败


public Step {

}


public void ShowName(){

MessageBox.Show(name);

}


public Step Execute(){

return _execute() ;

}


public void Dispose()}

}


private Step _execute(){

//覆盖每个子类

}

}


class Step_1:Step {


public Step_1 {

SuccessStep = Step_2; //这是成功的

FailureStep = Step_1a; //处理失败

}


private Step _execute(){

if(做一些处理){

返回SuccessStep;

}否则{

返回FailureStep;

}

}

}


class Step_2:Step {


public Step_2 {

SuccessStep = null; //这是最后一步

FailureStep = Step_2a; //处理失败

}


private Step _execute(){

if(something){

返回SuccessStep;

}否则{

返回FailureStep;

}

}

}


class Step_Driver {


Step_Drvier(){

}


public void Main(){


Step nextStep;


Step thisStep = new Step_1();


//处理步骤直到不再有

而(thisStep!= null){

nextStep = thisStep.Execute ();

thisStep.Dispose();

if(nextStep!= null){

thisStep = new nextStep();

}

}

}

}

解决方案

我不知道,Joel。好像你在这里工作太辛苦了。我认为

这些步骤按顺序执行一个,如下所示:


1,2,3,4,5,6,7,8


我说错了吗?如果是这样,看起来不是很像阵列或

集合,还是列表?


-

HTH,


Kevin Spencer

Microsoft MVP

..Net开发人员

A看着时钟永不沸腾。


" Joel Finkel" <乔******** @ discussions.microsoft.com>在消息中写道

news:05 ********************************** @ microsof t.com ...

人们,

我对如何实现以下解决方案感到困惑。

我有一系列的处理步骤,每个步骤包含类似的
特征
(形式等)。因此,我为
创建了一个基类,步骤和子类,以获取具体步骤。 Step类有一个方法,Execute(),它可以
返回成功或失败。

我有一个Step Driver,它实例化第一步,调用它的<执行()
方法,然后,根据状态,运行下一步的应用程序。

我怎么知道下一步是什么?我可以通过构建一些可用于决定要做什么的数据结构将其嵌入到
步骤驱动程序中。但这很笨拙。

事实上,每一步都应该知道。成功的下一步是什么,以及
下一步失败的原因是什么。 Execute()方法,而不是返回成功或失败,应该只是返回适当的下一步。

我的初步尝试,毫不奇怪,立即用尽所有
有效内存。更糟糕的是导致编译器错误。

我需要一些帮助来实现以下(伪)代码。
提前感谢所有建议!
/ Joel Finkel
fi****@sd-il.com
课程步骤:IDisposable {

字符串名称;
Step SuccessStep; //这是成功的
步骤NextStep; //处理失败

公共步骤{
}

public void ShowName(){
MessageBox.Show(name);
}

公共步骤执行(){
返回_execute();
}
公共无效Dispose()}
}

私人Step _execute(){
//覆盖每个子类
}


类Step_1:步骤{
<公共Step_1 {
SuccessStep = Step_2; //这样做是否成功
FailureStep = Step_1a; //处理失败

私有Step _execute(){
if(做一些处理){
返回SuccessStep;
}否则{
返回FailureStep;
}
}
}

类Step_2:步骤{

公共Step_2 {
SuccessStep =空值; //这是最后一步
FailureStep = Step_2a; //处理失败

私有Step _execute(){
if(something){
返回SuccessStep;
}否则{
返回FailureStep;
}
}

类Step_Driver {

Step_Drvier(){
}

public void Main(){

Step nextStep;

Step thisStep = new Step_1();

//处理步骤直到没有更多
while(thisStep!= null){
nextStep = thisStep.Execute();
thisStep.Dispose();
if(nextStep!= null){
thisStep = new nextStep();
}
}
}
}





" Joel Finkel" <乔******** @ discussions.microsoft.com>在消息中写道

news:05 ********************************** @ microsof t.com ...

人们,

我对如何实现以下解决方案感到困惑。

我有一系列的处理步骤,每个步骤包含类似的
特征
(形式等)。因此,我为
创建了一个基类,步骤和子类,以获取具体步骤。 Step类有一个方法,Execute(),它可以
返回成功或失败。

我有一个Step Driver,它实例化第一步,调用它的<执行()
方法,然后,根据状态,运行下一步的应用程序。

我怎么知道下一步是什么?我可以通过构建一些可用于决定要做什么的数据结构将其嵌入到
步骤驱动程序中。但这很笨拙。

事实上,每一步都应该知道。成功的下一步是什么,以及
下一步失败的原因是什么。 Execute()方法,而不是返回成功或失败,应该只是返回适当的下一步。

我的初步尝试,毫不奇怪,立即用尽所有
有效内存。更进一步的问题导致编译器错误。

我需要一些帮助来实现以下(伪)代码。
提前感谢所有建议!



我同意Kevin的观点,如果这是一个静态序列,你应该使用一个

数组来完成你需要采取的步骤并进行迭代。但是,如果这是一个更复杂的场景,你可以在

的正确轨道上采取多个步骤。但是,您的代码将不会按原样编译,您缺少

构造函数上的括号(公共Step_2(){...})并且

引用类型名称而不是创建它们(即新的Step_2();)。你可以建立一个简短但完整的例子[1]吗?


1. http://www.yoda.arachsys.com/csharp/complete.html

>成功的下一步是什么,以及

下一步失败是什么。


从他的描述来看,它听起来不像是一系列事件,它听起来更像是决策树,因为我们可能会有不同的

基于当前步骤结果的后续步骤。我同意你的看法,这将是一个糟糕的设计,以便在步骤中封装下一步。每个

步骤应该只包含确定其truthality所需的逻辑;

这将最大化步骤的可重用性。

第一个大问题是你正在将步骤方法调用基于

类而不是接口,这将抽象出你的问题并使其成为新的类更加灵活。




公共接口IStep

{

string Id;

bool执行();

void Dispose();

}


接下来封装决策数据,每一步都需要其中两个,一个
对于真实和一对为假。

公共支柱决定

{

bool结果;

string NextItemId; < br $>
}


我想说有两个项目的数组会很好;步骤和

关系。


私人Hashtable关系;

私人NameValueCollection步骤;


步骤集合将包含ISteps.Id作为键,IStep作为值。所以

当你执行一个步骤[" xyz]时,你会得到一个IStep的对象,其中包含一个id为xyz的



关系将具有当前ID的键并将返回值

对象决策。


所以这是如何工作的。将您的关系和步骤加载到数组后,将
加载到数组中。它是如何工作的一个例子:''Step1''首先是

已处理的调用执行并返回true,迭代关系

集合并获取NextItemId''对于所有真实的决定。它然后获取这些Id'并从Steps集合中获取ISteps,

调用execute方法,整个过程重新开始。实际上

过程必须是递归的。


您可能还想查找过滤器设计模式。


Joe

Daniel O''Connell [C#MVP]" < onyxkirx @ - NOSPAM--comcast.net>写在

消息新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
" Joel Finkel" <乔******** @ discussions.microsoft.com>在消息中写道
新闻:05 ********************************** @ microsof t.com。 ..

人们,

我对如何实施以下解决方案感到困惑。

我有一系列处理步骤,每个其中包含类似的
特征
(表格等)。因此,我为
创建了一个基类,步骤和子类,以获取具体步骤。 Step类有一个方法,Execute(),它可以
返回成功或失败。

我有一个Step Driver,它实例化第一步,调用它的<执行()
方法,然后,根据状态,运行下一步的应用程序。

我怎么知道下一步是什么?我可以通过构建一些可用于决定要做什么的数据结构将其嵌入到
步骤驱动程序中。但这很笨拙。

事实上,每一步都应该知道。成功的下一步是什么,以及
下一步失败的原因是什么。 Execute()方法,而不是返回成功或失败,应该只返回适当的下一步。

我的初步尝试,毫不奇怪,立即用完了
所有
可用内存。更进一步的问题导致编译器错误。

我需要一些帮助来实现以下(伪)代码。
提前感谢所有建议!


我同意Kevin的观点,如果这是一个静态序列,你应该使用
一系列你需要采取的步骤并进行迭代。但是,如果这是一个更复杂的场景,可能会采取多个步骤,使您走上正确的轨道。但是,您的代码不会按原样编译,您在构造函数上缺少括号(公共Step_2(){...})并且引用类型名称而不是创建它们(即新的Step_2( ))。你可以建立一个简短但完整的例子[1]吗?

1. http://www.yoda.arachsys.com/csharp/complete.html



Folks,

I am confused as to how to implement the following solution.

I have a series of processing steps, each of which contains similar features
(forms, etc). Therefore, I create a base class, Step, and subclass from that
for specific steps. The Step class has a method, Execute(), which can return
either Success or Failure.

I have a Step Driver, which instantiates the first Step, calls its Execute()
method, and then, depending on the status, runs the approriate next Step.

How do I know what the approriate next step is? I could embed this in the
Step Driver by building some data structure that can be used to decide what
to do. But this is clumsy.

In fact, each Step should "know" what the next Step for Success is, and what
the next Step for Failure is. The Execute() method, instead of returning
Success or Failure, should simply return the approriate next Step.

My initial attempt resulted, not surprisingly, in immediately using up all
available memory. Futher attmpts resulted in compiler errors.

I need some assistance in implementing the following (pseudo) code. Thanks
in advance for all suggestions!

/Joel Finkel
fi****@sd-il.com

class Step : IDisposable {

string name;
Step SuccessStep; // do this is sucessful
Step NextStep; // handle failure

public Step {
}

public void ShowName() {
MessageBox.Show(name);
}

public Step Execute() {
return _execute();
}

public void Dispose() }
}

private Step _execute() {
// override for each sub class
}
}

class Step_1 : Step {

public Step_1 {
SuccessStep = Step_2; // do this is successful
FailureStep = Step_1a; // handle failure
}

private Step _execute() {
if (do some processing) {
return SuccessStep;
} else {
return FailureStep;
}
}
}

class Step_2 : Step {

public Step_2 {
SuccessStep = null; // this is the last step
FailureStep = Step_2a; // handle failure
}

private Step _execute() {
if (something) {
return SuccessStep;
} else {
return FailureStep;
}
}
}

class Step_Driver {

Step_Drvier() {
}

public void Main() {

Step nextStep;

Step thisStep = new Step_1();

// process steps until there ain''t no more
while (thisStep != null) {
nextStep = thisStep.Execute();
thisStep.Dispose();
if (nextStep != null) {
thisStep = new nextStep();
}
}
}
}

解决方案

I don''t know, Joel. Seems like you''re working too hard here. I presume that
these "Steps" are executed one at a time, in a sequence, like the following:

1, 2, 3, 4, 5, 6, 7, 8

Am I correct? If so, doesn''t that look an awful lot like an array or a
Collection, or a List?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Joel Finkel" <Jo********@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...

Folks,

I am confused as to how to implement the following solution.

I have a series of processing steps, each of which contains similar
features
(forms, etc). Therefore, I create a base class, Step, and subclass from
that
for specific steps. The Step class has a method, Execute(), which can
return
either Success or Failure.

I have a Step Driver, which instantiates the first Step, calls its
Execute()
method, and then, depending on the status, runs the approriate next Step.

How do I know what the approriate next step is? I could embed this in the
Step Driver by building some data structure that can be used to decide
what
to do. But this is clumsy.

In fact, each Step should "know" what the next Step for Success is, and
what
the next Step for Failure is. The Execute() method, instead of returning
Success or Failure, should simply return the approriate next Step.

My initial attempt resulted, not surprisingly, in immediately using up all
available memory. Futher attmpts resulted in compiler errors.

I need some assistance in implementing the following (pseudo) code.
Thanks
in advance for all suggestions!

/Joel Finkel
fi****@sd-il.com

class Step : IDisposable {

string name;
Step SuccessStep; // do this is sucessful
Step NextStep; // handle failure

public Step {
}

public void ShowName() {
MessageBox.Show(name);
}

public Step Execute() {
return _execute();
}

public void Dispose() }
}

private Step _execute() {
// override for each sub class
}
}

class Step_1 : Step {

public Step_1 {
SuccessStep = Step_2; // do this is successful
FailureStep = Step_1a; // handle failure
}

private Step _execute() {
if (do some processing) {
return SuccessStep;
} else {
return FailureStep;
}
}
}

class Step_2 : Step {

public Step_2 {
SuccessStep = null; // this is the last step
FailureStep = Step_2a; // handle failure
}

private Step _execute() {
if (something) {
return SuccessStep;
} else {
return FailureStep;
}
}
}

class Step_Driver {

Step_Drvier() {
}

public void Main() {

Step nextStep;

Step thisStep = new Step_1();

// process steps until there ain''t no more
while (thisStep != null) {
nextStep = thisStep.Execute();
thisStep.Dispose();
if (nextStep != null) {
thisStep = new nextStep();
}
}
}
}




"Joel Finkel" <Jo********@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...

Folks,

I am confused as to how to implement the following solution.

I have a series of processing steps, each of which contains similar
features
(forms, etc). Therefore, I create a base class, Step, and subclass from
that
for specific steps. The Step class has a method, Execute(), which can
return
either Success or Failure.

I have a Step Driver, which instantiates the first Step, calls its
Execute()
method, and then, depending on the status, runs the approriate next Step.

How do I know what the approriate next step is? I could embed this in the
Step Driver by building some data structure that can be used to decide
what
to do. But this is clumsy.

In fact, each Step should "know" what the next Step for Success is, and
what
the next Step for Failure is. The Execute() method, instead of returning
Success or Failure, should simply return the approriate next Step.

My initial attempt resulted, not surprisingly, in immediately using up all
available memory. Futher attmpts resulted in compiler errors.

I need some assistance in implementing the following (pseudo) code.
Thanks
in advance for all suggestions!



I agree with Kevin that if this is a static sequence, you should just use an
array of the steps you need to take and iterate it. However, if this is a
more complicated scenario where multiple steps might be taken your on the
right track. However, your code will not compile as is, you are missing
parentheses on your constructors(public Step_2() { ... }) and are
referencing type names instead of creating them (ie new Step_2();). Could
you build a short but complete example[1]?

1. http://www.yoda.arachsys.com/csharp/complete.html


> what the next Step for Success is, and what

the next Step for Failure is.
From his description it doesnt sound to me like a sequence of events, it
sounds more like a decision tree because we potentially would have different
next steps based on the current steps result. I agree with you that it would
be a poor design to have the next step(s) encapsulated within the step. Each
step should only contain the logic necessary to determine its truthality;
this will maximize steps reuseability.
The first big problem is that you are basing the steps method calls to a
class and not an interface, this will abstract yyour problem out and make it
more flexible to new classes.

public interface IStep
{
string Id;
bool Execute();
void Dispose();
}

next encapsulate the decision data, each step would need two of these, one
for true and one for false.
public strut Decision
{
bool Result;
string NextItemId;
}

I would say having an array of two items would be good; steps and the
relationships.

private Hashtable Relationships;
private NameValueCollection Steps;

Steps collection would contain ISteps.Id as the key and IStep as value. So
when you do a Steps["xyz"] you would get back an object of IStep with an Id
of xyz.

Relationships would have the keys of the current ID and would return a value
object Decision.

So how this works overall. After your relationships and steps are loaded
into the arrays. An example of how it would work: ''Step1'' first to be
processed calls execute and returns true, iterates over the relationship
collection and acquires the NextItemId''s for all decisions that are true. It
then takes these Id''s and obtains the ISteps from the Steps collection,
calls execute method and the whole process starts over again. Actually the
process MUST be recursive.

You might also want to look up the Filter design pattern.

Joe
"Daniel O''Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
"Joel Finkel" <Jo********@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...

Folks,

I am confused as to how to implement the following solution.

I have a series of processing steps, each of which contains similar
features
(forms, etc). Therefore, I create a base class, Step, and subclass from
that
for specific steps. The Step class has a method, Execute(), which can
return
either Success or Failure.

I have a Step Driver, which instantiates the first Step, calls its
Execute()
method, and then, depending on the status, runs the approriate next Step.

How do I know what the approriate next step is? I could embed this in
the
Step Driver by building some data structure that can be used to decide
what
to do. But this is clumsy.

In fact, each Step should "know" what the next Step for Success is, and
what
the next Step for Failure is. The Execute() method, instead of returning
Success or Failure, should simply return the approriate next Step.

My initial attempt resulted, not surprisingly, in immediately using up
all
available memory. Futher attmpts resulted in compiler errors.

I need some assistance in implementing the following (pseudo) code.
Thanks
in advance for all suggestions!



I agree with Kevin that if this is a static sequence, you should just use
an array of the steps you need to take and iterate it. However, if this is
a more complicated scenario where multiple steps might be taken your on
the right track. However, your code will not compile as is, you are
missing parentheses on your constructors(public Step_2() { ... }) and are
referencing type names instead of creating them (ie new Step_2();). Could
you build a short but complete example[1]?

1. http://www.yoda.arachsys.com/csharp/complete.html



这篇关于我需要代表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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