适配器模式
The Adpative design pattern converts the interface of a class to another interface clients expect. This design pattern lets classes work together that couldn't otherwise because of incompatible interfaces.
适配器模式将类的接口转换为客户期望的另外一个接口,这种设计模式允许类一起工作,否则由于接口不兼容无法工作。
UML class diagram
组成元素
Target: defines the domain-specific interface that client uses.
Adapter: adapts the interface Adaptee to the Target interface.
Adaptee: defines the existing interface that needs adapting.
Client: collaborates with objects conforming to the Target interface.
The Object Adapter Design Pattern in C#
/// <summary> /// The ITarget defines the domain-specific interface used by the client code. /// This interface needs to be implemented by the Adapter. /// The Client can only see the this interface i.e. the class which implements the ITarget interface. /// </summary> public interface ITarget { void Request(); } /// <summary> /// This is the class that make two incompatible interfaces or system work together. /// The Adapter makes the Adapter's interface compatible with the Target's interface. /// </summary> public class Adapter : ITarget { private Adaptee adaptee = new Adaptee(); public void Request() { //Possibly do some other work and then call sepcificRequest adaptee.SpecificRequest(); } } /// <summary> /// The 'Adaptee' class /// The Adaptee contains some functionality that is required by the client,but this interface is not compatible with the client code. /// </summary> public class Adaptee { public void SpecificRequest() { Console.WriteLine("Called SpecificRequest()"); } }
The Class Adapter Design Pattern in C#
/// <summary> /// The only difference is that the Adapter class now implements the Target interface and is inherited from the Adaptee class. /// In the case of the Object Adapter design pattern,the adapter has a reference to the Adaptee object, and using that reference, it will call the adaptee method. /// But in the case of Class Adapter Design Pattern, the adapter will call the inherited method of the Adaptee class directly. /// </summary> public class ClassAdapter : Adaptee, ITarget { public void Request() { SpecificRequest(); } }
The only difference is that the Adapter class now implements the Target interface and is inherited from the Adaptee class. In the case of the Object Adapter Design Pattern, the adapter has a reference to the Adaptee object, and using that reference, it will call the adaptee methods. But in the case of the Class Adapter Design Pattern, the adapter will call the inherited method of the Adaptee class directly.
When to use the Object Adapter Pattern and when to use the Class Adapter Pattern in C#?
It is completely baed on the situation. For Example, if you have a java class and you want to make it compatible with the dotnet class, then you need to use the object Adapter Design Pattern and the reason is it is not possible to make inheritance. On the other hand, if both classes are within the same project and using the same programming language, and if inheritance is possible, then you need to go for Class Adapter Desgin Pattern.
When to use the Adapter Design Pattern in Real-Time Applications?
- A class needs to be reused that does not have an interface that a client required.
- Allow a system to use classes of another system that is incompatible with it.
- Allow communication between a new and already existing system that is independent of each other.
- Sometimes a toolkit or class library cannot be used because its interface is incompatible with the interface required by an application.