The adapter mode is a structure mode that describes how to combine classes or objects to obtain a larger structure.
Adapter mode:
Purpose:Convert a class orProgramTo adapt to the needs of the customer class.
Implementation points:It is implemented by means of inheritance or object combination, namely Class adapter and Object Adapter. It is also a design model that we often unconsciously use.
Code:
Class Targetclass
{
Public Void Upiple ()
{
Console. writeline ( " It works well " );
}
}
Class Classadapter: targetclass
{
Public Void Ipiple ()
{
Console. writeline ( " Use a class adapter to implement a U-shaped Port: " );
Base . Upiple ();
}
}
Class Objectadapter
{
Targetclass T1 = New Targetclass ();
Public Void Ipiple ()
{
Console. writeline ( " Implement the U-type port through the Object Adapter: " );
T1.upiple ();
}
}
// ------------------ Run ----------------------------
Class Program
{
Static Void Main ( String [] ARGs)
{
// If you are limited to certain conditions and can only use the I-port pipe, you can use an adapter to call the U-port pipe.
Classadapter adp1 = New Classadapter ();
Objectadapter adp2 = New Objectadapter ();
Adp1.ipiple ();
Adp2.ipiple ();
Console. Readline ();
}
}