关闭

Creational Patterns Part 4/5: Builder Pattern

标签: 建造者模式创造型模式设计模式
180人阅读 评论(0) 收藏 举报
分类:

Creational Patterns Part 4/5: Builder Pattern

目录


Definition

Builder Pattern使用一些简单的对象来build一个复杂的对象,逐步完成build的过程。在Factory Pattern中一个method call就可以创建出一个对象,而如果一个对象的创建需要准备很多事情,就可以使用Builder Pattern。

何时使用?创建对象需要事先做很多事时/当构造方法或静态工厂的参数过多时。

使用频率:Frequency of use Medium Low


UML Class Diagram

Builder Pattern


Implementation

现在要组装一台电脑,那么通常需要主板、内存、显卡、屏幕和键盘等设备。也就是说在组装电脑之前,要提前准备好以上这些对象,可以用Builder Pattern来实现。

// Device.java
package designpatterns.creationalpatterns.builder;

public interface Device {
    String name();
    Integer price();
}

// DeviceImpl.java
package designpatterns.creationalpatterns.builder;

class MainBoard implements Device {

    @Override
    public String name() {      
        return "MainBoard";
    }

    @Override
    public Integer price() {    
        return 1000;
    }
}

class Memory implements Device {

    @Override
    public String name() {      
        return "Memory";
    }

    @Override
    public Integer price() {        
        return 500;
    }   
}

class VideoCard implements Device {

    @Override
    public String name() {      
        return "VideoCard";
    }

    @Override
    public Integer price() {        
        return 2000;
    }   
}

class Screen implements Device {

    @Override
    public String name() {      
        return "Screen";
    }

    @Override
    public Integer price() {        
        return 1000;
    }   
}

class KeyBoard implements Device {

    @Override
    public String name() {      
        return "KeyBoard";
    }

    @Override
    public Integer price() {        
        return 500;
    }   
}

// BuilderDemo.java
package designpatterns.creationalpatterns.builder;

import java.util.*;

public class BuilderDemo {
    public static void main(String[] args) {
        ComputerBuilder builder = new ComputerBuilder();
        // The client does't know or care about how the object is created
        Computer computer = builder.assembleComputer();
        computer.showParts();
    }
}

class ComputerBuilder {

    public Computer assembleComputer() {
        Computer computer = new Computer();

        computer.add(new MainBoard());
        computer.add(new Memory());
        computer.add(new VideoCard());
        computer.add(new Screen());
        computer.add(new KeyBoard());

        return computer;
    }
}

class Computer {
    private List<Device> deviceList = new ArrayList<Device>();

    public void add(Device device) {
        deviceList.add(device);
    }

    public void showParts() {
        System.out.println(String.format("%-10s %5s", "Device", "Price"));
        System.out.println("——————————————————");

        Integer totalPrice = 0;
        for(Device device: deviceList) {
            System.out.println(String.format("%-10s %5d", device.name(), device.price()));
            totalPrice += device.price();
        }
        System.out.println("——————————————————");
        System.out.println(String.format("%-10s %5d", "Computer", totalPrice));
    }
}

// output
Device     Price
——————————————————
MainBoard   1000
Memory       500
VideoCard   2000
Screen      1000
KeyBoard     500
——————————————————
Computer    5000
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17439次
    • 积分:926
    • 等级:
    • 排名:千里之外
    • 原创:79篇
    • 转载:0篇
    • 译文:1篇
    • 评论:0条