Creational Patterns Part 4/5: Builder Pattern

原创 2016年05月30日 23:17:59

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
版权声明:本文为博主原创文章,未经博主允许不得转载。

Creational Patterns Part 1/5: Factory Pattern

Creational Patterns Part 1/5: Factory Pattern目录 Creational Patterns Part 15 Factory Pattern 目录 Defin...

Creational Patterns Part 5/5: Prototype Pattern

Creational Patterns Part 5/5: Prototype Pattern目录 Creational Patterns Part 55 Prototype Pattern 目录 D...

Design Pattern - Creational Patterns - Builder Pattern

2007 Section 3, Chapter 2 Builder Pattern Concept Builders usually deal with constru...

Structural Patterns Part 4/7: Decorator Pattern

Structural Patterns Part 4/7: Decorator Pattern目录 Structural Patterns Part 47 Decorator Pattern 目录 D...

Design Pattern - Creational Patterns - Factory Pattern

Section 1, Chapter 1 Factory Pattern Intr: Factories use a base class type to create a class ...

Design Pattern - Creational Patterns - Singleton Pattern

2007 Section 1, Chapter 1 Singleton Pattern Concept The Singleton pattern is a way t...

Design Pattern - Creational Patterns - Abstract Factory Pattern

2007 Section 2, Chapter 2 Abstract Factory Pattern Concept Since...

Behavioral Patterns Part 2/11: Command Pattern

Behavioral Patterns Part 2/11: Command Pattern目录 Behavioral Patterns Part 211 Command Pattern 目录 Def...

Structural Patterns Part 6/7: Flyweight Pattern

Structural Patterns Part 6/7: Flyweight Pattern目录 Structural Patterns Part 67 Flyweight Pattern 目录 D...

Structural Patterns Part 2/7: Bridge Pattern

Structural Patterns Part 2/6: Bridge Pattern目录 Structural Patterns Part 26 Bridge Pattern 目录 Definit...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Creational Patterns Part 4/5: Builder Pattern
举报原因:
原因补充:

(最多只允许输入30个字)