Vs 2008
When the interfaces provided by an existing component (Class Library) are inconsistent with those requested by the current customer system, use the adapter mode to convert the interfaces of existing components to those requested by the customer system.
1. Mode UML diagram
2. Application
Currently, we have a set of existing text logging components that provide a set of clientCodeThe requested interface.
However, the client code requests another set of interfaces. to reuse the existing text logging component, we use the adapter mode.
Textlogger. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Namespace Designpattern. Adapter. BLL {
Public Class Textlogger {
Public Void Writelog ( String Message) {
Console. writeline ("Exception message: {0}", Message );
}
}
}
Ilogger. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Namespace Designpattern. Adapter. BLL {
Public InterfaceIlogger{
VoidWrite (StringMessage );
}
}
Logadapter. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Namespace Designpattern. Adapter. BLL {
Public Class Logadapter: ilogger {
Private Textlogger = New Textlogger ();
Ilogger members # Region Ilogger members
Public Void Write ( String Message) {
Textlogger. writelog (Message );
}
# Endregion
}
}
Client
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using Designpattern. Adapter. BLL;
Namespace Designpattern. Adapter {
Class Program {
Static Void Main ( String [] ARGs) {
StringMessage= "Unknown exception occured";
NewLogadapter (). Write (Message );
}
}
}
Output
3. Thinking
The application describes the application in the most common adapter mode.
Continue to expand. Two-way adapters and plug-in adapters are available.