Problems frequently encountered in software development:
Assume that a software system already exists and you want it to work with a new vendor class library, but the interfaces designed by the new vendor are different from those of the old manufacturer. If you do not want to change the existingCodeThe vendor's Code cannot be changed, so how do you solve this problem. The method is actually very simple. It is to write a class and convert the interfaces of new vendors into the interfaces you expect.
This is the adapter mode.
Intent:Converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
Structure:Object Adapter
Class Adapter
Next let's look atExampleObject Adapter:
Prepare a duck first
1 Public Interface Iduck
2 {
3VoidQuack ();//The duck will scream
4VoidFly ();//Ducks fly
5}
Prepare another green duck
1 Public Class Mallardduck: iduck
2 {
3 Public Void Quack ()
4 {
5Console. writeline ("Quack");
6}
7
8 Public Void Fly ()
9 {
10Console. writeline ("I'm flying");
11}
12 }
Prepare a turkey
1 Public Interface Iturkey
2 {
3 Void Gobble (); // The Turkey will not scream, but it will scream
4 Void Fly (); // Turkey fly
5 }
Prepare a wild turkey
1 Public Class Wildturkey: iturkey
2 {
3 Public Void Gobble ()
4 {
5Console. writeline ("Gobble, gobble");
6}
7 Public Void Fly ()
8 {
9Console. writeline ("I'm flying a short distance");
10}
11 }
Now, if you are missing a duck object and want to pretend to be a duck with some turkey objects, what should you do? Obviously, the problem is that the Turkey interface is different from the duck interface, so we can solve it by writing an adapter. The Code is as follows:
1 Public Class Turkeyadapter: iduck // Pack the Turkey into a duck
2 {
3 Iturkey Turkey;
4
5 Public Turkeyadapter (iturkey Turkey)
6 {
7This. Turkey=Turkey;//Turkey instantiation
8}
9
10 Public Void Quack ()
11 {
12Turkey. Gobble ();//The call of a turkey impersonating a duck
13}
14
15 Public Void Fly ()
16 {
17Turkey. Fly ();//A turkey impersonates a duck for flight
18}
19 }
Now let's write a test.Program:
1 Static Void Main ( String [] ARGs)
2 {
3 Mallardduck duck = New Mallardduck ();
4 Wildturkey Turkey = New Wildturkey ();
5 Iduck turkeyadapter = New Turkeyadapter (Turkey );
6
7 Console. writeline ( " The Turkey says " );
8 Turkey. Gobble ();
9 Turkey. Fly ();
10 Console. writeline ( " The duck says " );
11 Testduck (duck );
12 Console. writeline ( " The turkeyadapter says " );
13 Testduck (turkeyadapter );
14
15 Console. Readline ();
16 }
17
18 Static Void Testduck (iduck duck)
19 {
20Duck. Quack ();
21Duck. Fly ();
22}
Effect
Adapter mode Resolution:
From the figure above, we can see the customer's process of using the adapter:
1) The customer sends a request to the adapter by calling the adapter method through the target interface.
2) the adapter uses the adapter interface to convert the request to one or more called interfaces of the adapter.
3) The customer receives the call results, but does not notice that this is the conversion function of the adapter.
Note:
1) the work that the adapter needs to do is proportional to the size of the target interface. The larger the interface, the more work that needs to be done.
2) based on actual needs, we can implement several interfaces for the adapter. In a word, you must use the mode flexibly.
The code of the class adapter is similar to the code of the Object Adapter. You can refer to the structure chart and write it like this. I will not go into details here.
Let's talk aboutDifferences between object adapters and responses:
1) object adapters are combined to provide greater flexibility. Class adapters use inheritance.
2) The Object Adapter can adapt to the subclass of the adapter. The class adapter can only overwrite the behavior of the adapter.
Applicability of the adapter Mode:
1) You want to use an existing class, and its interface does not meet your needs.
2) You want to create a reusable class that can work together with other unrelated classes or unforeseen classes (that is, classes that may not be compatible with certain interfaces.
3) You want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface.