Reprint please indicate the source:
http://blog.csdn.net/zxt0601/article/details/52848004
This article is from: "Zhang Xudong Blog" an overview
Definition: The adapter pattern transforms the interface of a class into another interface that the client expects, and the purpose of the master is to be compatible, so that two classes that would otherwise not work together because of an interface mismatch can work together. Its alias is the wrapper (Wrapper).
belongs to the structural pattern
There are three main categories: the class adapter mode, the adapter mode of the object, and the adapter mode of the interface.
This article defines:
Classes, interfaces, objects (we have) that need to be adapted, for short, SRC (source)
Final desired output (we want), abbreviated DST (destination, target)
The adapter is called Adapter.
A word describes the sense of adapter mode: SRC->ADAPTER->DST, which is src in some form (three forms corresponding to three adapter modes) to Adapter, eventually converted to DST.
Take our Android to develop the most familiar display list data of the three big controls: Listview,gridview,recyclerview Adapter, their three controls need to be view (DST), and some of us are generally datas (SRC), So adapter adapter is the completion of the data source datas conversion into itemview work.
Brought into the SRC->ADAPTER->DST, i.e. Datas->adapter->view. Usage Scenarios:
1 The system needs to use existing classes, and the interfaces of these classes do not meet the needs of the system.
2 want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future.
3 requires a unified output interface, and the input terminal type is unpredictable. Class Two adapter mode:
Description: Adapter class, by inheriting the SRC class, implementing the DST class interface, complete the SRC->DST adaptation.
Other articles are used in the life of the charger example to explain the adapter, indeed, this is an excellent example, this article can not mundane:
The charger itself is equivalent to adapter,220v alternating current equivalent to SRC, our target DST is 5V DC.
Our existing SRC class:
/**
* Introduction: SRC class: We have 220V voltage
* Author: zhangxutong
* Email: zhangxutong@imcoming.com
* Time: 2016/10/18.
*
/public class Voltage220 {public
int output220v () {
int src =;
System.out.println ("I am" + src + "V");
return src;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
We want the DST interface:
/**
* Introduction: DST interface: 5V voltage required by Customer
* Author: zhangxutong
* Email: zhangxutong@imcoming.com
* Time: 2016/10/18.
*
/public interface Voltage5 {
int output5v ();
}
1 2 3 4 5 6 7 8 9 10 11
Adapter class:
/**
* Introduction: Adapter class: Complete the transformation of the 220v-5v
* by inheriting the SRC class, implementing the DST class interface to complete the SRC->DST adaptation.
* Author: zhangxutong
* Email: zhangxutong@imcoming.com
* Time: 2016/10/18.
*
/public class Voltageadapter extends Voltage220 implements VOLTAGE5 {
@Override public
int output5v () {
int src = output220v ();
SYSTEM.OUT.PRINTLN ("Adapter operation begins to fit voltage");
int dst = SRC/44;
SYSTEM.OUT.PRINTLN ("Output voltage after adapter completion:" + DST);
return DST;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Client class:
/**
* Introduction: Client class: Mobile phone. Requires 5V voltage
* Author: zhangxutong
* e-mail: zhangxutong@imcoming.com
* Time: 2016/10/18.
*
/public class Mobile {
/**
* Charging Method
* *
@param voltage5 */public
void charging (Voltage5 VOLTAGE5) {
if (voltage5.output5v () = = 5) {
System.out.println ("Voltage just 5V, start charging");
} else if ( VOLTAGE5.OUTPUT5V () > 5) {
System.out.println ("The voltage is over 5V, I want to become Note7");}}}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Test code:
System.out.println ("=============== class Adapter ==============");
Mobile mobile = new mobile ();
Mobile.charging (New Voltageadapter ());
1 2 3
Output:
=============== class Adapter ==============
I am the 220V adapter is starting to fit the output voltage after the adaptor is
complete: 5
voltage Just good 5V, start charging
1 2 3) 4 5
The class diagram is as follows:
Summary:
Java is a single-inheritance mechanism, and I don't really like all the people I want to inherit.
So it's a disadvantage that class adapters need to inherit the SRC class.
Because this requires DST to be an interface, there are certain limitations;
and the Src class method will be exposed in the adapter, and also increase the cost of use.
But also because it inherits the Src class, it can rewrite the Src class method according to the requirement, which makes the flexibility of the adapter enhanced. three-Object adapter mode (Common):
The basic idea is the same as the adapter pattern of the class, except that the adapter class is modified, this time not inheriting the SRC class, but holding an instance of the SRC class to solve the compatibility problem.
That is: Hold the SRC class, implement the DST class interface, complete the SRC->DST adaptation.
(based on the "synthetic reuse principle", most of the structural patterns are object-structured patterns, using association relationships as an alternative to inheritance relationships in the system.) )
The adapter class is as follows:
/**
* Introduction: Object Adapter mode:
* holds the SRC class, implements the DST class interface, completes the SRC->DST adaptation. To resolve the issue of * * compatibility * *.
* Author: zhangxutong
* Email: zhangxutong@imcoming.com
* Time: 2016/10/18.
*/Public
class VoltageAdapter2 implements Voltage5 {
private Voltage220 mVoltage220;
Public VoltageAdapter2 (Voltage220 voltage220) {
mVoltage220 = voltage220;
}
@Override public
int output5v () {
int dst = 0;
if (null! = mVoltage220) {
int src = mvoltage220.output220v ();
SYSTEM.OUT.PRINTLN ("Object adapter operation, start fitting voltage");
DST = SRC/44;
SYSTEM.OUT.PRINTLN ("Output voltage after adapter completed:" + DST);
}
return DST;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Test code:
System.out.println ("\n=============== object Adapter ==============");
VoltageAdapter2 voltageAdapter2 = new VoltageAdapter2 (new Voltage220 ());
Mobile Mobile2 = new Mobile ();
Mobile2.charging (VOLTAGEADAPTER2);
1 2 3 4
Output:
=============== Object Adapter ==============
I am the 220V
Object adapter working, starting with the adapter voltage after the completion of the
output voltage: 5
voltage Just good 5V, start charging
1 2 3) 4 5
Class Diagram:
Summary:
Object adapters and class adapters are actually the same idea, but they are implemented in different ways.
According to the principle of synthetic multiplexing, the combination is greater than inheritance,
So it solves the problem that class adapters must inherit the limitations of SRC, and no longer insist that DST must be an interface.
It is also cheaper and more flexible to use.
(and decorator mode may be confused when beginners, here to understand that the decorator is the decoration of SRC, the user is unaware that the SRC has been decorated (user usage unchanged). After the object is adapted, the user's usage is changed.
That is, decorator usage: SETSRC->SETSRC, Object Adapter usage: Setsrc->setadapter.) Four Interface Adapter mode
It is also known as the adapter mode (default Adapter pattern) or the default adapter mode.
Defined:
When you do not need to fully implement the methods provided by the interface, you can design an abstract class implementation interface, and provide a default implementation (empty method) for each method in the interface, then the subclass of the abstract class can selectively overwrite some methods of the parent class to implement the requirements, it is applicable to an interface does not want to use all of its methods.
We go directly to everyone's favorite source: Source- backed Links:
Property animation in Android The Valueanimator class can add listeners through the AddListener (Animatorlistener Listener) method.
Then the general wording is as follows:
Valueanimator valueanimator = Valueanimator.ofint (0,100);
Valueanimator.addlistener (New Animator.animatorlistener () {
@Override public
void Onanimationstart ( Animator animation) {
}
@Override public
void Onanimationend (Animator animation) {
}
@Override Public
void Onanimationcancel (Animator animation) {
}
@Override public
void Onanimationrepeat ( Animator animation) {
}
});
Valueanimator.start ();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Sometimes we do not want to implement all the methods of Animator.animatorlistener interface, we just want to listen to Onanimationstart, we will write as follows:
Valueanimator valueanimator = Valueanimator.ofint (0,100);
Valueanimator.addlistener (New Animatorlisteneradapter () {
@Override public
void Onanimationstart (Animator Animation) {
//xxxx specific implementation
}
});
Valueanimator.start ();
1 2 3 4 5 6 7 8
Obviously, this animatorlisteneradapter class is an interface adapter.
View the adapter class source code: