Builder
The abstract builder can determine by the object passed in which
builder to return.
Both "handlers" preform the exact same function as viewed
from the client perspective. The differance is that two different
types of "handlers" are needed depending on atributes of the string
passed in. This is a contrived example as there is no need to seprate
a long string into parts but imagine this need is real. The "win"
here is that the client class is unaware of any difference in the
way the "handlers" are constructed.
This is the abstract builder. In this example the abstract
builder will determine which type of class to create by the string
passed in.
-----------------------------------------------------
public abstract class AbstractBuilder{
public static AbstractBuilder getInstance(String input){
if(input.length()>35)
return new LargeStringBuilder();
else
return new SmallStringBuilder();
}
public abstract void buildIt(String input);
public abstract IStringHandler getResult();
}
-----------------------------------------------------
This is a concrete builder.
-----------------------------------------------------
class LargeStringBuilder extends AbstractBuilder{
private LargeStringHandler lsh = new LargeStringHandler();
public void buildIt(String input){
lsh.setFirstHalfOfString(input.substring(0,input.length()/2));
lsh.setSecondHalfOfString(input.substring(input.length()/2,input.length()));
}
public IStringHandler getResult(){
return lsh;
}
}
-----------------------------------------------------
This is a concrete builder.
-----------------------------------------------------
class SmallStringBuilder extends AbstractBuilder{
private SmallStringHandler ssh = new SmallStringHandler();
public void buildIt(String input){
ssh.setString(input);
}
public IStringHandler getResult(){
return ssh;
}
}
-----------------------------------------------------
This is the common interface shared by all objects returned by a builder..
-----------------------------------------------------
public interface IStringHandler{
public void displayString();
}
-----------------------------------------------------
This is a concrete handler. The client and director
classes will make calls to this.
-----------------------------------------------------
class LargeStringHandler implements IStringHandler{
private String firstHalfOfString = null;
private String secondHalfOfString = null;
public void displayString(){
System.out.println(firstHalfOfString + secondHalfOfString);
}
public void setFirstHalfOfString(String value){
firstHalfOfString = value;
}
public void setSecondHalfOfString(String value){
secondHalfOfString = value;
}
}
-----------------------------------------------------
This is a concrete handler. the client and director
classes will make calls to this.
-----------------------------------------------------
class SmallStringHandler implements IStringHandler{
private String theWholeString = null;
public void displayString(){
System.out.println(this.theWholeString);
}
public void setString(String value){
this.theWholeString = value;
}
}
-----------------------------------------------------
The director class. This is used by the client to
build up the object the client needs. In this example only
one call is made to build the object up. This class
like the client is unaware of any diference in the
way the object is "built up".
-----------------------------------------------------
class Director{
private AbstractBuilder builder;
public Director(AbstractBuilder builder){
this.builder = builder;
}
public void buildIt(String input){
/*
In here is where you might see
multiple calls to the builder
to assembel/build the different
parts. This example has only the
one call.
*/
builder.buildIt(input);
}
}
-----------------------------------------------------
This is the client class. The abstract builder will
determine which 'type' of builder to return by the
attributes of the object (length in this example)
passed in.
-----------------------------------------------------
class Test{
public static void main(String[] args){
System.out.println("class: Test, method: main");
String one = "the king is gone, but he's not forgotten";
String two = "my my, hey hey";
//FLIP THIS BETWEEN THE TWO STRINGS TO SEE
//THE DIFERENT BUILDERS IN ACTION.
String useExample = two;
AbstractBuilder abstractBuilder = AbstractBuilder.getInstance(useExample);
Director director = new Director(abstractBuilder);
director.buildIt(useExample);
IStringHandler iStringHandler = abstractBuilder.getResult();
iStringHandler.displayString();
}
}
-----------------------------------------------------
Here is a tar with the above java classes in it.
builder.tar