Adapter mode (Adapter), which transforms the interface of a class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
In software development, the data behavior of the system is correct, but the interface does not match, we should consider the adapter, the purpose is to make an existing object outside the control range to match an interface. The adapter pattern is primarily used in situations where you want to reuse some existing classes, but the interfaces are inconsistent with the reuse environment requirements.
The UML diagram for the adapter mode is given below:
Here's a code structure diagram of the adapter pattern:
namespaceconsoleapplication1{//target class, the interface that the customer expects classTarget { Public Virtual voidRequest () {Console.WriteLine ("General Request! "); } } //Adaptee The classes that need to be adapted classAdaptee { Public voidSpecificrequest () {Console.WriteLine ("a special request! "); } } //Adapter (The source interface is converted to the target interface by wrapping an Adapee object inside) classAdapter:target {PrivateAdaptee adaptee =NewAdaptee ();//CV A private Adaptee object Public Override voidRequest () {adaptee. Specificrequest (); //This makes it possible to turn the request () method called on the surface into an actual call to Specificrequest () } } classProgram {Static voidMain (string[] args) {Target target=NewTarget ();//The client can only accept the target classTarget. Request ();//and call the request () method of the target class so that it is only adaptee by adapter white. This is what the adapter mode is forConsole.readkey (); } } }
Use a class that already exists, but if its interface, that is, its method is not the same as what you require, you should consider the adapter mode. However, the adapter mode is a bit too much to mend the feeling, so should be at the beginning of the system design to standardize the interface.
Here's an example of how to play the NBA in "Big Talk design mode":
namespaceconsoleapplication1{Abstract classPlayer {protected stringname; PublicPlayer (stringname) { This. Name =name; } Public Abstract voidAttack (); Public Abstract voidDefense (); } //forwards classForwards:player { PublicForwards (stringName):Base(name) {} Public Override voidAttack () {Console.WriteLine ("striker {0} offensive", name); } Public Override voidDefense () {Console.WriteLine ("striker {0} defensive", name); } } //Center classCenter:player { PublicCenter (stringname):Base(name) {} Public Override voidAttack () {Console.WriteLine ("Center {0} offensive", name); } Public Override voidDefense () {Console.WriteLine ("Center {0} Defense", name); } } //Defender classGuards:player { PublicGuards (stringname):Base(name) {} Public Override voidAttack () {Console.WriteLine ("Defender {0} offense", name); } Public Override voidDefense () {Console.WriteLine ("Defender {0} defense", name); } } //Foreign Center classForeigncenter {Private stringname; Public stringName {Get{returnname;} Set{name =value;} } Public voidOffensive ()//Note that the language of the foreign center is not available, so the attack () method cannot be called through the canonical abstract class player{Console.WriteLine ("Foreign Center {0} offensive", name); } Public voiddefensive () {Console.WriteLine ("Foreign Center {0} defensive", name); } } //Translator Class classTranslator:player {PrivateForeigncenter WJZF =NewForeigncenter (); PublicTranslator (stringname):Base(name) {Wjzf. Name=name; } Public Override voidAttack ()//invoking the translator's attack () is equivalent to calling the foreign center's offense, and the translator class is inherited from the player, so it is compatible with the abstract{Wjzf. offense (); } Public Override voidDefense () {wjzf. Defense (); } } classProgram {Static voidMain (string[] args) {Player B=NewForwards ("Shane Battier"); B.attack (); Player m=NewGuards ("McGrady"); M.attack (); Player ym=NewTranslator ("Yao Ming");//can not directly new foreign center object, because the call Foreign center attack Method is offensive (), the Player class object cannot callym. Attack (); Ym. Defense (); Console.readkey (); } } }
The output results are as follows:
The adapter mode is in. NET, DataAdapter is used as an adapter between a dataset and a data source to retrieve and save data. DataAdapter provides this adapter by mapping fill, which changes the data in the dataset to make it easier for data in the data source to match, and update, which changes the data in the data source to match the data in the dataset.
Adapter mode-Design mode learning