Adapter design mode

Source: Internet
Author: User

The adapter design mode is to convert the interface of a class to the interface expected by the client, so that the two classes that do not match the original interface can work together.

In reality, this scenario is very easy to encounter. For example, A type of mobile phone charger requires 10 V voltage for charging, and B type mobile phones require 15 V voltage for charging. The price of A-type mobile phones is very expensive, while that of B-type mobile phones is low. The market may see the conversion of the B-type charger into A-type mobile phone matching adapter.

There are two adapter design modes:

Class adapter: Use inheritance to implement Interface Conversion.

Instance adapter: Use a combination to implement Interface Conversion.

Class adapter class diagram: The Role of the class adapter design:

Source: The adaptive class.

Target: the customer's expected interface. Because the adapter needs to inherit the source. Therefore, the target must be an interface.

Adapter: Convert the source interface to the target interface. The adapter must inherit from the source.

Code:

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Namespace Hbb0b0. DesignPatter

{

/// <Summary>

/// Source: Adapter

/// </Summary>

Public class Adaptee

{

/// <Summary>

/// Respond to special requests

/// </Summary>

Public void SpecialRequest ()

{

Console. WriteLine ("Call SpecialRequest ");

}

}

/// <Summary>

/// Target: the interface expected by the client. Must be an Interface

/// </Summary>

Public interface ITarget

{

/// <Summary>

/// The desired response method of the Client

/// </Summary>

Void Request ();

}

/// <Summary>

/// Adapter

/// </Summary>

Public class ClassAdapter: Adaptee, ITarget

{

# Region ITarget Member

/// <Summary>

/// Implement the expected Interface

/// </Summary>

Public void Request ()

{

Base. SpecialRequest ();

}

# Endregion

}

}

Call example:

// Class Adapter

Console. WriteLine ("class adapter ");

ITarget iTarget = new ClassAdapter ();

ITarget. Request ();

Instance adapter class diagram instance adapter design role:

Source: The adaptive class.

Target: The interface the customer expects because the source is encapsulated in the adapter. Therefore, the target can be a class, abstract class, or interface.

Adapter: Convert the source interface to the target interface. Adapter packaging source.

Code:

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Namespace Hbb0b0. DesignPatter

{

/// <Summary>

/// Source: Adapter

/// </Summary>

Public class Adaptee

{

/// <Summary>

/// Respond to special requests

/// </Summary>

Public void SpecialRequest ()

{

Console. WriteLine ("Call SpecialRequest ");

}

}

/// <Summary>

/// Target: the client can expect an interface, either a specific class or an abstract class,

/// It can also be an Interface

/// </Summary>

Public class Target

{

/// <Summary>

/// Expected Method

/// </Summary>

Virtual public void Request ()

{

}

}

}

/// <Summary>

/// Adapter: internal packaging source, which converts the source interface to the Target Interface

/// </Summary>

Public class InstanceAdapter: Target

{

Private Adaptee adaptee = null;

Public InstanceAdapter (Adaptee adt)

{

Adaptee = adt;

}

Public override void Request ()

{

Adaptee. SpecialRequest ();

}

}

}

Call example:

// Object Adapter

Console. WriteLine ("Object Adapter ");

Adaptee adt = new Adaptee ();

Target target = new InstanceAdapter (adt );

Target. Request ();

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.