Class computer
{
Private String Name=null;
Private String Cpu=null;
Private String Ddr=null;
Private String Hdd=null;
public void SetName (String name)
{
This.name=name;
}
public void SetCpu (String CPU)
{
THIS.CPU=CPU;
}
public void Setddr (String DDR)
{
THIS.DDR=DDR;
}
public void Sethdd (String HDD)
{
THIS.HDD=HDD;
}
public void Printinfo ()
{
SYSTEM.OUT.PRINTLN (name+ "Computer profile is:" + "cpu->" +cpu+ ", ddr->" +ddr+ ", hdd->" +HDD ");
}
}
Abstract class Builder
{
Private computer acomputer=new computer ();
public abstract void buildcpu ();
public abstract void buildddr ();
public abstract void buildhdd ();
Public abstract computer getproduct ();
}
Class T60builder extends Builder
{
Private computer acomputer=null;
Public T60builder ()
{
Acomputer=new computer ();
Acomputer.setname ("T60");
}
public void Buildcpu ()
{
ACOMPUTER.SETCPU ("Core Duro 2.0GHZ");
}
public void Buildddr ()
{
Acomputer.setddr ("Kingston 1G DDR");
}
public void Buildhdd ()
{
ACOMPUTER.SETHDD ("WD 160GB DISK");
}
Public Computer getproduct ()
{
return acomputer;
}
}
Class T61builder extends Builder
{
Private computer acomputer=null;
Public T61builder ()
{
Acomputer=new computer ();
Acomputer.setname ("T61");
}
public void Buildcpu ()
{
ACOMPUTER.SETCPU ("Core Duro 2.3GHZ");
}
public void Buildddr ()
{
Acomputer.setddr ("Kingston 2G DDR");
}
public void Buildhdd ()
{
ACOMPUTER.SETHDD ("WD 250GB DISK");
}
Public Computer getproduct ()
{
return acomputer;
}
}
Class Workerdirector
{
Private Builder Abuilder=null;
public void Constructorcomputer (builder builder)
{
Abuilder=builder;
Abuilder.buildcpu ();
Abuilder.buildddr ();
ABUILDER.BUILDHDD ();
}
}
public class Builderpatterntest
{
public static void Main (string[] args)
{
Builder abuilder=new T60builder ();
Workerdirector aworkerdirector=new workerdirector ();
Aworkerdirector.constructorcomputer (Abuilder);
Computer acomputer=abuilder.getproduct ();
Acomputer.printinfo ();
Abuilder=new T61builder ();
Aworkerdirector.constructorcomputer (Abuilder);
Acomputer=abuilder.getproduct ();
Acomputer.printinfo ();
}
}
///////////////////////////////////////
Output Result:
T60 Computer profile Is:cpu->core duro 2.0GHZ, Ddr->kingston 1G DDR, HDD->WD 160GB DISK
T61 Computer profile Is:cpu->core duro 2.3GHZ, Ddr->kingston 2G DDR, HDD->WD 250GB DISK
23 Design Patterns in Java 9-builder mode (builder pattern)