简体   繁体   English

这是立面模式吗? 如果没有,那又如何?

[英]Is this the Facade pattern? If not then what?

I am trying to figure out what this design pattern is. 我试图弄清楚这种设计模式是什么。 I used it often when I have common logic that needs to execute on/for a single object. 当我具有需要在单个对象上执行/为单个对象执行的通用逻辑时,经常使用它。

I am thinking maybe Facade ? 我在想Facade吗?

Eg Validation 例如Validation

A public interface 公共接口

public interface IValidator<T>
{
    void Validate(T obj);
}

Multiple implementations 多种实现

public class CodeValidator<T> : IValidator<T> { public void Validate(T obj) { } }
public class PerfValidator<T> : IValidator<T> { public void Validate(T obj) { } }
public class DotValidator<T> : IValidator<T> { public void Validate(T obj) { } }
public class NetfValidator<T> : IValidator<T> { public void Validate(T obj) { } }

And then a class takes either a IValidatorCollection or straight IEnumerable<IValidator> which injects all registered validators. 然后,一个类采用IValidatorCollection或直接的IEnumerable<IValidator> ,后者将注入所有已注册的验证器。

public class PostValidaitonController
{
    private IEnumerable<IValidator<Post>> _validators;

    public PostValidaitonController(IEnumerable<IValidator<Post>> validators)
    {
        _validators = validators;
    }

    public void Validate(Post post)
    {
        foreach (var validator in _validators)
        {
            validator.Validate(post);
        }
    }
}

EDIT 编辑

I know this uses IoC/DI to inject the collection of IValidator<T> but that is not the pattern I am looking for. 我知道这使用IoC / DI注入IValidator<T>的集合, IValidator<T>不是我要寻找的模式。 Its the pattern that you hide multiple implementations of an interface behind a single interface / class 它是在单个interface / class后面隐藏interface多个实现的模式

The example you've given is a good example of the Strategy pattern . 您所提供的示例是Strategy模式的一个很好的示例。

Both the implementers of the Validator strategy and the users of the Validator depend on the abstraction IValidator. 验证者策略的实施者和验证者的用户都依赖于抽象IValidator。

This keeps the Validation users from caring about the details of particular Validation operations. 这使Validation用户不必关心特定Validation操作的细节。

This also keeps the Validation implementers from caring about how or when a given Validation is performed. 这也使验证实施者不必关心如何或何时执行给定的验证。

Edit: In fact, under the wiki entry for strategy pattern it explicitly mentions Validation: 编辑:实际上,在策略模式的Wiki条目下,它明确提到了Validation:

For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, or other discriminating factors. 例如,对传入数据执行验证的类可以使用策略模式基于数据类型,数据源,用户选择或其他区分因素来选择验证算法。 These factors are not known for each case until run-time, and may require radically different validation to be performed. 在每种情况下,直到运行时,这些因素都是未知的,并且可能需要进行根本不同的验证。 The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication. 与验证对象分开封装的验证策略可以由系统(甚至不同系统)不同区域中的其他验证对象使用,而无需代码重复。

The closest match imho is actually bridge pattern, but not really. 最接近的匹配恕我直言实际上是桥接模式,但实际上不是。 You're decoupling the validation rules/logic from the Validate method. 您正在将验证规则/逻辑与Validate方法脱钩。 While clients rely on the Validate method, you can actually modify the rules dynamically, based on some kind of criteria. 当客户端依靠Validate方法时,您实际上可以根据某种标准动态修改规则。

I've written such code before, albeit a little bit more complicated (eg the order of validation rules can be configured dynamically), I wouldn't personally classify it as any pattern. 我之前写过这样的代码,尽管稍微复杂一点(例如,验证规则的顺序可以动态配置),但我个人不会将其归类为任何模式。 You're simple using dependency injection to decouple the validation rules from the actual validation process. 您很容易使用依赖项注入将验证规则与实际验证过程脱钩。

That said, I don't like the injection of list validations. 就是说,我不喜欢注入列表验证。 You might want to consider refactoring it into strategy pattern (eg ValidationStrategy ), and move the Validate method into there instead. 您可能需要考虑将其重构为策略模式(例如ValidationStrategy ),然后将Validate方法移到其中。 Right now, most of your controllers get this "long" type, which is not that readable, plus, you'd have to duplicate the "Validate" method to each controller, or move it to base class (which I don't think is correct place). 现在,您的大多数控制器都获得了这种“长”类型,这种类型的可读性不高,此外,您必须将“ Validate”方法复制到每个控制器,或者将其移至基类(我认为这不是。)是正确的地方)。

Ps, to me your current solution does not look like strategy. 附言:对我来说,您当前的解决方案看起来不像策略。 Maybe "implementation wise", but the point is to get the intent right, as some of the patterns are implemented quite similarly anyways, but differ in their intent. 也许是“明智的实现”,但重点是要正确实现意图,因为无论如何,某些模式的实现都非常相似,但意图不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM