想要实现一个实用程序类,其方法代表验证过程中的步骤。 是否有一个模式或最佳实践?
我想实现一个实用程序类,其方法是验证过程的内部步骤。 有没有一个模式,或者我应该使用完全不同的方法? 我愿意接受建议。 (我正在 abap 中编码,但我认为这并不重要)
编辑:它没有文本的前端验证,而是检查某些条件是否匹配。 (参数实际上是一个表。对于每一行,我检查是否有匹配的条件,例如其他数据库表中是否有有效条目。)
像这样的:
Class Validator
{
private bool flag_error;
private Step1 ( var a, var b )
{
//do somthing ...
}
private Step 2 ( var a )
{
//do somthing ...
}
private Step 3 ( var c )
{
//do somthing ...
}
static Check(var a, var b, var c)
{
Step1(a, b );
Step2( a );
Step3( c );
return flag_error;
}
}
用法:
if (Validator.Check(a,b,c) )
{
//do good stuff
}
else
{
//do error handling
};
i want to implement a utility class which methods are internal steps of a validation process. Is there a pattern for this or should i use a totally different approach? Im open for suggestions. (I´m coding in abap but i dont think that is important)
Edit: Its no frontend validation of text, but a check if certain conditions are matched. (The parameter is actually a table. For each row i check if there are conditions matched as an example if there is a valid entry in an other db table.)
Somthing like this:
Class Validator
{
private bool flag_error;
private Step1 ( var a, var b )
{
//do somthing ...
}
private Step 2 ( var a )
{
//do somthing ...
}
private Step 3 ( var c )
{
//do somthing ...
}
static Check(var a, var b, var c)
{
Step1(a, b );
Step2( a );
Step3( c );
return flag_error;
}
}
Usage:
if (Validator.Check(a,b,c) )
{
//do good stuff
}
else
{
//do error handling
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
设计决策实际上取决于细节。 会有多种验证器算法实现吗? 尝试策略或模板方法 模式。
如果您只需要这个类执行这些多个步骤,那么您已经实现了一种模式,组合方法。 把事情简单化。 除非确实需要,否则不要增加复杂性。
Design decisions really depend on the details. Will there be multiple validator algorithm implementations? Try a Strategy or Template Method pattern.
If you only need this one class performing these multiple steps, you've already implemented a pattern, Composed Method. Keep it simple. Don't add layers of complexity unless they're truly needed.
我想到了一种模式。 这是模板模式,
我将使用
Check
创建一个抽象类如上实现以及由具体类实现的Step
方法。There is one patten that comes to mind. It is the template pattern
I would make an abstract class with
Check
implemented as above and theStep
methods to be implemented by the concrete classes.我曾经编写过一个访问者模式,它非常适合将实际验证与字段分开。
I once wrote a visitor pattern, which worked quite nicely for separating the actual validation from the fields.
我使用 C# 属性进行验证。 您可以在此处找到详细文章。 这可以帮助您通过配置而不是约定来实现验证。 使用这种技术,您的验证例程将变得高度抽象和可重用。
I use C# attributes for validation purpose. You can find a detailed article here. This helps you to implement validation by configuration over convention. Using this technique your validation routines become highly abstract and reusable.