Adapter (adapter)

xiaoxiao2021-04-05  276

Definition: Turn two incompatible classes together, belong to structure mode, need to have two identities of Adaptee and Adaptor (Adapters).

Why? We often encounter to use the two unrequited classes, the first solution is to modify the interfaces of the individual classes, but if we don't have source code, or we are not willing to modify each other for an application Interface. How to do?

Use Adapter to create a hybrid interface (mixed blood) between the two interfaces.

How to use? Implement the Adapter mode, in fact, "Type Regeneration" section of "Think in Java" has been mentioned, there are two ways: Composition and inheritance.

Suppose we have to pile, there are two types: square pile round pile.public class squarepeg {public void insert (String str) {system.out.println ("Squarepeg INSERT (): str);}

}

Public class runk {public void insertintohole (string msg) {system.out.println ("Roundpeg InsertIntoHole ():" msg);}}

There is now an application that requires both square piles and a circular pile. Then we need to integrate these two unrequited classes. We don't want to modify these or source code, then we use adapter Implement this application:

Public class pegadapter extends squarepeg {private roundpeg rugg;

Public Pegadapter (this.Roundpeg = peg;)

Public void insert (String str) {roundpeg.insertintohole (str);}

}

In the above code, Roundpeg belongs to Adaptee, is adapted .pegadapter is Adapter, adapting Adaptee (Passer Roundpeg) and Target (Target Squarepeg). In fact, this is a combination method (Composition) and inheritance Comprehensive Application of Inheritance.

Pegadapter first inherits Squarepeg, then generates an object mode using a combination of New, generates Roundpeg's object Roundpeg, and then recombil the parent class INSERT () method. From here, you also know how different uses new generation objects and uses Extends inheritance to generate objects. The former does not need to modify the original class, and even do not need to know its internal structure and source code.

If you have some Java experience, this model has been discovered frequently.

To further use the PEGADAPTER inherited Squarepeg, if we need two sides inherit, inherit Squarepeg and inherit the Roundpeg, because more inheritance is not allowed in Java, but we can implement two interfaces (Interface)

Public interface IROUNDPEG {public void insertintohole (String MSG);

Public interface isquarepeg {public void insert (String Str);

}

Below is a new RoundPEG and Squarepeg, in addition to the difference in the interface, and there is no difference above. Public class squarepeg imports isquarepeg {public void insert (string str) {system.out.println ("squarepeg insert ():" str);}}}

Public class ruggic Void IroundPeg {public void insertintohole (string msg) {system.out.println ("Roundpeg InsertIntoHole ():" msg);}}

Here is a new Pegadapter called Two-Way Adapter:

public class PegAdapter implements IRoundPeg, ISquarePeg {private RoundPeg roundPeg; private SquarePeg squarePeg; // Constructors public PegAdapter (RoundPeg peg) {this.roundPeg = peg;} // Constructors public PegAdapter (SquarePeg peg) (this.squarePeg = peg ;)

Public void insert (String str) {roundpeg.insertintohole (str);}

}

There is also a kind of Pluggable Adapters that can be dynamically obtained one of several Adapters. With Reflection technology, you can dynamically discovery the public method in the class.

转载请注明原文地址:https://www.9cbs.com/read-132038.html

New Post(0)