重构帮助-策略模式
这里的目标是更新 UI。 我通常在客户端上执行此操作,但是此应用程序使用隐藏的代码。 无论如何,我的问题是我正在尝试清理这些 if else 语句,并且我认为策略模式可能是合适的。 我不需要为我做所有的事情,但如果你能给我一些指导,让我继续前进。 我是否要先创建一个接口,然后每个策略都实现该接口? 泛型在这里有用吗? 接口中应该包含哪些类型的方法? 任何能让我继续前进的事情都将非常感激。
if (someObject.Status == 'A') {
btnRecordCall.Enabled = false;
btnAddMailOrStatusAction.Enabled = false;
btnPayments.Enabled = false;
btnAddressMaint.Enabled = false;
btnFilter.Enabled = false;
btnAddCoverage.Enabled = false;
btnPolicyForms.Enabled = false;
lblIsArchived.Text = "********** THIS CLAIM HAS BEEN ARCHIVED **********";
} else if (someObject.Status == 'D') {
btnRecordCall.Enabled = false;
btnAddMailOrStatusAction.Enabled = false;
btnPayments.Enabled = false;
btnAddressMaint.Enabled = false;
btnFilter.Enabled = false;
btnAddCoverage.Enabled = false;
btnPolicyForms.Enabled = false;
lblIsArchived.Text = "- De-archive Request Pending";
} else {
btnRecordCall.Enabled = true;
btnAddMailOrStatusAction.Enabled = true;
btnPayments.Enabled = true;
btnAddressMaint.Enabled = true;
btnFilter.Enabled = true;
btnAddCoverage.Enabled = true;
btnPolicyForms.Enabled = true;
lblIsArchived.Text = "";
}
谢谢, 〜ck
The object here is to update the UI. I normally do this on the client however this application uses the code behind. Anyways my question is I am trying to clean up these if else statements and I thought the strategy pattern may be appropriate. I don't need everything done for me but if you could give me a couple pointers to get going. Do I create an interface first and then each strategy implement the interface? Are generics helpful here? What types of methods should be in the interface? Any thing to get me going would be very much appreciated.
if (someObject.Status == 'A') {
btnRecordCall.Enabled = false;
btnAddMailOrStatusAction.Enabled = false;
btnPayments.Enabled = false;
btnAddressMaint.Enabled = false;
btnFilter.Enabled = false;
btnAddCoverage.Enabled = false;
btnPolicyForms.Enabled = false;
lblIsArchived.Text = "********** THIS CLAIM HAS BEEN ARCHIVED **********";
} else if (someObject.Status == 'D') {
btnRecordCall.Enabled = false;
btnAddMailOrStatusAction.Enabled = false;
btnPayments.Enabled = false;
btnAddressMaint.Enabled = false;
btnFilter.Enabled = false;
btnAddCoverage.Enabled = false;
btnPolicyForms.Enabled = false;
lblIsArchived.Text = "- De-archive Request Pending";
} else {
btnRecordCall.Enabled = true;
btnAddMailOrStatusAction.Enabled = true;
btnPayments.Enabled = true;
btnAddressMaint.Enabled = true;
btnFilter.Enabled = true;
btnAddCoverage.Enabled = true;
btnPolicyForms.Enabled = true;
lblIsArchived.Text = "";
}
Thanks,
~ck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果无论状态如何,所有按钮都获得相同的启用(如您的示例所示),那么我会将其隐藏在按钮列表中,然后迭代它们。 然后你的策略参数可以归结为一个布尔值和一个字符串——无论如何,做到这一点都很容易。 如果它的复杂性更大,那么实现设置控件的接口的不同类将是有序的,可能具有保存整体逻辑的抽象超类。
If all the buttons get the same enabling regardless of the status, as in your example, then I'd hide that away in a list of buttons and just iterate over them. Then your parameters for the strategy boil down to a single boolean and a string - and it's pretty easy to do that anywhichway. If there's greater complexity to it, then different classes implementing an interface that sets the controls would be in order, possibly with an abstract superclass that holds the overall logic.
首先,如果您要将策略模式与接口一起使用,您可能需要这样的接口。
然后是你的逻辑的几个类
然后你的 if 语句在技术上可以是一个工厂。
或者您可以将此对象作为接口返回,并将 switch 语句封装在另一个方法或工厂类中。
这样你就可以像这样使用它。
由你决定。
另外,我在这里没有使用收容类,但如果您愿意,也可以这样做。
您也可以与代表一起执行此操作。
遏制类
不同的策略方法。
Switch 语句
从外部调用此语句。
Well first of all, if you're going to use the Strategy pattern with an Interface you might want an interface like this.
then a few classes for your logic
then your if statement can be a factory technically.
or you can return this object as an interface and encapsulate the switch statement in another method or factory class.
So that you can use it like this.
It's up to you.
Also I am not using a containment class here, but you can if you want to.
You can also do this with delegates.
Containment class
Different strategy Methods.
Switch statement
Call this from outside.
我认为您正在寻找的是 State 模式。 这与策略模式类似,只是每个状态对象在创建时通常会被赋予对上下文对象的引用(在您的情况下,这是表单)。 这允许不同的状态对上下文对象执行操作以响应事件。
实现状态模式时,通常最好让每个单独的状态继承抽象基类。 然后,基类可以在虚拟方法中实现默认操作,然后您只需重写每个状态不同的操作。
I think what you are looking for is the State patten. This is similar to the strategy pattern except that each state object is normally given a reference to the context object (in your case this is the form) when it is created. This allows the different states to do things to the context object in response to events.
When implementing a state pattern is is often preferable to make each seperate state inherit from an abstract base class. The base class can then implement the default operations in virtual methods and then you only have to override the operations that differ for each state.