我第一次使用这个模式,并且使用C#。
I just wanted to check that this is the correct implementation.
I have a button on a Winform, and when clicked on that will output some data in a particular format, defined by choosing from a drop down box. Now this may change in the future, hence me using the Strategy pattern to encapsulate what changes.
我有一个策略接口,它只公开了一个方法:“DisplayData()”。
单击按钮时,我使用以下代码:
private void ConfirmButton_Click(object sender, EventArgs e)
{
IViewData viewData;
switch (outputMedia)
{
case "Excel":
viewData = new ExcelOutput(operation, study);
viewData.DisplayData();
break;
case "Spotfire":
viewData = new SpotfireOutput(operation, study);
viewData.DisplayData();
break;
}
}
这是一种可以接受的使用这种模式的方式吗?显然,如果确定了额外的输出媒体,那么我只需创建一个新的子类,并在switch语句中添加一个额外的case。
谢谢