JAVA设计模式(建造者模式)

概念: 建造者模式也称为生成器模式(Builder Pattern),将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。简单来说就是,相同的过程可以创建不同的产品。将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。
--------------简单来说就是,相同的过程可以创建不同的产品。
适用于:

  • 一个对象有非常复杂的内部结构(很多属性)
  • 想将复杂对象的创建和使用分离。

优点: 1、封装性好,创建和使用分离 2、拓展性好,建造类之间独立,一定程度上解耦。

缺点: 1、产生多余的Builder对象; 2、产品内部发生变化,建造者需要更改,成本较大

例子:
1、新增商铺类Shop,包含名称,地点和类型属性:

/*
 * 商铺类
 * */
public class Shop {

	// 名称
	private String name;

	// 地点
	private String location;

	// 类型
	private String type;

	@Override
	public String toString() {
		return "Shop{" + "name='" + name + '\'' + ", location='" + location + '\'' + ", type='" + type + '\'' + '}';
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}

2、接着创建Shop抽象生成器ShopBuilder:包含和Shop相同的属性及对应的抽象构建方法。

/*
 * 抽象生成器
 * 包含和Shop相同的属性及对应的抽象构建方法。
 * */
public abstract class ShopBuilder {


	public abstract void name(String name);

	public abstract void location(String location);

	public abstract void type(String type);

	public abstract Shop build();

}

3、继续创建ShopBuilder的实现,水果店构造器FruitShopBuilder:

/*
 * 水果店构造器
 * */
public class FruitShopBuilder extends ShopBuilder {

	private Shop shop = new Shop();

	@Override
	public void name(String name) {
		this.shop.setName(name);
	}

	@Override
	public void location(String location) {
		this.shop.setLocation(location);
	}

	@Override
	public void type(String type) {
		this.shop.setType(type);
	}

	@Override
	public Shop build() {
		return shop;
	}

}

4、创建个经销商类Dealer,用于通过ShopBuilder构建具体的商店:

public class Dealer {

	private ShopBuilder builder;

	public void setBuilder(ShopBuilder builder) {
		this.builder = builder;
	}

	public Shop build(String name, String location, String type) {
		this.builder.name(name);
		this.builder.location(location);
		this.builder.type(type);
		return builder.build();
	}

}

5、创建个客户端Application测试一波:

import com.fiber.wipe.builderpattern.dao.Dealer;
import com.fiber.wipe.builderpattern.dao.FishShopBuilder;
import com.fiber.wipe.builderpattern.dao.FruitShopBuilder;
import com.fiber.wipe.builderpattern.dao.Shop;
import com.fiber.wipe.builderpattern.dao.ShopBuilder;

public class TestApplication {

	public static void main(String[] args) {
		ShopBuilder builder = new FruitShopBuilder();
		Dealer dealer = new Dealer();
		dealer.setBuilder(builder);

		Shop shop = dealer.build("建寧水果店", "平凡路", "水果店");
		System.out.println(shop);
		ShopBuilder builder2 = new FishShopBuilder();
		Dealer dealer2 = new Dealer();
		dealer2.setBuilder(builder2);
		Shop shop2 = dealer2.build("长兴市场", "西安路", "卖鱼的");
		System.out.println(shop2);
	}
}

6、结果:
在这里插入图片描述
7、UML展示
在这里插入图片描述

8、其实建造者模式更为常用的例子是下面这个:
创建一个店铺类Shop,Shop里包含构造该Shop的内部类:

public class Shop {

    private String name;
    private String location;
    private String type;

    public Shop(ShopBuilder builder) {
        this.name = builder.name;
        this.location = builder.location;
        this.type = builder.type;
    }

    @Override
    public String toString() {
        return "Shop{" +
                "name='" + name + '\'' +
                ", location='" + location + '\'' +
                ", type='" + type + '\'' +
                '}';
    }

    public static class ShopBuilder {
        private String name;
        private String location;
        private String type;

        public ShopBuilder name(String name) {
            this.name = name;
            return this;
        }

        public ShopBuilder location(String location) {
            this.location = location;
            return this;
        }

        public ShopBuilder type(String type) {
            this.type = type;
            return this;
        }

        public Shop build() {
            return new Shop(this);
        }
    }
}

在客户端构建Shop只需:

public class Application {

    public static void main(String[] args) {
        Shop shop = new Shop.ShopBuilder()
                .name("XX水果店")
                .location("福州市XX区XX街XX号")
                .type("水果经营")
                .build();
        System.out.println(shop);
    }
}

这种用法和Lombok的@Builder注解效果是一样的。
9、UML展示
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值