Core design patterns (6) Adapter adapter Mode

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.