Design Pattern learning Summary-adapter Pattern)

Source: Internet
Author: User

Problem:
In the object-oriented software design, it is often necessary to put some existing objects in a new environment or work with new objects. However, their interfaces are different from interfaces required by the new environment, both parties cannot change.
Definition:
The adapter mode is also called a packaging style or packaging, which converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
Intent:
Define an adapter role to implement the same interface with an existing adaptee class by means of inheritance or packaging, convert to another interface that the customer wants, so that classes that cannot work together due to incompatibility of the interface can work together.
Participants:
• Target role:
This is the interface the customer expects. The target can be a specific or abstract class or interface.
• Source (adaptee) role:
The class to be adapted.
• Adapter role:
The source interface is converted to the Target Interface by an adaptee object in inheritance or internal packaging (WRAP. This role must inherit the target.

Category
The adapter mode varies a lot and can be adapted by means of inheritance and combination. The adapter can be a set of adapter products or an abstract type.
• Class adapter:
Provides a class to implement this interface, extends existing classes, and creates subclasses to implement adaptation.

• Object Adapter:
The combination of object adapters not only meets the user expectation interface, but also reducesCodePoor coupling.

Instance description:

Nokia mobile phone Factory
There are two production workshops in the production plant, with different production methods. The following example uses the Object Adapter to adapt to the Production Workshop 1 and the class adapter to adapt to the Production Workshop 2, let them have a common customer-oriented interface.

The UML diagram is as follows:

 

Code:

///   <Summary>
/// N8 mobile phone abstract product class, target role, client call interface
///   </Summary>
Public Interface Iphonefactory
{
Void Create ( String Name, Int Count );
}

/// <Summary>
///Role of adaptee in No. 1 production workshop
/// </Summary>
Public ClassPhonefactory1
{
Public IntCount {Get;Set;}

Public Void Create ( String Name)
{
System. Console. writeline ( " Production of {0} {1} mobile phones " , This . Count, name );
}
}
///   <Summary>
/// Role of adaptee in workshop 2
///   </Summary>
Public Class Phonefactory2
{
Public Void Build ( String Name, Int Count)
{
System. Console. writeline ( " Production of {0} {1} mobile phones " , Count, name );
}
}
///   <Summary>
/// Adapter 1 is used to adapt to No. 1 production workshop. By wrapping an adaptee object in the internal container (WRAP), the source interface is converted to the target interface.
///   </Summary>
Public Class Factoryadapter1: iphonefactory
{
Phonefactory1 phonefactory1 = New Phonefactory1 ();

Public Void Create ( String Name, Int Count)
{
Phonefactory1.count = count;
Phonefactory1.create (name );
}
}
///   <Summary>
/// Adapter 2 is used to adapt to Workshop 2. By inheriting an adaptee object, the source interface is converted to the target interface.
///   </Summary>
Public Class Factoryadapter2: phonefactory2, iphonefactory
{
Public Void Create ( String Name, Int Count)
{
Base . Build (name, count );
}
}
///   <Summary>
/// Client Test
///   </Summary>
Public Void Adaptertest ()
{
Iphonefactory pF = New Factoryadapter1 ();
PF. Create ( " N8 " ,10 );

PF =NewFactoryadapter2 ();
PF. Create ("N8",10);
}

Advantages:
• It is easy to implement code reuse and class library migration.
• Because adapter is a subclass of adaptee, You can redefine some behavior of adaptee by rewriting.
• The Object Adapter allows an adapter to work with multiple adaptee, that is, the adaptee itself and all its subclasses.

Disadvantages:

• You need to change (adapt) the interfaces of multiple existing sub-classes. If you use the class adapter mode, you need to create an adapter for each class.
• Class adapters use the "Multi-inheritance" implementation method, resulting in poor high coupling.

Application Scenario:

• The system needs to use existing classes, and such interfaces do not meet the requirements of the system.

• You want to create a reusable class for some classes that are not highly correlated with each other, including some classes that may be introduced in the future.
PS:

The key to successful use of the adapter mode is whether the Code itself is based on Interface Programming. If not, the adapter is powerless.

The adapter mode is mainly used in scenarios where you want to reuse some existing classes, but the interfaces are inconsistent with the requirements of the reuse environment. It is useful in legacy code reuse and class library migration.

The adapter mode can be very implemented. With the class adapter and Object Adapter, you can also use the "existing object" in the adapter mode as a new interface method parameter to adapt.
Class adapters use the "Multi-inheritance" implementation method, resulting in poor high coupling, so it is generally not recommended. The Object Adapter adopts the "Object combination" method, which is loosely coupled.

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.