这也叫适配器吗? +适配器与装饰器

发布于 2024-11-28 14:04:45 字数 1004 浏览 6 评论 0原文

我有 2 个项目:A 和 B,它们应该相互交互。

  1. 项目A引入接口名称ISpecialTask​​,项目B应该实现它。

  2. Projet B 有一个名为 TaskWithListOfProperties 的实体,该实体无法实现 ISpecialTask​​,因为它具有不同的属性结构(此外,所有系统都知道如何使用 TaskWithListOfProperties 并且我不想更改其结构)。

因此,我决定创建一个名为 SpecialTask​​FromListOfProperties 的类,它实现 ISpecialTask​​ 并使用 TaskWithListOfProperties 实例,以便将其用于项目之间的交互。

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{ ... } }
}

SpecialTask​​FromListOfProperties 实际上是适配器模式吗?
适配器模式和装饰器模式有什么区别?

I have 2 projects: A and B that should interactwith each other.

  1. Project A introduce interface names ISpecialTask and Project B should implement it.

  2. 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).

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{ ... } }
}

Is SpecialTaskFromListOfProperties actually the Adapter pattern?
What is the difference between the adapter pattern and decorator pattern?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-12-05 14:04:45

来自原始 GoF 书籍,适配器模式的意图[黑黄蜂] [Wikipedia] 是为了...

将一个类的接口转换为客户期望的另一个接口。适配器让类能够协同工作,否则由于接口不兼容而无法协同工作。

而装饰器模式的意图[Black Wasp]< /sup> [维基百科] 是...

动态地将附加职责附加到对象。装饰器为扩展功能提供了子类化的灵活替代方案。

尽管模式相似,但从定义来看,很明显这是适配器模式。您有一个方钉 (TaskFromListOfProperties),需要装入圆孔 (ISpecialTask​​),因此您已使用 SpecialTask​​FromListOfProperties 对其进行了调整>。

装饰器将增强/扩展TaskFromListOfProperties 的现有功能,即它不会更改其现有接口。这不是 SpecialTask​​FromListOfProperties 正在做的事情。

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.

清泪尽 2024-12-05 14:04:45

取决于您实际想要实现的目标。 AdapterDecorator 非常相似,但是当您实现 Adapter 时,除了转换之外,您不会带来任何新逻辑。在实现装饰器时,您实际上引入了一些以前在您正在装饰的对象中从未存在过的全新功能。

因此,长话短说,如果接口属性 IdWorkerId 等自然来自 TaskWithListOfProperties - 那么您应该将其视为 适配器。否则它是一个装饰器

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, should interface properties Id, WorkerId etc be naturally coming from TaskWithListOfProperties - then you should consider it as an Adapter. Otherwise it's a Decorator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文