This repository contains a simple implementation of the Adapter design pattern in C#.
The Adapter design pattern allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
In this implementation:
ITargetInterface: Represents the target interface that the client expects.AdapterClass: Adapts theAdapteeclass to work with theITargetinterface.AdapteeClass: The class that needs to be adapted.
To use the Adapter pattern:
- Create an instance of the
Adapteeclass. - Create an instance of the
Adapterclass, passing theAdapteeinstance to its constructor. - Use the
ITargetinterface to make requests.
Example:
Adaptee adaptee = new Adaptee();
ITarget target = new Adapter(adaptee);
target.Request();The ITarget interface declares the target interface that the client code expects. It typically includes one or more methods that the client can invoke.
The Adapter class is responsible for adapting the Adaptee class to the ITarget interface. Here are the key details:
-
Constructor:
- The class has a constructor that takes an instance of the
Adapteeclass, establishing a connection between the adapter and the adaptee.
- The class has a constructor that takes an instance of the
-
RequestMethod Implementation:- The
Requestmethod is implemented to delegate the request to theSpecificRequestmethod of the encapsulatedAdapteeinstance.
- The
The Adaptee class represents the existing class that needs to be adapted. It contains the specific functionality that the client code wants to use through the ITarget interface.
SpecificRequestMethod:- The class includes a method called
SpecificRequest, which represents the specific functionality provided by theAdaptee.
- The class includes a method called