23 Design Patterns in Java 9-builder mode (builder pattern)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.