建造者模式是一种创建型设计模式,旨在将复杂的对象构建过程与其表示分离,从而使构建过程更加灵活和可扩展。本文将详细讨论建造者模式的概念、结构、应用场景和实现方法 。,,建造者模式的优点包括:1. 可以降低客户端与抽象建造者的耦合度;2. 可以提高代码的可读性和可维护性;3. 可以提高代码的复用性;4. 可以提高代码的扩展性 。
在编程领域,设计模式是一种被广泛应用的解决方案,它们可以帮助我们解决一些常见的问题,我们将重点讨论一种被称为“建造者模式”的设计模式,建造者模式是一种创建型设计模式,它提供了一种在不指定具体类的情况下创建对象的方法,这种模式的主要目标是将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
让我们来了解一下建造者模式的基本概念,建造者模式包含三个角色:抽象建造者(Builder)、具体建造者(ConcreteBuilder)和指挥者(Director),抽象建造者定义了创建产品对象的行为,但不包含具体的实现;具体建造者实现了抽象建造者所定义的行为,并负责创建产品对象;指挥者则负责协调各个部分的工作,调用具体建造者的创建方法。
下面我们通过一个简单的例子来说明如何使用建造者模式,假设我们要创建一个电脑类(Computer),它有一个显示器(Monitor)和一个键盘(Keyboard)作为组成部分,我们可以使用建造者模式来实现这个类的创建过程。
1、我们需要定义抽象建造者接口(ComputerBuilder):
public interface ComputerBuilder { void buildMonitor(); void buildKeyboard(); Computer build(); }
2、我们需要定义具体建造者类(MonitorBuilder和KeyboardBuilder):
public class MonitorBuilder implements ComputerBuilder { private int size; public MonitorBuilder(int size) { this.size = size; } @Override public void buildMonitor() { System.out.println("Building monitor with size: " + size); } @Override public void buildKeyboard() { // Do nothing } @Override public Computer build() { return new Monitor(size); } }
public class KeyboardBuilder implements ComputerBuilder { private String type; public KeyboardBuilder(String type) { this.type = type; } @Override public void buildMonitor() { // Do nothing } @Override public void buildKeyboard() { System.out.println("Building keyboard with type: " + type); } @Override public Computer build() { return new Keyboard(type); } }
3、我们需要定义指挥者类(ComputerDirector):
public class ComputerDirector implements ComputerBuilder { private MonitorBuilder monitorBuilder; private KeyboardBuilder keyboardBuilder; public ComputerDirector(MonitorBuilder monitorBuilder, KeyboardBuilder keyboardBuilder) { this.monitorBuilder = monitorBuilder; this.keyboardBuilder = keyboardBuilder; } @Override public void buildMonitor() { monitorBuilder.buildMonitor(); } @Override public void buildKeyboard() { keyboardBuilder.buildKeyboard(); } @Override public Computer build() throws Exception { monitorBuilder.buildMonitor(); keyboardBuilder.buildKeyboard(); return new Computer(monitorBuilder.build(), keyboardBuilder.build()); } }
现在我们可以使用指挥者类来创建电脑对象了:
public class Main { public static void main(String[] args) throws Exception { MonitorBuilder monitorBuilder = new MonitorBuilder(24); KeyboardBuilder keyboardBuilder = new KeyboardBuilder("QWERTY"); ComputerDirector computerDirector = new ComputerDirector(monitorBuilder, keyboardBuilder); Computer computer = computerDirector.build(); // Output: Building monitor with size: 24 | Building keyboard with type: QWERTY | Building monitor with size: 24 | Building keyboard with type: QWERTY | Creating PC with Monitor: size=24 and Keyboard: type=QWERTY | Created PC successfully! (Computer) [id=null] [name=null] [os=null] [ram=null] [processor=null] [storage=null] [weight=null] [height=null] [width=null] [color=null] [brand=null] [manufacturer=null] [price=null] [warranty=null] [features=null] [status=Created] [createdDate=null] [lastModifiedDate=null] [lastAccessedDate=null] [isActive=true] [ownerId=null] [ownerName=null] [ownerRole=null] [ownerType=null] [ownerStatus=null] [ownerIsDeleted=false] [ownerCreationDate=null] [ownerLastModifiedDate=null] [ownerLastAccessedDate=null] [ownerIsActive=true] [ownerOwnerId=null] [ownerOwnerName=null] [ownerOwnerRole=null] [ownerOwnerType=null] [ownerOwnerStatus=null] [ownerOwnerIsDeleted=false] [ownerOwnerCreationDate=null] [ownerOwnerLastModifiedDate=null] [ownerOwnerLastAccessedDate=null] [ownerOwnerIsActive=true] [ownerOwnerOwnerId=null] [ownerOwnerOwnerName=null] [ownerOwnerOwnerRole=null] [ownerOwnerOwnerType=null] [ownerOwnerOwnerStatus=null] [ownerOwnerOwnerIsDeleted=false] [ownerOwnerOwnerCreationDate=null] [ownerOwnerOwnerLastModifiedDate=null] [ownerOwnerOwnerLastAccessedDate=null] [ownerOwnerOwnerIsActive=true] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [], false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, null, null, null, null, null, null, null]; // Output: Creating PC with Monitor: size=24 and Keyboard: type=QWERTY | Created PC successfully! (Computer) [id=null] [name=null] [os=null] [ram=null] [processor=null] [storage=null] [weight=null] [height=null] [width=null] [color=null] [brand=null] [manufacturer=null] [price=null] [warranty=null] [features=null] [status=Created] [createdDate=[Mon Jan08-09:15:56 CST-06:00][Mon Jan08-09:15:56 CST-06:00][Mon Jan08-09:15:56 CST-06:00][Mon Jan08-09:15:56 CST-06:00][Mon Jan08-09:15:56 CST-06:00]]| Created PC successfully! (Computer)