Interface
Here is the interface
-----------------------------------------------------
public interface Ithing{
public String getName();
public String getType();
}
-----------------------------------------------------
Here is the class that implemnts the interface
-----------------------------------------------------
class TheRealThing implements Ithing{
public String getName(){return "coke";}
public String getType(){return "soft drink";}
}
-----------------------------------------------------
Here is the class that is decoupled from
TheRealThing. This class need not change when a different
implementation of Ithing is provided.
Notice there are only references to the Ithing interface.
-----------------------------------------------------
class Decoupled{
Ithing thing = null;
public void setThing(Ithing thing){
this.thing = thing;
}
public void getThingDetails(){
if(thing!=null){
System.out.println(thing.getName());
System.out.println(thing.getType());
}
}
}
-----------------------------------------------------
This is a test stub to run everything.
-----------------------------------------------------
class Test{
public static void main(String[] args){
System.out.println("class: Test, method: main");
Decoupled decoupled = new Decoupled();
TheRealThing aThing = new TheRealThing();
decoupled.setThing(aThing);
decoupled.getThingDetails();
}
}
-----------------------------------------------------
Here is a tar with the above java classes in it.
interface.tar