Friday, July 1, 2011

[Tutorial] Common Design Patterns in C# 4.0
Part7: Adapter Pattern

Pattern Name: 
Adapter Pattern

Short Description:
Match interfaces of classes with different interfaces

Usage:
Often used and easy to implement, useful if classes need to work together that have incompatible existing interfaces.

Complexity: 
1 / 5

UML Class Diagram:

image

Explanation:

  • The TradingDataImporter class acts as a client using classes with an existing Connector interface.

image

  • The abstract Adapter class defines the interface that the client class knows and that it can work with.
  • The concrete Adapter classes convert the interface of the incompatible classes into an interface the client expects. They make different existing interfaces work together.

image

  • Here are some examples of different adaptee classes that implement different interfaces. However, the client expects a generic interface that they currently don’t provide. That is why they get wrapped by the concrete adapter classes to make them compatible with the client.

image

  • In the last step we add some code to test the software design and the Adapter implementation.

image

  • When running the example you can see that everything is working as expected and that the correct classes are instantiated during runtime.
image

Source Code:

http://csharpdesignpatterns.codeplex.com/


Share/Save/Bookmark

No comments: