builder---构建复杂对象

在这里插入图片描述

在这里插入图片描述

这个模板方法非常像,这里强调的是构建对象。模板方法强调的是方法执行。

public interface TerrainBuilder {   //地形构建器

    TerrainBuilder buildWall();     //墙
    TerrainBuilder buildFort();     //碉堡
    TerrainBuilder buildMine();     //地雷

    Terrain build();        //地形

}

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 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 Person {       //可能由几十,几百个属性,每次构建时可能需要其中不同几个
    int id;
    String name;
    int age;
    double weight;
    int score;
    Location loc;

    private Person() {
    }

    public static class PersonBuilder{  //PersonBuilder是Person的内部静态类,只用来构建Person, 别人调不了。
        Person p = new Person();    //是内部类,所以可访问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) {
        TerrainBuilder builder = new ComplexTerrainBuilder();
        Terrain t = builder.buildWall().buildFort().buildMine().build();

        Person person = new Person.PersonBuilder().basicInfo(1, "zhangsan", 18)
                .score(20)		//score, weight , loc 随意组合,不需要隐掉即可
                .weight(200)
                .loc("beijing", "302")
                .build();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值