建造者模式
作者:互联网
建造者模式
简介
什么是创建型模式?
创建型模式(Creational Pattern)关注点是如何创建对象,其核心思想是要把对象的创建和使用相分离。这样使得两者能够相对独立地变换。
创建型模式对类的实例化过程进行了抽象,能够将软件模块中对象的创建和对象的使用分离。为了使软件的结构更加清晰,外界对于这些对象只需要知道它们共同的接口,而不清楚器具体的实现和组合细节,使得整个系统的设计更加单一职责原则。
创建型模式在创建什么(What),由谁创建(Who),何时创建(When)等方面都为软件设计者提供了尽可能大的灵活性。
创建型模式隐藏了类的实例的创建细节,通过隐藏对象如何被创建和组合在一起达到整个系统独立的目的。
什么是建造者模式?
建造者模式(Builder Pattern)是常见设计模式的一种,它使用多个简单的对象一步一步构建一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式
一个Builder类会一步步构造最终的对象,该Builder类是独立于其他对象的。
典型应用
肯德基点餐,Java中的StringBuilder, lombook中的@Builder
使用场景
1.需要生成的对象具有复杂的内部结构
2.需要生成的对象内部属性本身互相依赖
3.基本部件不变,而其组合经常变
优缺点对比
优点:1.建造者独立,易扩展 2.便于控制细节风险
缺点:1.产品必须有共同点,范围有控制 2.如果内部变化复杂,会有很多的建造类
实践
个人感觉菜鸟上的示例挺好的,可以看下https://www.runoob.com/design-pattern/builder-pattern.html
以下为跟着狂神一起写的例子
1.造房子示例
- 构建产品
package com.example.designPattern23.builder.demo0;
/*
产品:房子
*/
public class Product {
public String buildA;
public String buildB;
public String buildC;
public String buildD;
public String getBuildA() {
return buildA;
}
public void setBuildA(String buildA) {
this.buildA = buildA;
}
public String getBuildB() {
return buildB;
}
public void setBuildB(String buildB) {
this.buildB = buildB;
}
public String getBuildC() {
return buildC;
}
public void setBuildC(String buildC) {
this.buildC = buildC;
}
public String getBuildD() {
return buildD;
}
public void setBuildD(String buildD) {
this.buildD = buildD;
}
@Override
public String toString() {
return "Product{" +
"buildA='" + buildA + '\'' +
", buildB='" + buildB + '\'' +
", buildC='" + buildC + '\'' +
", buildD='" + buildD + '\'' +
'}';
}
}
2.抽象builder
package com.example.designPattern23.builder.demo0;
public abstract class Builder {
abstract void buildA(); //地基
abstract void buildB(); //钢筋工程
abstract void buildC(); //铺电线
abstract void buildD(); //粉刷
abstract Product getProduct();
}
3.具体建造者
package com.example.designPattern23.builder.demo0;
public class Worker extends Builder {
public Product product;
public Worker() {
product = new Product();
}
@Override
void buildA() {
product.setBuildA("水泥");
}
@Override
void buildB() {
product.setBuildB("钢筋工程");
}
@Override
void buildC() {
product.setBuildC("铺电路");
}
@Override
void buildD() {
product.setBuildD("粉刷");
}
@Override
Product getProduct() {
return product;
}
}
4.指挥者
package com.example.designPattern23.builder.demo0;
// 指挥是核心。负责指挥构建一个工程。工程如何构建,由他确定
public class Director {
public Product build(Builder builder) {
builder.buildA();
builder.buildB();
builder.buildC();
builder.buildD();
return builder.getProduct();
}
}
5.测试类
package com.example.designPattern23.builder.demo0;
public class Test {
public static void main(String[] args) {
Director director = new Director();
Product product = director.build(new Worker());
System.out.println(product);
}
}
2.麦当劳点餐
与示例一的区别在于没有指挥者,客户为知会者
- 产品
package com.example.designPattern23.builder.demo1;
public class Product {
public String buildA = "可乐";
public String buildB = "鸡翅";
public String buildC = "薯条";
public String buildD = "汉堡";
public String getBuildA() {
return buildA;
}
public void setBuildA(String buildA) {
this.buildA = buildA;
}
public String getBuildB() {
return buildB;
}
public void setBuildB(String buildB) {
this.buildB = buildB;
}
public String getBuildC() {
return buildC;
}
public void setBuildC(String buildC) {
this.buildC = buildC;
}
public String getBuildD() {
return buildD;
}
public void setBuildD(String buildD) {
this.buildD = buildD;
}
@Override
public String toString() {
return "Product{" +
"buildA='" + buildA + '\'' +
", buildB='" + buildB + '\'' +
", buildC='" + buildC + '\'' +
", buildD='" + buildD + '\'' +
'}';
}
}
- 抽象建造者
package com.example.designPattern23.builder.demo1;
public abstract class Builder {
abstract Builder buildA(String msg);//可乐
abstract Builder buildB(String msg);//鸡翅
abstract Builder buildC(String msg);//薯条
abstract Builder buildD(String msg);//汉堡
abstract Product getProduct();
}
- 具体建造者
package com.example.designPattern23.builder.demo1;
public class Worker extends Builder {
private Product product;
public Worker() {
product = new Product();
}
@Override
Builder buildA(String msg) {
product.setBuildA(msg);
return this;
}
@Override
Builder buildB(String msg) {
product.setBuildB(msg);
return this;
}
@Override
Builder buildC(String msg) {
product.setBuildC(msg);
return this;
}
@Override
Builder buildD(String msg) {
product.setBuildD(msg);
return this;
}
@Override
Product getProduct() {
return product;
}
}
- 测试类
package com.example.designPattern23.builder.demo1;
public class Test {
public static void main(String[] args) {
Worker worker = new Worker();
// System.out.println(worker.getProduct());
System.out.println(worker.buildA("全家桶").buildB("鸡腿").getProduct());
}
}
标签:String,建造,模式,buildD,buildB,buildC,buildA,public 来源: https://www.cnblogs.com/Oh-mydream/p/16067897.html