适配器模式 vs 装饰者模式

Posted codingforum

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了适配器模式 vs 装饰者模式相关的知识,希望对你有一定的参考价值。

Reference

[1] https://refactoring.guru/design-patterns/adapter

1. Adaptor Design Pattern 适配器模式

Adapter is a structural design pattern, which allows incompatible objects to collaborate.

You can create an adapter. This is a special object that converts the interface of one object so that another object can understand it.

When adapter receives a call to any of its methods, it translates parameters to appropriate format and then directs the call to one or several methods of the wrapped object.

Exampes could be found here https://refactoring.guru/design-patterns/adapter/java/example

 

2. Decorator 装饰者模式

Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

Wrapper is the alternative nickname for the Decorator pattern that clearly expresses the main idea of the pattern. A “wrapper” is an object that can be linked with some “target” object. The wrapper contains the same set of methods as the target and delegates to it all requests it receives. However, the wrapper may alter the result by doing something either before or after it passes the request to the target.

Exampes could be found herehttps://refactoring.guru/design-patterns/decorator/java/example

以上是关于适配器模式 vs 装饰者模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式 结构型模式 -- 结构型模式(代理模式适配器模式装饰者模式桥接模式外观模式组合模式享元模式)

装饰者模式,适配器模式,代理模式区别

设计模式-结构型模式讲解下(装饰者外观代理)

小白自我提高学习设计模式笔记—装饰者模式

JDK设计模式之——装饰者模式(适配器模式)

设计模式----适配器装饰者和外观模式