Structural Design Pattern: Adapter Design Pattern

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:02:00

Define Structural Design Pattern

Structural Design Patterns are design patterns that make design easier by establishing a simple way to represent the link between elements. In simple terms, the Structural Design Pattern is used to manage the structure of classes & interfaces, as well as the relationships between classes. 

When should I use Structural Design Patterns in C#?

In real-time applications, structural design patterns help to preserve flexibility by allowing changes to class structures or relationships to be made without affecting the project. For example, structural design patterns allow you to change the relationship between classes such as User and Product without affecting the project.

Types of Structural Design Patterns in C#

The following is a list of structural design patterns:

  1. Adapter Design Pattern
  2. Facade Design Pattern
  3. Decorator Design Pattern
  4. Bridge Design Pattern
  5. Composite Design Pattern
  6. Proxy Design Pattern
  7. Flyweight Design Pattern

Define Adapter Design Pattern

The adapter pattern serves as a bridge between two incompatible interfaces. This design uses a single class, adapter, to handle communication between two independent or incompatible interfaces.

Example of Adapter Design Pattern

A card reader serves as an interface between a memory card and a laptop. You connect the memory card to a card reader and the card reader to the laptop, allowing the laptop to read the memory card.

Examples of adapter patterns include: 

  • Data Transfer Object (DTO) to Business Object.
  • Need to convert third-party objects to application types.
  • ADO.NET SqlAdapter, OracleAdapter, and MySqlAdapter

Adapter Design pattern with UML diagram

The UML class diagram above includes the following classes, interfaces, and objects:

  • ITarget: This is the interface that the client uses to complete its functionality/request.
  • Adapter: This class implements the ITarget interface and inherits the Adaptee class. It is in charge of communication between the Client and the Adaptee.
  • Adaptee: This is a class that provides the functionality required by the client. However, the interface is incompatible with the client.
  • Client: This class communicates with a type that implements the ITarget interface. However, the communication class named Adaptee is incompatible with the client.

When to use the Adapter Design pattern?

The adapter design pattern is applicable in the following scenarios:

  • Allow a system to use classes from another system that is not compatible with it.
  • Allow communication between new and current systems that are independent of each other.
  • ADO.NET SqlAdapter, OracleAdapter, and MySqlAdapter are the best examples of the Adapter Pattern.

Different types of Adapter design patterns

There are two types of Adapter design patterns:

  1. Class Adapter
  2. Object Adapter

Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this