C#的设计模式,并基于字符串参数调用不同的方法 [英] Design Pattern for C# and invoking different methods based on a string parameter

查看:183
本文介绍了C#的设计模式,并基于字符串参数调用不同的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我目前正在开发一个非常酷的库,该库中我将从Excel工作表或Sharepoint列表中提取数据,然后将WatiN和.NET与C#结合使用以执行各种浏览器命令以进行自动化UI测试。但是,在尝试封装不断变化的需求(可能需要生成将来的命令或测试)时,我遇到了一个大设计问题。目前,我需要根据命令参数(存储为Excel或Sharepoint列表中的字符串)执行大约5个独特的操作,但是我想轻松地使命令数量可扩展以及执行验证以确保没有不好的命令。谁能为我指出正确的设计模式方向,而这可能有助于我有效而稳健地实现这一目标,而不仅仅是在HandleCommand()函数中编写一条巨型switch语句?感谢您帮助新程序员! = D

So I am currently about to develop a rather cool library in which I will pull data from an Excel Worksheet or Sharepoint List and then use WatiN and .NET with C# to execute various browser commands for automated UI testing. However, I am running into a big design issue in trying to encapsulate the changing requirements that future commands or tests may need to be generated. Currently, I have about 5 unique actions that I need to perform based on a command parameter (stored as a string in the Excel or Sharepoint List), but I would like to easily make the number of commands extensible as well as perform validation to ensure no bad commands. Can anyone point me in the right direction for Design Patterns that might help me implement this efficiently and robustly rather than just writing one giant switch statement in a HandleCommand() function? Thanks for helping a new programmer out! =D

推荐答案

查看命令模式来封装命令,并使用工厂模式来根据名称创建命令对象的实例。工厂可以使用反射来确定根据文本创建哪个命令。

Look at the command pattern to encapsulate the commands and use the factory pattern to create the instance of the command object based on its name. The factory can use reflection to determine which command to create based on the text.

这篇关于C#的设计模式,并基于字符串参数调用不同的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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