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

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

1 / 5

UML Class Diagram:



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


  • 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.


  • 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.


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


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

Source Code:


No comments: