设计模式之构建器

本文介绍了设计模式中的Builder模式,包括定义、特点和应用场景。Builder模式用于一步步创建复杂对象,它将构建过程和表示分离,使得创建过程更加灵活。文章通过代码示例展示了经典Builder模式和变种Builder模式的实现,帮助读者更好地理解和应用Builder模式。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

本人对于设计模式的学习,仅供参考!


一、构建器Builder

1.定义:

Builder模式是一步一步创建一个复杂对象的创建型模式。该模式将构建复杂对象的过程和它的部件解耦,使得构建过程和部件的表示隔离开来。
在这里插入图片描述

2.特点:

1.分离复杂对象的构建和表示。
2.同样的构建过程可以创建不同的表示
3.无需记忆,自然使用

3.应用场景:

1.当对象创建过程比较复杂,或对创建顺序或组合有依赖(经典Builder模式)。
2.当创建对象时所需参数较多,且包含较多可选参数(变种Builder模式)。

二、代码实现

1.经典Builder模式

现有Terrain类,利用构建器模式构建Terrain类。

//地形类
public class Terrain {
    Wall w;
    Fort f;
    Mine m;
}
//墙
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;
    }
}
//暗堡
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;
    }
}
//地雷
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;
    }
}
//构建器接口
public interface TerrainBuilder {
    TerrainBuilder buildWall();
    TerrainBuilder buildFort();
    TerrainBuilder buildMine();
    Terrain build();
}
//构建器实现类
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;
    }
}
//测试结果
public class Main {
    public static void main(String[] args) {
        //如果有SimpleBuilder也可以使用 new SimpleTerrainBuilder()
        TerrainBuilder builder=new ComplexTerrainBuilder();
        //可以非常灵活的指定对象的一部分
        Terrain terrain1=builder.buildWall().buildFort().buildMine().build();
        Terrain terrain2=builder.buildWall().buildFort().build();
    }
}

2.变种Builder模式

现有Person类如下,利用构建器进行构建:

//需要被构建的对象的类
public class Person {
    int id;
    String name;
    int age;
    double weight;
    int score;
    Location loc;
    private Person(){}
}
//地址信息类
class Location{
    String street;
    String roomNo;
    public Location(String street,String roomNo){
        this.street=street;
        this.roomNo=roomNo;
    }
}

以静态内部类的形式为其添加一个构建器:

//需要被构建的对象的类
public class Person {
    int id;
    String name;
    int age;
    double weight;
    int score;
    Location loc;
    private Person(){}
    //构建器类可作静态为内部类使用
    public static class PersonBuilder{
        private 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;
        }
    }
}
//地址信息类
class Location{
    String street;
    String roomNo;
    public Location(String street,String roomNo){
        this.street=street;
        this.roomNo=roomNo;
    }
}

测试结果:

public class Main {
    public static void main(String[] args) {
        //可以非常灵活的指定对象的一部分
        Person person=new Person.PersonBuilder()
                .basicInfo(1,"张三",18)
                .weight(18.0)
                //.score(98)
                //.loc( "樱花街","XXX居33号")
                .build();
    }
}

总结

变种Builder模式目的在于:减少对象创建过程中引入的多个构造函数、可选参数以及多个setter过度使用导致的不必要的复杂性,使代码保持整洁。其缺点也很明显:额外写的代码变多了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值