Design Mode C # Language Description - Adapter (Adapter) mode
* This article refers to some of the "Java and Mode", suitable for beginners for design patterns.
The adapter mode converts a class interface into another interface that the client is expected, so that two classes that cannot be worked together by the interface do not match together can work together. Includes the adapter mode of the class and the adapter mode of the object.
The class adapter mode converts the API of the adapted class into the target class, the static structure diagram is as follows:
The role of the mode is as follows:
Target Role: This is the interface that is expected. Note that the class's adapter mode is discussed here, so the target cannot be a class.
ADAPTEE Role: Exported interfaces that require adaptation.
Adapter Role: Convert the source interface into a target interface.
Target;
Public Interface Target
{
Void sampleOperation1 ();
Void sampleOperation2 (); // Source class is not included
} // end interface definition target
Adaptee;
Public Class Adaptee PUBLIC CLASS Adaptee
{
Public void sampleOperation1 ()
{
}
} // End class definition adaptee
Adapter;
Public Class Adapter: Adaptee, Target
{
Public void sampleOperation2 ()
{
}
} // End class definition adapter
Effect of class adapter mode:
Use a specific class to adapt the source to the target, so that if the source and source subclasses use this type of adaptation, the shape is not connected.
Since the adapter class is a subclass of the source, some of the methods can be replaced in the adapter class.
Similar to the adapter mode of the class, the object's adapter mode converts the API of the adapted class into the API of the target class, different from the adapter mode of the class, the adapter mode of the object is not connected to the Adaptee class with inheritance relationship, but use delegation Relationship, class diagram as follows:
Target;
Public Interface Target
{
Void sampleOperation1 ();
Void sampleOperation2 ();
} // end interface definition target
Adaptee;
Public Class Adaptee PUBLIC CLASS Adaptee
{
Public void sampleOperation1 ()
{
}
} // End class definition adaptee
Adapter:
Public Class Adapter: Target
{
Private adaptee adaptee;
Public void adapter (Adaptee Adaptee)
{
THIS.Adaptee = adaptee;
}
Public void sampleOperation1 ()
{
Adaptee.sampleOperation1 ();
}
Public void sampleOperation2 ()
{
}
} // End class definition adapter
Object adapter mode effect:
A adapter can match a variety of different sources to the same goal. That is, the same adapter can adapt the source class and its subclasses to the target interface.