目录
建造者模式所完成的内容就是通过将多个简单对象通过⼀步步的组装构建出⼀个复杂对象的过程。
很多装修公司都会给出⾃家的套餐服务,⼀般有;欧式豪华、轻奢⽥园、现代简约等等,⽽这些套餐的
后⾯是不同的商品的组合。例如;⼀级&⼆级吊顶、多乐⼠涂料、圣象地板、⻢可波罗地砖等等,按照
不同的套餐的价格选取不同的品牌组合,最终再按照装修⾯积给出⼀个整体的报价。
- public interface Matter {
- String scene(); // 场景;地板、地砖、涂料、吊顶
- String brand(); // 品牌
- String model(); // 型号
- BigDecimal price(); // 价格
- String desc(); // 描述
- }
- public class LevelOneCeiling implements Matter {
- public String scene() {
- return "吊顶";
- }
-
- public String brand() {
- return "装修公司⾃带";
- }
-
- public String model() {
- return "⼀级顶";
- }
-
- public BigDecimal price() {
- return new BigDecimal(260);
- }
-
- public String desc() {
- return "造型只做低⼀级,只有⼀个层次的吊顶,⼀般离顶120-150mm";
- }
- }
- public class LevelTwoCeiling implements Matter {
- public String scene() {
- return "吊顶";
- }
-
- public String brand() {
- return "装修公司⾃带";
- }
-
- public String model() {
- return "⼆级顶";
- }
-
- public BigDecimal price() {
- return new BigDecimal(850);
- }
-
- public String desc() {
- return "两个层次的吊顶,⼆级吊顶⾼度⼀般就往下吊20cm,要是层⾼很⾼,也可增加每级的厚度";
- }
- }
- public class DuluxCoat implements Matter {
- public String scene() {
- return "涂料";
- }
-
- public String brand() {
- return "多乐⼠(Dulux)";
- }
-
- public String model() {
- return "第⼆代";
- }
-
- public BigDecimal price() {
- return new BigDecimal(719);
- }
-
- public String desc() {
- return "多乐⼠是阿克苏诺⻉尔旗下的著名建筑装饰油漆品牌,产品畅销于全球100个国家,每年全球有5000万户家庭使⽤多乐⼠油漆。";
- }
- }
- public class LiBangCoat implements Matter {
- public String scene() {
- return "涂料";
- }
-
- public String brand() {
- return "⽴邦";
- }
-
- public String model() {
- return "默认级别";
- }
-
- public BigDecimal price() {
- return new BigDecimal(650);
- }
-
- public String desc() {
- return "⽴邦始终以开发绿⾊产品、注⾼科技、⾼品质为⽬标,以技术⼒量不断推进科研和开发,满⾜消费者需求。";
- }
- }
- public class DerFloor implements Matter {
- public String scene() {
- return "地板";
- }
-
- public String brand() {
- return "德尔(Der)";
- }
-
- public String model() {
- return "A+";
- }
-
- public BigDecimal price() {
- return new BigDecimal(119);
- }
-
- public String desc() {
- return "DER德尔集团是全球领先的专业⽊地板制造商,北京2008年奥运会家装和公装地板供应商";
- }
- }
- public class ShengXiangFloor implements Matter {
-
- public String scene() {
- return "地板";
- }
-
- public String brand() {
- return "圣象";
- }
-
- public String model() {
- return "⼀级";
- }
-
- public BigDecimal price() {
- return new BigDecimal(318);
- }
-
- public String desc() {
- return "圣象地板是中国地板⾏业著名品牌。圣象地板拥有中国驰名商标、中国名牌、国家免检、中国环境标志认证等多项荣誉。";
- }
- }
- public class DongPengTile implements Matter {
- public String scene() {
- return "地砖";
- }
-
- public String brand() {
- return "东鹏瓷砖";
- }
-
- public String model() {
- return "10001";
- }
-
- public BigDecimal price() {
- return new BigDecimal(102);
- }
-
- public String desc() {
- return "东鹏瓷砖以品质铸就品牌,科技推动品牌,⼝碑传播品牌为宗旨,2014年品牌价值132.35亿元,位列建陶⾏业榜⾸。";
- }
-
- }
- public class MarcoPoloTile implements Matter {
- public String scene() {
- return "地砖";
- }
-
- public String brand() {
- return "⻢可波罗(MARCO POLO)";
- }
-
- public String model() {
- return "缺省";
- }
-
- public BigDecimal price() {
- return new BigDecimal(140);
- }
-
- public String desc() {
- return "“⻢可波罗”品牌诞⽣于1996年,作为国内最早品牌化的建陶品牌,以“⽂化陶瓷”占领市场,享有“仿古砖⾄尊”的美誉。";
- }
- }
- public interface IMenu {
- IMenu appendCeiling(Matter matter); // 吊顶
- IMenu appendCoat(Matter matter); // 涂料
- IMenu appendFloor(Matter matter); // 地板
- IMenu appendTile(Matter matter); // 地砖
- String getDetail(); // 明细
- }
- public class DecorationPackageMenu implements IMenu {
- private List<Matter> list = new ArrayList<Matter>(); // 装修清单
- private BigDecimal price = BigDecimal.ZERO; // 装修价格
- private BigDecimal area; // ⾯积
- private String grade; // 装修等级;豪华欧式、轻奢⽥园、现代简约
-
- private DecorationPackageMenu() {}
-
- public DecorationPackageMenu(Double area, String grade) {
- this.area = new BigDecimal(area);
- this.grade = grade;
- }
-
- public IMenu appendCeiling(Matter matter) {
- list.add(matter);
- price = price.add(area.multiply(new BigDecimal("0.2")).multiply(matter.price()));
- return this;
- }
-
- public IMenu appendCoat(Matter matter) {
- list.add(matter);
- price = price.add(area.multiply(new BigDecimal("1.4")).multiply(matter.price()));
- return this;
- }
-
- public IMenu appendFloor(Matter matter) {
- list.add(matter);
- price = price.add(area.multiply(matter.price()));
- return this;
- }
-
- public IMenu appendTile(Matter matter) {
- list.add(matter);
- price = price.add(area.multiply(matter.price()));
- return this;
- }
-
- public String getDetail() {
- StringBuilder detail = new StringBuilder("\r\n-------------------------------------------------------\r\n" +
- "装修清单" + "\r\n" +
- "套餐等级:" + grade + "\r\n" +
- "套餐价格:" + price.setScale(2, BigDecimal.ROUND_HALF_UP) + " 元\r\n" +
- "房屋⾯积:" + area.doubleValue() + " 平⽶\r\n" +
- "材料清单:\r\n");
- for (Matter matter: list) {
- detail.append(matter.scene()).append(":").append(matter.brand()).append("、").append(matter.model()).append("、平⽶价格:").append(matter.price()).append(" 元。\n");
- }
- return detail.toString();
- }
-
- }
- public class Builder {
- public IMenu levelOne(Double area) {
- return new DecorationPackageMenu(area, "豪华欧式")
- .appendCeiling(new LevelTwoCeiling()) // 吊顶,⼆级顶
- .appendCoat(new DuluxCoat()) // 涂料,多乐⼠
- .appendFloor(new ShengXiangFloor()); // 地板,圣象
- }
-
- public IMenu levelTwo(Double area){
- return new DecorationPackageMenu(area, "轻奢⽥园")
- .appendCeiling(new LevelTwoCeiling()) // 吊顶,⼆级顶
- .appendCoat(new LiBangCoat()) // 涂料,⽴邦
- .appendTile(new MarcoPoloTile()); // 地砖,⻢可波罗
- }
-
- public IMenu levelThree(Double area){
- return new DecorationPackageMenu(area, "现代简约")
- .appendCeiling(new LevelOneCeiling()) // 吊顶,⼆级顶
- .appendCoat(new LiBangCoat()) // 涂料,⽴邦
- .appendTile(new DongPengTile()); // 地砖,东鹏
- }
- }
- @Test
- public void test_Builder(){
- Builder builder = new Builder();
- // 豪华欧式
- System.out.println(builder.levelOne(132.52D).getDetail());
- // 轻奢⽥园
- System.out.println(builder.levelTwo(98.25D).getDetail());
- // 现代简约
- System.out.println(builder.levelThree(85.43D).getDetail());
- }