A pluggable adapter is an adapter that adapts dynamically to one of several classes. Of course, the adapter can adapt only to classes that it can recognize, and usually the adapter decides which class it is adapting to based on differing constructors or setParameter methods.
Java has yet another way for adapters to recognize which of several classes it must adapt to: reflection. You can use reflection to discover the names of public methods and their parameters for any class. For example, for any arbitrary object you can use the getClass method to obtain its class and the getMethods method to obtain an array of the method names.
java 代码
/*
*Adaptee
*/
class Pathfinder
{
public void explore()
{
System.out.println("explore");
}
}
/*
*Target
*/
class Digger
{
public void dig()
{
System.out.println("Dig");
}
}
/*
*Target
*/
class Collector
{
public void collect()
{
System.out.println("collect");
}
}