Creative mode: Builder mode
Builder pattern
1. Builder mode: Introduction
- Builder pattern
- The core purpose is to build a complex object step by step by using multiple simple objects.
- The final intention of the builder's model is to produce specific contents according to the same materials and different assembly methods
- That is, a complex build is separated from its representation, and different representations can be created with the same build process
2. Builder mode: simulation scene
- Optional scenes of decoration package
- Many decoration companies will provide some package services, generally including luxury European style, light luxury pastoral and modern simple decoration service packages.
- Behind these packages are combinations of different decoration materials and design styles
- Primary roof and secondary roof
- Dulux paint, Nippon Paint
- Icon floor, del floor
- Marco Polo floor tile, Dongpeng floor tile
- According to different package prices, select different brands for combination, and finally give the overall quotation in combination with the decoration area
3. Builder mode: Scene Simulation Project
0. Engineering structure
lino-design-6.0 |-src |-main |--java |--com.lino.design |--ceiling |--LevelOneCeiling.java |--LevelTwoCeiling.java |--coat |--DuluxCoat.java |--LiBangCoat.java |--floor |--DerFloor.java |--ShengXiangFloor.java |--tile |--DongPengTile.java |--MarcoPoloTile.java |--Matter.java
- In the simulated decoration material project, the following categories are provided:
- ceiling material package: LevelOneCeiling, LevelTwoCeiling
- coat package: DuluxCoat, LiBangCoat
- floor material package: DerFloor, ShengXiangFloor
- tile package: DongPengTile, MarcoPoloTile
1. Decoration material interface
- The decoration material interface provides a basic method to obtain information to ensure that all decoration materials of different specifications and types can be obtained according to unified standards.
import java.math.BigDecimal; /** * @description: Decoration materials */ public interface Matter { /** * Scene; Floor, floor tile, coating, ceiling * * @return String */ String scene(); /** * brand * * @return String */ String brand(); /** * model * * @return String */ String model(); /** * Square meter quotation * * @return BigDecimal */ BigDecimal price(); /** * describe * * @return String */ String desc(); }
2. ceiling material
- Primary roof
import com.lino.design.Matter; import java.math.BigDecimal; /** * @description: Ceiling: primary ceiling */ public class LevelOneCeiling implements Matter { @Override public String scene() { return "suspended ceiling"; } @Override public String brand() { return "Decoration company's own"; } @Override public String model() { return "Primary roof"; } @Override public BigDecimal price() { return new BigDecimal(260); } @Override public String desc() { return "The modeling is only one lower level, and there is only one level of ceiling, which is generally 120 away from the top-150mm"; } }
- Secondary roof
import com.lino.design.Matter; import java.math.BigDecimal; /** * @description: Ceiling: secondary ceiling */ public class LevelTwoCeiling implements Matter { @Override public String scene() { return "suspended ceiling"; } @Override public String brand() { return "Decoration company's own"; } @Override public String model() { return "Secondary roof"; } @Override public BigDecimal price() { return new BigDecimal(850); } @Override public String desc() { return "For the two-level ceiling, the height of the secondary ceiling is generally suspended downward by 20 cm,If the layer height is very high, the thickness of each stage can also be increased"; } }
3. Coating material (coat)
- Dulux
import com.lino.design.Matter; import java.math.BigDecimal; /** * @description: Paint: Dulux */ public class DuluxCoat implements Matter { @Override public String scene() { return "coating"; } @Override public String brand() { return "Dulux(Dulux)"; } @Override public String model() { return "Second generation"; } @Override public BigDecimal price() { return new BigDecimal(719); } @Override public String desc() { return "Dulux is a famous architectural decoration paint brand under AkzoNobel. Its products sell well in 100 countries around the world, and 50 million families use Dulux paint every year."; } }
- Nippon
import com.lino.design.Matter; import java.math.BigDecimal; /** * @description: Coating: Nippon */ public class LiBangCoat implements Matter { @Override public String scene() { return "coating"; } @Override public String brand() { return "Nippon"; } @Override public String model() { return "Default level"; } @Override public BigDecimal price() { return new BigDecimal(650); } @Override public String desc() { return "Nippon always aims to develop green products, pay attention to high technology and high quality, and continuously promote scientific research and development with technical force to meet the needs of consumers."; } }
4. floor material
- Del
import com.lino.design.Matter; import java.math.BigDecimal; /** * @description: Floor: Der */ public class DerFloor implements Matter { @Override public String scene() { return "floor"; } @Override public String brand() { return "Del(Der)"; } @Override public String model() { return "A+"; } @Override public BigDecimal price() { return new BigDecimal(119); } @Override public String desc() { return "DER Del group is a leading professional wood flooring manufacturer in the world and a supplier of home decoration and public decoration flooring for Beijing 2008 Olympic Games"; } }
- Holy image
import com.lino.design.Matter; import java.math.BigDecimal; /** * @description: Floor: holy elephant */ public class ShengXiangFloor implements Matter { @Override public String scene() { return "floor"; } @Override public String brand() { return "Powerdekor "; } @Override public String model() { return "class a"; } @Override public BigDecimal price() { return new BigDecimal(318); } @Override public String desc() { return "Shengxiang flooring is a famous brand in China's flooring industry. Shengxiang flooring has many honors, such as China's well-known trademark, China's famous brand, national inspection exemption, China's environmental mark certification and so on."; } }
5. tile material
- Dongpeng
import com.lino.design.Matter; import java.math.BigDecimal; /** * @description: Floor tile: Dongpeng ceramic tile */ public class DongPengTile implements Matter { @Override public String scene() { return "floor tile"; } @Override public String brand() { return "Dongpeng ceramic tile"; } @Override public String model() { return "10001"; } @Override public BigDecimal price() { return new BigDecimal(102); } @Override public String desc() { return "Dongpeng ceramic tile aims at creating brand by quality, promoting brand by science and technology and spreading brand by word of mouth. The brand value in 2014 was 132.35 Billion yuan, ranking first in the construction ceramics industry."; } }
- Marco Polo
import com.lino.design.Matter; import java.math.BigDecimal; /** * @description: Floor tile: Marco Polo */ public class MarcoPoloTile implements Matter { @Override public String scene() { return "floor tile"; } @Override public String brand() { return "Marco Polo(MARCO POLO)"; } @Override public String model() { return "default"; } @Override public BigDecimal price() { return new BigDecimal(140); } @Override public String desc() { return ""The "Marco Polo" brand was born in 1996. As the earliest branded ceramic brand in China, it occupies the market with "cultural ceramics" and enjoys the reputation of "supreme antique brick"."; } }
4. Builder mode: code implementation
0. Engineering structure
lino-design-6.0 |-src |-main |--java |--com.lino.builder |--Builder.java |--DecorationPackageMenu.java |--IMenu.java |-test |--java |--com.lino.test |--Test.java
- The builder pattern code project has three core classes, which are the concrete implementation of the builder pattern.
- Builder: various specific assemblies of the builder class are implemented by this class.
- DecorationPackageMenu: it is the implementation class of IMenu interface. It mainly carries the filler in the construction process. It is equivalent to a set of contents connecting the material and the creator
1. Define decoration package interface
- The decoration package interface defines the method of filling various materials of ceiling, coating, floor and floor tiles, and finally provides the method of obtaining all details
import com.lino.design.Matter; /** * @description: Decoration package interface */ public interface IMenu { /** * suspended ceiling * * @param matter Decoration materials * @return Decoration package interface */ IMenu appendCeiling(Matter matter); /** * coating * * @param matter Decoration materials * @return Decoration package interface */ IMenu appendCoat(Matter matter); /** * floor * * @param matter Decoration materials * @return Decoration package interface */ IMenu appendFloor(Matter matter); /** * floor tile * * @param matter Decoration materials * @return Decoration package interface */ IMenu appendTile(Matter matter); /** * detailed * * @return String */ String getDetail(); }
2. Implement decoration package interface
- In the implementation of decoration package, each method returns the this object itself, which can be used to fill various materials continuously.
- During filling, the quotation of the corresponding area will also be calculated according to the materials, and the ceiling and coating will be calculated according to the area multiplied by the unit price
- Finally, it also provides a unified method to obtain the details of the decoration list
import com.lino.design.Matter; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; /** * @description: Decoration package interface implementation class */ public class DecorationPackageMenu implements IMenu { /** * Decoration list */ private List<Matter> list = new ArrayList<>(); /** * Decoration price */ private BigDecimal price = BigDecimal.ZERO; /** * the measure of area */ private BigDecimal area; /** * Decoration grade; Luxury European style, light luxury pastoral, modern simplicity */ private String grade; public DecorationPackageMenu() { } public DecorationPackageMenu(Double area, String grade) { this.area = new BigDecimal(area); this.grade = grade; } @Override public IMenu appendCeiling(Matter matter) { list.add(matter); price = price.add(area.multiply(new BigDecimal("0.2")).multiply(matter.price())); return this; } @Override public IMenu appendCoat(Matter matter) { list.add(matter); price = price.add(area.multiply(new BigDecimal("1.4")).multiply(matter.price())); return this; } @Override public IMenu appendFloor(Matter matter) { list.add(matter); price = price.add(area.multiply(matter.price())); return this; } @Override public IMenu appendTile(Matter matter) { list.add(matter); price = price.add(area.multiply(matter.price())); return this; } @Override public String getDetail() { StringBuilder detail = new StringBuilder("\r\n-------------------------------------------------------\r\n" + "Decoration list" + "\r\n" + "Package level:" + grade + "\r\n" + "Package price:" + price.setScale(2, BigDecimal.ROUND_HALF_UP) + " element\r\n" + "House area:" + area.doubleValue() + " square metre\r\n" + "Bill of materials:\r\n"); for (Matter matter : list) { detail.append(matter.scene()).append(": ").append(matter.brand()).append(",").append(matter.model()).append(",Price per square meter:").append(matter.price()).append(" Yuan.\n"); } return detail.toString(); } }
3. Builder class creation
- The unified construction method fills different decoration styles through different materials: luxury European style, light luxury pastoral, modern simplicity
import com.lino.design.ceiling.LevelOneCeiling; import com.lino.design.ceiling.LevelTwoCeiling; import com.lino.design.coat.DuluxCoat; import com.lino.design.coat.LiBangCoat; import com.lino.design.floor.ShengXiangFloor; import com.lino.design.tile.DongPengTile; import com.lino.design.tile.MarcoPoloTile; /** * @description: Builder class */ public class Builder { /** * Deluxe European style: ceiling, secondary roof, coating, Dulux, floor, icon * * @param area the measure of area * @return IMenu */ public IMenu levelOne(Double area) { return new DecorationPackageMenu(area, "Deluxe European style") .appendCeiling(new LevelTwoCeiling()) .appendCoat(new DuluxCoat()) .appendFloor(new ShengXiangFloor()); } /** * Deluxe European style: ceiling, secondary roof, coating, Nippon, floor tile, Marco Polo * * @param area the measure of area * @return IMenu */ public IMenu levelTwo(Double area) { return new DecorationPackageMenu(area, "Light luxury countryside") .appendCeiling(new LevelTwoCeiling()) .appendCoat(new LiBangCoat()) .appendTile(new MarcoPoloTile()); } /** * Deluxe European style: ceiling, secondary roof, coating, Nippon, floor, Dongpeng * * @param area the measure of area * @return IMenu */ public IMenu levelThree(Double area) { return new DecorationPackageMenu(area, "Modern simplicity") .appendCeiling(new LevelOneCeiling()) .appendCoat(new LiBangCoat()) .appendTile(new DongPengTile()); } }
5. Unit test
- Use the builder mode to create the prices of three decoration packages respectively, obtain detailed information and output
- levelOne, levelTwo and levelThree correspond to three decoration styles: luxury European style, light luxury pastoral style and modern simplicity
- The input information is the house area, and finally obtain the decoration quotation.
import com.lino.builder.Builder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @description: Test class */ public class Test { @org.junit.Test public void testBuilder() throws Exception { // Builder class Builder builder = new Builder(); // Deluxe European style System.out.println(builder.levelOne(132.52D).getDetail()); // Light luxury countryside System.out.println(builder.levelTwo(98.25D).getDetail()); // Modern simplicity System.out.println(builder.levelThree(85.43D).getDetail()); } }
- test result
------------------------------------------------------- Decoration list Package level: Deluxe European style Package price: 198064.39 element House area: 132.52 square metre Bill of materials: Ceiling: decoration company's own, secondary roof, square meter price: 850 yuan. Coating: Dulux(Dulux),Second generation, square meter price: 719 yuan. Floor: holy elephant, class I, square meter price: 318 yuan. ------------------------------------------------------- Decoration list Package level: light luxury pastoral Package price: 119865.00 element House area: 98.25 square metre Bill of materials: Ceiling: decoration company's own, secondary roof, square meter price: 850 yuan. Coating: Nippon, default level, square meter price: 650 yuan. Floor tile: Marco Polo(MARCO POLO),Default price per square meter: 140 yuan. ------------------------------------------------------- Decoration list Package level: Modern and simple Package price: 90897.52 element House area: 85.43 square metre Bill of materials: Ceiling: decoration company's own, first-class roof, square meter price: 260 yuan. Coating: Nippon, default level, square meter price: 650 yuan. Floor tile: Dongpeng ceramic tile, 10001 square meters, price: 102 yuan.
4. Builder mode: summary
-
Conditions for selecting builder mode
- When some basic materials remain unchanged and their combinations often change.
-
Meet the principle of single responsibility and reusable technology, and the builder is independent, easy to expand and easy to control detail risk.
-
Disadvantages: when there are too many materials and combinations, the continuous expansion of classes will also cause problems that are difficult to maintain.
- Solution: duplicate contents can be abstracted into the database and configured as needed to reduce a large number of duplicate codes.