Five minutes a design pattern, using the simplest method to describe the design pattern. To see more design patterns, click a design mode series for five minutes
http://blog.csdn.net/daguanjia11/article/category/3259443
Recognize adapter mode
The adapter pattern is defined by converting the interface of one class to another interface that the client wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
The main function of the adapter mode is to convert the interface, the purpose is to reuse the existing functions, instead of implementing a new interface, for the implementation of the function but the interface is incompatible with the occasion. The adapter combines the original object and provides an interface that is compatible with the client, and the actual work is done by the original interface.
Sample code
The following sample code mainly contains these sections:
- Target: The interface required by the client, related to a specific domain
- Adaptee: Adapted Object
- Adapter: Adapter
To the code:
/// <SUMMARY> /// clients require interfaces that are related to a specific domain /// </summary public interface target{ Span class= "hljs-comment" >/// <summary> /// method of Client request processing /// </SUMMARY> void Request ();}
// <summary> /// be adapted to the object// </summary> Public classadaptee{// <summary> ///The original method has been implemented, but the interface has changed // </summary> Public void oldrequest() {Console.WriteLine ("The original method was executed."); }}
// <summary> /// Adapter// </summary> Public classadapter:target{// <summary> /// combination of objects to be adapted // </summary> PrivateAdaptee adaptee; Public Adapter(Adaptee adaptee) { This. adaptee = Adaptee; } Public void Request() { This. Adaptee. Oldrequest (); }}
Let's see how the client uses
class Program{ staticvoid Main(string[] args) { //创建被适配对象 new Adaptee(); //创建客户端需要调用的接口对象 new Adapter(adaptee); //请求处理 target.Request(); }}
Program execution Results:
The original method was executed
Five minutes adapter mode for one design mode