Design Pattern series-adapter Pattern

Source: Internet
Author: User

Story
With the increasing number of followers, Xiao Xue has become a popular lover. Light snow's mobile phone is becoming more and more powerful, and it also adds a lot of convenience to the small snow engaged in foreign trade work. I think that in the near future, the classic design pattern will surely make a "pattern lady" that is even better than "Sister Furong ".

But now Xiaoxue has encountered new troubles. At the product fair the day before yesterday, Xiao Xue met a Japanese businessman who invested in China. After several hours of conversation, the beautiful, intelligent, and kind of light snow impressed the Japanese merchant and left communication methods for each other. After the event, Xiao Xue remembered that her mobile phone did not support Japanese display. What should I do?

We recommend that you add a function to translate a Japanese text message into a Chinese text message without changing the original function, and then send it in the original way.

Adapter mode solution
The adapter mode becomes the wrapper mode, which encapsulates the interface of a class into another interface that the client expects, so that the two classes that cannot work together because the original interface does not match can work together. The adapter mode can be a class adapter mode or an Object Adapter mode. I will use two different adapter modes to meet the requirements of light snow.

1) the adapter mode of the class, the role to be defined:
Target role (ijapanmessage): This is the interface that the customer expects.
Source roles (Nokia, Motorola, and Sankey): classes to be adapted.
For implementation code, refer to design pattern Series 2-factory method pattern
Adapter roles (nokiaadapter, javaslaadapter, and sansungadapter): Convert the source interface to the target interface. This role must be a class and inherit the source role class.

The target role interface is as follows:

Namespace Xiaoxue
{
Public interface ijapanmessage
{
Void sendjapanmessage (Object japanmsg );
}
}

The adapter's Angle class is as follows:

Namespace Xiaoxue
{
Public class nokiaadapter: Nokia, ijapanmessage
{
Public void sendjapanmessage (Object japanmsg)
{
// Translate Japanese MSG into Chinese msg

Base. sendmessage (MSG );
}
}

Public class extends laadapter: Motorola, ijapanmessage
{
Public void sendjapanmessage (Object japanmsg)
{
// Translate Japanese MSG into Chinese msg

Base. sendmessage (MSG );
}
}

Public class sansungadapter: sansung, ijapanmessage
{
Public void sendjapanmessage (Object japanmsg)
{
// Translate Japanese MSG into Chinese msg

Base. sendmessage (MSG );
}
}
}

Due to the addition of the adapter role, the factory class is as follows:

Namespace Xiaoxue
{
Public class japanmessagefactory
{
Public static ijapanmessage createjapanmessage (string type)
{
Ijapanmessage message = NULL;
Switch (type)
{
Case "Nokia ":
Message = new nokiaadapter ();
Break;
Case "Motorola ":
Message = new rabbitlaadapter ();
Break;
Case "Sankey ":
Message = new sansungadapter ();
Break;
}
Return message;
}
}
}

Client call method:

IMessage message = Japan messagefactory. createjapanmessage ("Nokia ");
// Send a Japanese text message
(Ijapanmessage) Message). sendjapanmessage ("too many attempts have been made before sending messages ");

2) Object Adapter Mode
Target role (ijapanmessage): This is the interface that the customer expects.
Source roles (Nokia, Motorola, and Sankey): classes to be adapted.
For implementation code, refer to design pattern Series 2-factory method pattern
SuitableWorker roles (nokiaadapter, javaslaadapter, and sansungadapter): encapsulate (WRAP) Source role objects by means of aggregation to convert the source interface to the target interface.

Target role interface is the same as above

The adapter role is as follows:

Namespace Xiaoxue
{
Public class nokiaadapter: ijapanmessage
{
Private Nokia _ Nokia;
Public nokiaadapter (Nokia)
{
This. _ Nokia = Nokia;
}
Public void sendjapanmessage (Object japanmsg)
{
// Translate Japanese MSG into Chinese msg
_ Nokia. sendmessage (MSG );
}
Public void sendmessage (Object MSG)
{
_ Nokia. sendmessage (MSG );
}
}

Public class implements laadapter: ijapanmessage
{
Private Motorola _ Motorola;
Public worker laadapter (Motorola)
{
This. _ Motorola = Motorola;
}
Public void sendjapanmessage (Object japanmsg)
{
// Translate Japanese MSG into Chinese msg
_ Motorola. sendmessage (MSG );
}
Public void sendmessage (Object MSG)
{
_ Motorola. sendmessage (MSG );
}
}

Public class sansungadapter: ijapanmessage
{
Private Sankey _ Sankey;
Public sansungadapter (santheastsanadapter)
{
This. _ Sankey = Sankey;
}
Public void sendjapanmessage (Object japanmsg)
{
// Translate Japanese MSG into Chinese msg
_ Sankey. sendmessage (MSG );
}
Public void sendmessage (Object MSG)
{
_ Sankey. sendmessage (MSG );
}
}
}

The factory class implementation is as follows:

Namespace Xiaoxue
{
Public class japanmessagefactory
{
Public static ijapanmessage createjapanmessage (string type)
{
Ijapan message Japan message = NULL;
Switch (type)
{
Case "Nokia ":
Japan message = new nokiaadapter (new Nokia ());
Break;
Case "Motorola ":
Japanmessage = new motorolaadapter (New Motorola ());
Break;
Case "Sankey ":
Picture = new sansungadapter (New santure ());
Break;
}
Return japanmessage;
}
}
}

Client call method:

Ijapan message = Japan messagefactory. createjapanmessage ("Nokia ");
// Send a Japanese MMS Message
Message. sendjapanmessage ("too many attempts have been made before sending messages ");

To better describe the adapter mode, we reference a life example: the input voltage of my DELL notebook is 19.5 V, but our daily voltage is generally 220 V, then I have to use a dedicated notebook power to convert 19.5 V to V, then this notebook power is adapter.

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.