C # design mode adapter pattern Details _c# Tutorial

Source: Internet
Author: User
Tags abstract wrapper
Subsequent content will include the following architectural patterns:

Adapter mode (Adapter): Match interfaces of different classes synthesis mode (composite): A tree structure The simple and composite decorative mode ( Decorator): Add responsibilities to Objects dynamically proxy mode (proxy): An object representing another Object-sharing Mode (Flyweight): A fine-grained instance used for efficient sharing façade mode (façade): A single class this represents an ENT IRE Subsystem Bridge Model: Separates an object interface from its implementation

First, adapter (Adapter) mode

The adapter pattern transforms the interface of a class into another interface that the client expects, so that two classes that do not match the original interface can work together.

Origin of the name

This is much like a transformer (Adapter), which transforms one voltage into another. Electricity in the United States is 110V and the voltage in China is 220V. If you want to use American electrical appliances in China, you must have a transformer that converts a 220V voltage to a 110V voltage. This transformer is a adapter.

The adapter model is also similar to the packing process of the goods: the real appearance of the packaged goods is concealed and changed by the packaging, so some people call this pattern the packaging (wrapper) mode. In fact, we often write many of these wrapper classes, wrapping up some of the existing classes so that they can meet the needs of the interface.

Two forms of Adapter pattern

Adapter mode has both the adapter mode of the class and the adapter pattern of the object. We will discuss both of these adapter modes separately.

second, the structure of the adapter pattern of the class:


As can be seen from the diagram, the Adaptee class has no request method, and the customer expects this method. In order to enable customers to use the Adaptee class, provide an intermediate link, i.e. class adapter class, adapter class implements the target interface and inherits from Adaptee, The request method of the adapter class encapsulates the Adaptee Specificrequest method and achieves the goal of adaptation.

Because adapter and adaptee are inherited relationships, this determines that the adapter pattern is class.

The roles involved in this adapter pattern include:

Destination (target) Role: This is the interface that the customer expects. Because C # does not support multiple inheritance, target must be an interface, not a class.
Source (adaptee) role: A class that needs to fit.
Adapter (Adapter) Role: Converts the source interface to the target interface. This role must be a class.

three, the class of adapter mode schematic implementation:

The following program gives a schematic implementation of a class's adapter pattern:
Copy Code code as follows:

Class Adapter Pattern--structural example
Using System;

"ITarget"
Interface ITarget
{
Methods
void Request ();
}

"Adaptee"
Class Adaptee
{
Methods
public void Specificrequest ()
{
Console.WriteLine ("Called Specificrequest ()");
}
}

"Adapter"
Class Adapter:adaptee, ITarget
{
Implements ITarget Interface
public void Request ()
{
Possibly do some data manipulation
And then call Specificrequest
This. Specificrequest ();
}
}

/**////<summary>
Client Test
</summary>
public class Client
{
public static void Main (string[] args)
{
Create adapter and place a request
ITarget t = new Adapter ();
T.request ();
}
}

IV. structure of the object's adapter pattern:

As you can see from the diagram: The client needs to invoke the request method, and Adaptee does not, and in order for the client to be able to use the Adaptee class, a wrapper (wrapper) class adapter is required. The wrapper class wraps a Adaptee instance to connect the client to the adaptee. Because adapter and adaptee are delegated, this determines that the adapter pattern is an object.

The roles involved in this adapter pattern include:

Destination (target) Role: This is the interface that the customer expects. A target can be a specific or abstract class, or it can be an interface.
Source (adaptee) role: A class that needs to fit.
Adapter (Adapter) Role: Converts the source interface into a target interface by wrapping (Wrap) A Adaptee object inside the inner package.


Five, the object of the adapter mode of the implementation of the schematic:

The following program gives a schematic implementation of a class's adapter pattern:
Copy Code code as follows:

Adapter pattern--structural example
Using System;

"Target"
Class Target
{
Methods
Virtual public void Request ()
{
Normal implementation goes here
}
}

"Adapter"
Class Adapter:target
{
Fields
Private Adaptee adaptee = new Adaptee ();

Methods
Override public void Request ()
{
Possibly do some data manipulation
And then call Specificrequest
Adaptee. Specificrequest ();
}
}

"Adaptee"
Class Adaptee
{
Methods
public void Specificrequest ()
{
Console.WriteLine ("Called Specificrequest ()");
}
}

/**////<summary>
Client Test
</summary>
public class Client
{
public static void Main (string[] args)
{
Create adapter and place a request
Target t = new Adapter ();
T.request ();
}
}


Six, under what circumstances use the adapter mode
Use adapter mode in the following situations:

1, the system needs to use existing classes, and this kind of interface does not meet the needs of the system.
2. You want to create a reusable class that works with some classes that don't have much to do with each other, including classes that might be introduced in the future. These source classes do not necessarily have very complex interfaces.
3, (for object adapters) in the design, you need to change the interface of many existing subclasses, if you use the adapter mode of the class, it is necessary to make an adapter for each subclass, which is impractical.

examples of 七、一个 practical application of adapter patterns
The following program demonstrates the application of class adapter and object adapter.
Copy Code code as follows:

Example of implementing the Adapter pattern
Using System;

Target
public interface ICar
{
void Drive ();
}

Direct Use without Adapter
public class Ctoyota:icar
{
public void Drive ()
{
Console.WriteLine ("Vroom Vroom, we ' re off at our Toyota");
}
}

Adaptee
public class Ccessna
{
public void Fly ()
{
Console.WriteLine ("Static runup OK, we ' re off with our C172");
}
}

Class Adapter
public class Cdrivablecessna:ccessna, ICar
{
public void Drive () {base. Fly (); }
}

Object Adapter
public class Cdrivablecessna2:icar
{
Private Ccessna m_ocontained;

Public CDrivableCessna2 ()
{
m_ocontained = new Ccessna ();
}

public void Drive () {m_ocontained.fly ();}
}

Client
public class Client
{
public static void Main (string[] args)
{
ICar Ocar = new Ctoyota ();

Console.Write ("Class adapter:driving an automobile");
Ocar.drive ();
Ocar = new Cdrivablecessna ();
Console.Write ("driving a Cessna");
Ocar.drive ();
Ocar = new CDrivableCessna2 ();
Console.Write ("Object adapter:driving a Cessna");
Ocar.drive ();
}
}


Viii. Discussion on the adapter model

The adapter model has the following notable areas for implementation:

1, the target interface can be omitted, the pattern has degraded. But this practice seems mediocre and not mediocre, it can make adaptee do not need to implement unwanted methods (can refer to the default adapter mode). The manifestation is that the parent class implements the default method, and subclasses only need to implement their own unique methods. This is somewhat like a template (Template) pattern.
2. The adapter class can be an abstract class.
3, with the parameters of the adapter mode. With this approach, the adapter class can return a suitable instance to the client based on the parameters.

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.