一看就懂的设计模式〖三〗建造者模式
一. 建造者方法
创建者模式又叫建造者模式,是将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。创建者模式隐藏了复杂对象的创建过程,它把复杂对象的创建过程加以抽象,通过子类继承或者重载的方式,动态的创建具有复合属性的对象。
优点
- 1.使操作获取对象的人不再关心内部对象构建的逻辑,把内部的建造过程和细节隐藏起来
- 2.将代码封装复用,提高可用性及阅读性
二. 具体实现
2.1 上代码
例如我们去创建手机这个对象,那手机其实由很多组件组装起来,例如芯片,屏幕,操作系统等等,那我们需要创建不同手机的时候,需要有一个公共Phone基类对象去存储信息,抽象Builder来提取公共方法,具体实现类IphoneBuilder,XiaoMiBuilder来具体赋值,最后通过Director类来进行操作组装顺序.
Phone
package DesignPattern.BuliderPattern;
import lombok.Data;
@Data
public class Phone {
private String chip;
private String screen;
private String os;
}
Builder
package DesignPattern.BuliderPattern;
public abstract class Builder {
abstract void buildChip();
abstract void buildScreen();
abstract void buildOS();
abstract Phone createPhone();
}
**IphoneBuilder **
package DesignPattern.BuliderPattern;
public class IphoneBuilder extends Builder{
Phone phone=new Phone();
@Override
void buildChip() {
phone.setChip("A15");
}
@Override
void buildScreen() {
phone.setScreen("LTPO");
}
@Override
void buildOS() {
phone.setOs("IOS");
}
@Override
Phone createPhone() {
return phone;
}
}
XiaoMiBuilder
package DesignPattern.BuliderPattern;
public class XiaoMiBuilder extends Builder{
Phone phone=new Phone();
@Override
void buildChip() {
phone.setChip("骁龙888");
}
@Override
void buildScreen() {
phone.setScreen("康宁大猩猩");
}
@Override
void buildOS() {
phone.setOs("安卓");
}
@Override
Phone createPhone() {
return phone;
}
}
Director
package DesignPattern.BuliderPattern;
import org.springframework.stereotype.Component;
public class Director {
private Builder phoneBuilder;
public Director(Builder builder) {
this.phoneBuilder = builder;
}
public Phone contruct(){
phoneBuilder.buildOS();
phoneBuilder.buildScreen();
phoneBuilder.buildChip();
return phoneBuilder.createPhone();
}
public static void main(String[] args) {
showPhone(new IphoneBuilder());
showPhone(new XiaoMiBuilder());
}
private static void showPhone(Builder builder){
Director director=new Director(builder);
Phone contruct = director.contruct();
System.out.println(contruct);
}
}
最终结果
也可以直接不使用Director类,直接复用Builder来进行操作组装顺序
Builder
package DesignPattern.BuliderPattern;
public abstract class Builder {
abstract void buildChip();
abstract void buildScreen();
abstract void buildOS();
abstract Phone createPhone();
public Phone construct(){
this.buildChip();
this.buildScreen();
this.buildOS();
return createPhone();
}
}
2.2 建造者模式另外一种使用
package DesignPattern.BuliderPattern;
import lombok.val;
public class PeopleInfo {
private String name;
private String gender;
private String area;
private String nation;
public PeopleInfo(Builder builder) {
this.name = builder.name;
this.gender = builder.gender;
this.area = builder.area;
this.nation = builder.nation;
}
public static final class Builder {
private String name;
private String gender;
private String area;
private String nation;
public Builder() {
}
public PeopleInfo build(){
return new PeopleInfo(this);
}
public Builder name(String val){
name=val;
return this;
}
public Builder gender(String val){
gender=val;
return this;
}
public Builder area(String val){
area=val;
return this;
}
public Builder nation(String val){
nation=val;
return this;
}
}
public static void main(String[] args) {
PeopleInfo peopleInfo=new PeopleInfo.Builder().name("aa").build();
}
}
在类的内部使用静态类Builder,当我们不方便使用全参构造时,直接可以一步一步build出来,Lombok的@Builder注解的功能原理就是这样,极大方便了我们使用,用于创建对象的无障碍花哨 API!