1. Effect of adapter mode
assume that a developed class and its specific method exist, but there is an inconsistency between the existing interface and the reuse method, at this time, we must use a mechanism to convert existing methods into other acceptable methods. This is the adapter pattern.
(adapter pattern is the simplest and most intuitive mode in all modes)
2 adapter mode classification
adapter mode implementation is divided into two ways.
one is to create an inherented from adaptee adapter class, which of course implements the target interface that meets the client requirements. Therefore, the relationship between adapter and adaptee is-. The class diagram is as follows:
another method is to define an adaptee object in the adapter class. Use the adaptee object to implement the methods in the target interface. Therefore, the relationship between adapter and adaptee is has-. The class diagram is as follows:
3. Code implementation:
is-a adapter pattern implementation :
Public interface target
{< br> Public void requestmethod ();
}< br> public class adaptee
{< br> Public void existedmethod ()
{< br> console. writeline ("This Is A original method");
}< BR >}
public class adatper: Target, adaptee
{< br> Public void requestmethod ()
{< br> console. write ("the method is modified");
base. existedmethod ();
}< BR >}
has-a adapter pattern implementation:
Public interface target
{< br> Public void requestmethod ();
}< br> public class adaptee
{< br> Public void existedmethod ()
{< br> console. writeline ("This Is A original method");
}< BR >}
Public class adatper: Target
{
Protected adaptee adapteeobj;
Public void requestmethod ()
{
Adapteeobj = new adaptee ();
Adapteeobj. existedmethod ();
}
}