【学习笔记】设计模式-构造器模式(Builder)

0 设计模式

不了解设计模式的小伙伴可以通过这篇文章了解一下什么是设计模式

https://blog.csdn.net/qq_42874315/article/details/120006447?spm=1001.2014.3001.5502

1 构造器模式

以往构造对象并赋值的方法无非就两种,第一种使用有参构造进行赋值,第二种使用set方法一个一个进行注入。

构造器模式是构建复杂对象用的,一个对象可以有多种组合方式,构建不同组合方式的对象。

不使用构造器构建对象时,如果有些属性没有的话,还得写,或者使用多个set方法进行赋值,使用构造器就不用。

2 实现思路

2.1 Java中典型的Builder模式

在这里插入图片描述

核心思想:就是在需要构造的类中,定义一个静态的私有类(构造器),利用方法对属性进行赋值,最后通过build()方法返回对象即可。

2.2 复杂构造者模式

在这里插入图片描述

与2.1的不同:将构造器抽离出来写在外部

3 需要的类

3.1 Java典型的Builder模式需要的类

  1. 需要构造的实体类

    实体类中需要一个具体的构造类

  2. 构造类中需要聚合一个需要构造的实体类对象

    构造类中需要写各种构造该实体类中不同属性的方法

    构造类中还需要一个构造结束的方法build(),返回的对象是内聚的实体类

  3. 测试类

注意:构造类中方法的返回值均为构造类本身(return this),这样做的目的是为了能够链式构造

3.2 复杂构造者模式需要的类

  1. 构建器接口

    其中包含所有需要构造的属性的方法,返回值均为该构造器

    额外还有一个构造结束的方法build(),返回值为具体需要构造的实体类

  2. 需要构造的实体类

  3. 构造器接口实现类(里面要聚合一个将要构造的具体对象)

    实现构造器接口

    实现接口中的方法(每个方法中编写对于属性的赋值原则,使用聚合的实体类对象的属性进行赋值操作)

    build()方法中返回聚合的实体类

  4. 测试类

    面向构造器去创建对象

4 具体实现

4.1 Java典型的Builder模式具体实现

4.1.1 Person(需要构造的类,构造器放在其内部,用静态内部类表示)

/**
 * 典型的java中builder模式
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:43
 */
public class Person {
    int id;
    String name;
    int age;
    double weight;
    int score;
    Location loc;
    private Person(){}

    public static class PersonBuilder{
        Person p = new Person();
        public PersonBuilder basicInfo(int id,String name,int age){
            p.id = id;
            p.name = name;
            p.age = age;
            return this;
        }
        public PersonBuilder weight(double weight){
            p.weight = weight;
            return this;
        }
        public PersonBuilder score(int score){
            p.score = score;
            return this;
        }
        public PersonBuilder loc(String street,String roomNo){
            p.loc = new Location(street,roomNo);
            return this;
        }
        public Person build(){
            return p;
        }
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", weight=" + weight +
                ", score=" + score +
                ", loc=" + loc +
                '}';
    }
}

4.1.2 Location(Person中以用的类)

/**
 * Person中引用的属性
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:39
 */
public class Location {
    String street;
    String roomNo;

    public Location(String street, String roomNo) {
        this.street = street;
        this.roomNo = roomNo;
    }
}

4.1.3 测试类

/**
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:55
 */
public class Test {
    public static void main(String[] args) {
        // 下面.后面的不想要的可以注掉
        Person p = new Person.PersonBuilder()
                .basicInfo(1,"zhangsan",18)
                // .score(20)
                .weight(200)
                // .loc("bj","23")
                .build();
        System.out.println(p);
    }
}

4.2 复杂构造者模式具体实现

4.2.1 TerrainBuilder(构造器接口)

/**
 * 构建器接口
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:20
 */
public interface TerrainBuilder {
    TerrainBuilder buildWall();
    TerrainBuilder buildFort();
    TerrainBuilder buildMine();
    Terrain build();
}

4.2.2 Terrain(需要构造的实体类)

/**
 * 要构造的实体类
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:25
 */
public class Terrain {
    Wall w;
    Fort f;
    Mine m;
}

Terrain属性引用的三个类

Fort

/**
 * 实体类引用的类型
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:25
 */
public class Fort {
    int x,y,w,h;

    public Fort(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }
}

Mine

/**
 * 实体类引用的类型
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:25
 */
public class Mine {
    int x,y,w,h;

    public Mine(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }
}

Wall

/**
 * 实体类引用的类型
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:25
 */
public class Wall {
    int x,y,w,h;

    public Wall(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }
}

4.2.3 ComplexTerrainBuilder(构造器接口实现类)

/**
 * 构造器接口的实现类
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:30
 */
public class ComplexTerrainBuilder implements TerrainBuilder {
    Terrain terrain = new Terrain();
    @Override
    public TerrainBuilder buildWall() {
        terrain.w = new Wall(10,10,50,50);
        return this;
    }

    @Override
    public TerrainBuilder buildFort() {
        terrain.f = new Fort(10,10,50,50);
        return this;
    }

    @Override
    public TerrainBuilder buildMine() {
        terrain.m = new Mine(10,10,50,50);
        return this;
    }

    @Override
    public Terrain build() {
        return terrain;
    }
}

4.2.4 测试类

/**
 * 测试类
 * @Author ChenJiahao(程序员五条)
 * @Date 2021/9/22 22:35
 */
public class Test {
    public static void main(String[] args) {
        TerrainBuilder builder = new ComplexTerrainBuilder();
        Terrain t = builder.buildFort()
                .buildMine()
                .buildWall()
                .build();
        System.out.println(t);
    }
}

5 思维导图

在这里插入图片描述

6 示例源码地址

https://github.com/ChenJiahao0205/design-pattern/tree/master

最后

我是通过马士兵老师的视频和菜鸟教程学习的,部分内容可能会有雷同

想阅读更多设计模式相关文章,欢迎到我的专栏【设计模式学习笔记】、【设计模式】中去查看

在23篇设计模式文章发布完成之后,我会公开完整的思维导图,点关注,不迷路

感谢大家看到这里,文章如有不足,欢迎大家指出;彦祖点个赞吧彦祖点个赞吧彦祖点个赞吧,欢迎关注程序员五条

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五条Programmer

比心~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值