Q: What are design patterns
Slowly: Design pattern is a solution for common scenarios in system service design, which can solve common problems encountered in the development of functional logic. Design pattern is not limited to the final implementation scheme, but in this conceptual pattern, to solve the code logic problems in system design.
Q: What is the Builder mode
Slowly: The core purpose of the Builder pattern is to build a complex object step by step from multiple simple objects. For example, when we decorate a house, we not only need to consider the hard part, such as floor, tile, door frame and so on, but also need to consider the soft part, such as tables and chairs, sofas, household appliances and so on. If we represent the entire decoration process with only one object, it makes the entire code complex and difficult to decouple. The Builder pattern is also a creator pattern.
Small Q: Understood, hurry up the code!
Slowly: We take the decoration as an example, first create a material interface.
public interface Matter {
String category(a); / / class
String brand(a); / / brand
double price(a); / / price
}
Copy the code
Ceramic tile:
/ / 1. Lens
public class DongPeng implements Matter {
public category(a) {
return "Ceramic tile";
}
public brand(a) {
return "East peng";
}
public price(a) {
return 100L; }} -- -- -- -- --// 2. Marco Polo
public class MarcoPolo implements Matter {
public String category(a) {
return "Ceramic tile";
}
public String brand(a) {
return "Marco Polo";
}
public double price(a) {
return 150L; }}Copy the code
The floor:
/ / 1. Del
public class Der implements Matter {
public String category(a) {
return "Floor";
}
public String brand(a) {
return "Del";
}
public double price(a) {
return 50L; }} -/ / 2. The icon
public class ShengXiang implements Matter {
public String category(a) {
return "Floor";
}
public String brand(a) {
return "Icon";
}
public double price(a) {
return 30L; }}Copy the code
TV:
/ / 1. Millet
public class XiaoMi implements Matter {
public String category(a) {
return "Television";
}
public String brand(a) {
return "Millet";
}
public double price(a) {
return 3000L; }} -/ / 2. Letv
public class LeShi implements Matter {
public String category(a) {
return "Television";
}
public String brand(a) {
return "Letv";
}
public double price(a) {
return 2500L; }}Copy the code
Decorate a listing
public class DecorationList {
ArrayList<Matter> list = new ArrayList<>(); // List list
public DecorationList add(Matter matter) {
list.add(matter);
return this; }}Copy the code
Use test:
public class Demo {
public static void main(String[] args) {
DecorationList decorationList = new DecorationList();
decorationList.add(new Der())
.add(new MarcoPolo())
.add(new LeShi()); // chain programming}}Copy the code
Q: The Builder mode is so powerful. Where can it be applied
Slowly: When we are faced with the creation of a complex object, we can use this pattern to separate the parts as child objects and then group them together to make the code more robust and usable.