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
If you look at listing 7 onwards, the author says it is using the decorator pattern. However, is one of the main principles of this pattern to wrap objects and ADD responsibilities and behaviour?
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. 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 agree with you, that looks to me like the Adapter Pattern, that is, the
ModelStateDictionaryis abstracted behind the interfaceIValidationDictionary(the adapter interface) using a concrete type (the adapter) such that the implementation can be changed later.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:
Get()method to first check the cache for the item (andSave()would be overridden to also update the cache as well as the database).