这是所谓的适配器? +适配器VS装饰 [英] Is this called adapter? + adapter vs decorator
问题描述
我有2个项目:A和B,应该interactwith对方。
I have 2 projects: A and B that should interactwith each other.
-
A计划引入接口名称ISpecialTask和B项目应该实现它。
Project A introduce interface names ISpecialTask and Project B should implement it.
谟B具有无法实现ISpecialTask,因为它的属性不同的结构(此外,所有的系统知道如何与TaskWithListOfProperties工作,我不想改变它的结构)的命名实体TaskWithListOfProperties。
Projet B has an entity named TaskWithListOfProperties that cannot implement ISpecialTask because it has different structure of properties (in addition, all the system knows how to work with TaskWithListOfProperties and I don't want to change its structure).
所以,我决定创建一个名为类SpecialTaskFromListOfProperties实现ISpecialTask并使用TaskWithListOfProperties例如为了使用它的项目之间的互动。
So I decided to create a class named SpecialTaskFromListOfProperties that implements ISpecialTask and uses TaskWithListOfProperties instance in order to use it for the interaction between the projects.
interface ISpecialTask {
long Id{get;}
long WorkerId{get;}
long VehicleId{get;}
long CustomerId{get;}
}
class TaskWithListOfProperties {
IDictionary<string, long> Properties {get;
}
class SpecialTaskFromListOfProperties : ISpecialTask {
public SpecialTaskFromListOfProperties(TaskWithListOfProperties ins) {
...
...
}
public long Id { get{ ... } }
public long WorkerId { get{ ... } }
public long VehicleId { get{ ... } }
public long CustomerId { get{ ... } }
}
时SpecialTaskFromListOfProperties实际上Adapter模式?
适配器模式和装饰图案之间的区别是什么?
Is SpecialTaskFromListOfProperties actually the Adapter pattern?
What is the difference between the adapter pattern and decorator pattern?
推荐答案
取决于你真正想要达到的目的。 适配器和装饰是pretty的很多类似的,但是当你实施适配器您带来任何新的逻辑之外的转换。当实施装饰您实际上带来了从来没有在你装饰对象之前存在的一些全新的功能。
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.
所以,长STOR总之,如果界面特性编号
, WorkerId
等自然从哪里来 TaskWithListOfProperties
- 那么你应该考虑它作为一个适配器。否则,它是一个装饰
So, long stor 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.
这篇关于这是所谓的适配器? +适配器VS装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!