Is this called adapter? + adapter vs decorator

两盒软妹~` 提交于 2019-12-04 10:27:07
Vitaly

Depends on what you're actually trying to achieve. Adapter and Decorator are pretty much similar, however when you implement an Adapter you bring no new logic besides conversion. When implementing a Decorator you actually bring in some brand new functionality that never existed before in the object you're decorating.

So, long story short, if interface properties Id, WorkerId etc are naturally coming from TaskWithListOfProperties - then you should consider it as an Adapter. Otherwise it's a Decorator.

From the original GoF book, the intent of the Adapter pattern [Black Wasp] [Wikipedia] is to...

convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

While the intent of the Decorator pattern [Black Wasp] [Wikipedia] is to...

attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending funtionality.

Though the patterns are similar, from the definition it's clear that this is the adapter pattern. You've got a square peg (TaskFromListOfProperties) that needs to fit into a round hole (ISpecialTask), so you've adapted it using SpecialTaskFromListOfProperties.

A decorator would augment/extend the existing functionality of TaskFromListOfProperties, i.e. it wouldn't change its existing interface. That's not what SpecialTaskFromListOfProperties is doing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!