设计模式
我对模式编码方式很陌生,并且想开始在我的代码中实现模式。我有一个具有多个操作的 Web 服务,并为每个操作接收不同的 xml 输入(具有自己的架构)。我想实现一种模式来验证提供的输入。
我的设计: 有一个接口IValidate 实现 EntityAValidate、EntityBValidate 等接口, 每个实现的验证方法都会检查 xml 的有效性,并且还会进行单个节点验证,例如字符串不能包含特殊字符等。
我的问题: 我们可以使用其他设计吗?请告诉我图案名称 如何在操作中重用一些常见的验证,例如数字检查、日期检查 如何自动选择相应的验证器?
I am new to the pattern way of coding and would like to start implementing patterns in my code. I have a webservice that has multiple operations and receives different xml inputs for each operation(having its own schema). I would like to implement a pattern to validate the input provided.
My design:
Have an interface IValidate
implement the interface to EntityAValidate, EntityBValidate etc..,
each implemented validate method will check for validity of the xml and also do the individual node validation such as the string cannot have special characters etc..,
My questions :
Can we use any other design? please let me know the pattern name
How can I reuse some common validations like numeric check, date check across operations
How can the selection of corresponding validator be done automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于“我有一个具有多个操作并为每个操作接收不同的 xml 输入”的 Web 服务,命令模式可能是一个不错的选择。不过,它的应用范围更广,不需要专门针对验证。
命令模式将每个操作封装为一个对象,每个操作都派生自一个公共基类(称为操作类),该基类提供“执行”方法。从每个传入的 xml 操作中,您将实例化相应的操作子类,将它们传递给调用执行的某个操作处理实体。
您可以在其中构建验证,向您的操作基类添加“验证”方法。然后,处理器可以在“执行”每个操作之前“验证”每个操作。
总的来说,我同意你帖子的评论者的观点。你应该研究设计模式本身,然后当你解决问题时它们就会跳出来。不要选择一种模式并试图将其塞入现有设计中。
Based on this statement "I have a webservice that has multiple operations and receives different xml inputs for each operation", the Command Pattern would probably be a good fit. It would apply more generally though, it need not be specific to validation.
The Command Pattern encapsulates each operation as an object, each derived from a common base class (call it class Operation) which provides an 'execute' method. From each incoming xml operation, you would instantiate the corresponding Operation sub-class, passing them to some operation processing entity who calls execute.
You could build your validation into this, adding a "validate" method to your Operation base class. The processor could then "validate" each Operation before "execute"ing it.
In general, I agree with the commenters on your post. You should study the design patterns themselves, then they'll jump out at you when your solving problems. Don't pick a pattern and try to cram it into an existing design.
我建议您开始阅读有关 DP 的内容,然后了解其中的许多内容。这样您就会意识到何时以及哪一个适合或不适合您的需求。设计模式应该在您确实有特定需求时使用,这意味着“使用得越多越好”是一种非常糟糕的方法。希望有帮助!
我留下两篇关于 DP 文献的推荐;
首先 - 设计模式 和
可重用面向对象软件的元素
I would recommend you to start reading about DP and then get to know many of them. This way you will just realize when and which one fits or not your needs. Design Patterns are meant to be used when you do have an specific need, its means that "the most you use the better" is a very bad approach. Hope it helps!
Im leaving two recomendations on DP literature;
Head First - Design Patterns and
Elements of Reusable Object Oriented Software