简体   繁体   English

设计模式混乱

[英]Design Pattern Confusion

I am brushing up on my design patterns at the moment and got a little confused when I came across this tutorial: 此刻,我正在重新整理我的设计模式,当遇到本教程时,我感到有些困惑:

http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs

If you look at listing 7 onwards, the author says it is using the decorator pattern. 如果您从清单7开始看,作者说它正在使用装饰器模式。 However, is one of the main principles of this pattern to wrap objects and ADD responsibilities and behaviour? 但是,这种模式的主要原则之一是包装对象以及ADD的职责和行为吗?

I think it looks more like and adapter pattern as it is adapting the MVC specific ModelStateDictionary to work with a more flexible IValidationDictionary so that different implementations can be used with the service if WPF etc were used instead. 我认为它看起来更像是适配器模式,因为它正在适应MVC特定的ModelStateDictionary以与更灵活的IValidationDictionary一起工作,以便如果使用WPF等代替,则可以在服务中使用不同的实现。 There is new responsibility or behaviour added. 添加了新的责任或行为。

Do I have this correct or not? 我是否正确? If I'm wrong can anyone please explain why? 如果我错了,谁能解释为什么?

Thanks 谢谢

I think you're correct, and that there's an error in the post. 我认为您是正确的,并且帖子中有错误。 From the article: 从文章:

The Decorator pattern enables you to wrap an existing class in a new class in order to implement an interface. Decorator模式使您可以将现有类包装在新类中,以实现接口。

That's not exactly true - decorators do allow you to wrap one implementation inside another, but the intention usually isn't to implement another interface, but to "decorate" the instance with new functionality. 这不是完全正确的-装饰器确实允许您将一个实现包装在另一个实现中,但是通常的目的不是实现另一个接口,而是用新功能“装饰”实例。 The adapter pattern allows you to take two dissimilar interfaces, and modify one instance to be have like another. 适配器模式允许您采用两个不同的接口,并将一个实例修改为与另一个实例相似。

I agree with you, that looks to me like the Adapter Pattern , that is, the ModelStateDictionary is abstracted behind the interface IValidationDictionary (the adapter interface) using a concrete type (the adapter) such that the implementation can be changed later. 我同意您的看法,在我看来,它就像Adapter Pattern一样 ,也就是说,使用具体类型(适配器)在接口IValidationDictionary (适配器接口)后面抽象了ModelStateDictionary ,以便以后可以更改实现。

The Decorator Pattern usually provides additional functionality via composition, exposing the same interface as the decorated type. 装饰器模式通常通过合成提供其他功能,并公开与装饰类型相同的界面。 This is usually done via sub-classing or through interface implementation. 这通常通过子类化或通过接口实现来完成。

An example of a decorator would be: 装饰器的示例为:

  1. you have a repository class that fetches "objects" from the database 您有一个存储库类,可从数据库中获取“对象”
  2. you have a repository decorator class that can cache objects without needing to fetch them from the database each time. 您有一个存储库装饰器类,它可以缓存对象而无需每次都从数据库中获取它们。 This decorator class provides the cache fetching and retrieving through composition by sub-classing the original repository class and overriding the Get() method to first check the cache for the item (and Save() would be overridden to also update the cache as well as the database). 此装饰器类通过对原始存储库类进行子类化并重写Get()方法以首先检查项目的缓存来提供缓存的获取和通过组合检索的功能(然后将覆盖Save()来更新缓存以及数据库)。

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

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