Adapter Pattern (Structural Pattern)

The design patterns which allow the interface of a class to be used as another interface. This is used to adapt and make two existing classes work together. Like the real world adapters, which let you use a headphone-jack or plug-in a phone charger in an otherwise non-compatible socket, the adapter design pattern bridges a gap between two objects.

The purpose of the Adapter pattern is to convert the interface of a class into another interface and thus make it usable by the other objects/classes/interfaces. Depending on the extent of change required for the adaptation, one might need to write code to convert data structures and logic to implement Adapter Pattern.

Recommended Reading: Delegation Pattern.

Adapter Pattern uses composition; it delegates the calls to the ‘Adaptee’. Benefit of this is that the Adapter adapts the ‘Adaptee’, and also its sub-classes. And with that, the limitation is obvious: Anytime you add a method to the ‘Adaptee’ class or its subclasses, you have to change/extend the Adapter classes as well. You have to write the code to delegate all the necessary requests to the ‘Adaptee’.

Src: https://javarevisited.blogspot.com/2016/08/adapter-design-pattern-in-java-example.html


要查看或添加评论,请登录

Rahul Srivastava的更多文章

  • Singleton Pattern (Creational Pattern)

    Singleton Pattern (Creational Pattern)

    Singleton Pattern: It is a type of Creational Pattern. When we need to make sure that one (and only one) instance of a…

  • Overview of Design Patterns

    Overview of Design Patterns

    There are more than 50 identified Design Patterns. There are 4 main categories of Design Patterns: Creational…

  • Object Oriented Design Patterns

    Object Oriented Design Patterns

    Gang of Four: In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book…

    1 条评论

社区洞察

其他会员也浏览了