==> AdapterDemo.java <==
/**
* Class Adapter Design Pattern example for COM1204 Summer 2003 - Tester/Driver.
*
This test uses the OldProvider and then the EnhancedProvider,
* which adapts the call to use an already ExistingService class.
*
* @author Bob Futrelle
* @version 0.1, 4 July 2003
*
*/
public class AdapterDemo {
public static void main(String[] args) {
// Create a Client handing it an OldProvider.
//Then call its getService() method.
Provider oldProvider = new OldProvider();
Client client1 = new Client(oldProvider);
System.out.println("Using the basic Provider without an Adapter:");
client1.getService();
// Then create one handing it an EnhancedProvider
// and then call its getService() method.
Provider useExisting = new EnhancedProvider();
Client client2 = new Client(useExisting);
System.out.println("\nUsing an Adapter to access an existing service.");
client2.getService();
} // main()
} // class AdapterDemo
// Output from the run above:
/*
*/
==> Client.java <==
/**
* The Client simply uses the Provider interface object given it.
*
Class Adapter Design Pattern example for COM1204 Summer 2003.
*
* @author Bob Futrelle
* @version 0.1, 4 July 2003
*
*/
public class Client {
// This references an interface only.
// It does not know the actual class.
Provider provider;
/** Brings in a normal or adapted Provider object.
*/
public Client(Provider provider){
this.provider = provider;
}
/** Calls a service, which depends on whether it's adapted or not
*/
public void getService() {
provider.doYourThing();
}
} // class Client
==> EnhancedProvider.java <==
/**
* This is the EnhancedProvider which adapts an ExistingService to the Provider method.
*
Class Adapter Design Pattern example for COM1204 Summer 2003.
*
* @author Bob Futrelle
* @version 0.1, 4 July 2003
*
*/
public class EnhancedProvider extends ExistingService implements Provider {
public void doYourThing(){
usefulMethod();
}
} // class EnhancedProvider
==> ExistingService.java <==
/**
* This is the ExistingService class which provides a "useful method".
* Knows nothing about the Provider interface and its doYourThing() method.
*
Class Adapter Design Pattern example for COM1204 Summer 2003.
*
* @author Bob Futrelle
* @version 0.1, 4 July 2003
*
*/
public class ExistingService {
void usefulMethod(){
System.out.println("I'm an existing service.");}
} // class ExistingService
==> OldProvider.java <==
/**
* This is the OldProvider implemementation of the interface -- nothing adapted here.
*
Class Adapter Design Pattern example for COM1204 Summer 2003.
*
* @author Bob Futrelle
* @version 0.1, 4 July 2003
*
*/
public class OldProvider implements Provider {
public void doYourThing(){
System.out.println("Do'in the old thing.");
}
} // class OldProvider
==> Provider.java <==
/**
* This is the Provider interface -- only thing the Client sees and uses.
*
Class Adapter Design Pattern example for COM1204 Summer 2003.
*
* @author Bob Futrelle
* @version 0.1, 4 July 2003
*
*/
public interface Provider {
void doYourThing();
} // interface Provider