java中23种设计模式之9-建造者模式(builder pattern)

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();
}
}

///////////////////////////////////////

输出结果:

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

时间: 2024-10-06 08:29:46

java中23种设计模式之9-建造者模式(builder pattern)的相关文章

java中23种设计模式之13-组合模式(composite pattern)

import java.util.LinkedList;abstract class Company{ private String name; public void setName(String name) { this.name=name; } public String getName() { return name; } abstract void add(Company company); abstract void remove(Company company); abstract

java中23种设计模式之5-多例模式(multition pattern)

顾名思义,多例模式相对于单例模式,就是可以有多于一个的有限实例,如下列,只能有两个实例. import java.util.ArrayList;import java.util.Random;class Multition{ private final static int MAX_NUMBER=2; private static ArrayList<Multition> aMultitionArrayList=new ArrayList<Multition>(); private

java中23种设计模式之6-外观模式(facade pattern)

概念:为子系统中的一组接口提供一个统一接口.Facade模式定义了一个高层接口,这个接口使得这子系统更容易使用. 应用场景: 1)为一个复杂子系统提供一个简单接口. 2)提高子系统的独立性. 3)在层次化结构中,可以使用Facade模式定义系统中每一层的入口. 以大型娱乐商场提供的休闲一条龙服务为例,包括购物,餐饮,按摩.其中购物,按摩,餐饮作为子系统的组成,提供统一对外接口PackageService 类图如下: class Shopping{ String customer=null; pu

java中23种设计模式之18-原型模式(Prototype pattern)

class Something{ public String something=null;}class Prototype implements Cloneable{ private String name; public Something aSomething=new Something(); public void setName(String name) { this.name=name; } public String getName() { return name; } publi

java中23种设计模式之12-装饰模式(decorator pattern)

class Girl { public void showAppearance() { System.out.println("the girl: face without make up"); } } class TakeFlower extends Girl { Girl girl=null; public TakeFlower(Girl girl) { this.girl=girl; } public void showAppearance() { girl.showAppear

java中23种设计模式之17-状态模式(state pattern)

interface State{ public void handle(StateMachine statemachine);} class Eat implements State{ StateMachine statemachine=null; public void handle(StateMachine statemachine) { System.out.println("eat"); this.statemachine=statemachine; this.statemac

java中23种设计模式之10-代理模式(Proxy pattern)

interface Station{ void sellTickets();}class TrainStation implements Station{ public TrainStation() { } public void sellTickets() { System.out.println("TrainStation selling tickets"); }} class TrainStationProxy implements Station{ private TrainS

java中23种设计模式之4-单例模式(singleton pattern)

作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例类只能有一个实例. 单例类必须自己创建自己的唯一实例. 单例类必须给所有其他对象提供这一实例. 一般是通过private 来描述构造函数,禁止从外部构造对象,通过getInstance来获取唯一的实例. class Singleton{ private static Singleton aSingleton=null; private static int indexSingleton=0; pr

java中23种设计模式之7-适配器模式(adapter pattern)

class Barrel{ public void getOilFromBarrel() { System.out.println("get oil from Barrel ok"); }}class Funnel implements Bottle{ private Barrel aBarrel=new Barrel(); public void getOil() { aBarrel.getOilFromBarrel(); }}interface Bottle{ void getOi

java中23种设计模式之10-桥梁模式(bridge pattern)

abstract class Vehicle{ VehicleImpl aVehicleImpl=null; public Vehicle(VehicleImpl vehicleImpl) { aVehicleImpl=vehicleImpl; } void transport() { aVehicleImpl.transportImpl(); }}interface VehicleImpl{ void transportImpl();}class TransportPeople impleme