头图
Bao Jianfeng has been honed and plum blossoms have come from bitter cold. The articles are continuously updated. You can search for [Xiaoqi JAVA Interview] on WeChat to read it for the first time, and reply to [Information] and there are more benefits I prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.

 title=

@[TOC]


foreword

I don't know if you have read today's hot news, "Focusing on the domineering moment of Shandong ship", seeing such a magnificent aircraft carrier, I can't help but feel that the motherland is getting stronger and stronger, and we have the first domestic aircraft carrier in the true sense.

 title=

With this happy event, we just happened to learn the builder mode. After all, you can only contribute to the country if you study hard. As long as you learn my builder mode, you can "build" an aircraft carrier for the country.

 title=

1. Interview

Interviewer: Can you talk about the builder pattern?

Me: The builder pattern is a relatively complex creational pattern. It separates the client from the creation process of complex objects. The client does not need to know the internal components and assembly methods of complex objects, but only needs to know the type of builder required. That's it.

For example, an aircraft carrier has multiple components and various parts. For most of our users, we do not know the installation details of these parts. We only need to know and use the built aircraft carrier.

Interviewer: Can you tell me what roles the builder mode has?

Me: There are 4 characters in builder mode.

1. Builder (abstract builder): It specifies an abstract interface for each component that creates a product object. In this interface, two types of methods are generally declared. One method is the build() method, which is used to create various components of a complex object. ; another class of methods is getResult(), which returns complex objects. Builder can be either an abstract class or an interface.

2. ConcreteBuilder (concrete builder): It implements the Builder interface, implements the specific construction and assembly methods of each component, defines and clarifies the complex objects created, and can also provide a method to return the created complex product object (this method also Can be implemented by abstract builder).

3. Product: It is a complex object that is built, containing multiple components, and the specific builder creates an internal representation of the product and defines its assembly process.

4. Director: The director is also known as the director class. It is responsible for arranging the construction order of complex objects. There is an association between the director and the abstract builder, and the builder object can be called in its construct() construction method. Component construction and assembly method. Complete the construction of complex objects. The client generally only needs to interact with the conductor, determine the type of the specific builder on the client, and instantiate the specific builder object (which can also be implemented through configuration files and reflection mechanisms), and then pass the constructor or Setter of the conductor class. method passes the object into the conductor class.

Interviewer: Can you write a code example of the builder pattern?

1. First you need an aircraft carrier entity

 public class HangMu {
    private String JiaBan;     //夹板
    private String PaoDao;     //跑道
    private String FeiJI;      //飞机

    public String getJiaBan() {
        return JiaBan;
    }

    public void setJiaBan(String jiaBan) {
        JiaBan = jiaBan;
    }

    public String getPaoDao() {
        return PaoDao;
    }

    public void setPaoDao(String paoDao) {
        PaoDao = paoDao;
    }

    public String getFeiJI() {
        return FeiJI;
    }

    public void setFeiJI(String feiJI) {
        FeiJI = feiJI;
    }
}

2. Then you need an abstract builder class

 public abstract class Builder {
    //创建航母
    protected HangMu hangMu = new HangMu();

    public abstract void buildJiaBan(); //创建夹板的方法
    public abstract void buildPaoDao(); //创建跑道的方法
    public abstract void buildFeiJI();  //创建飞机的方法

    //返回航母实体
    public HangMu getResult(){
        return hangMu;
    }

}

3. Then you need a specific builder class

 public class SDBuilder extends Builder {
    @Override
    public void buildJiaBan() {
        hangMu.setJiaBan("山东的航空母舰夹板很厚");
    }

    @Override
    public void buildPaoDao() {
        hangMu.setPaoDao("山东的航空母舰跑道很长");
    }

    @Override
    public void buildFeiJI() {
        hangMu.setFeiJI("山东的航空母舰飞机很隐蔽");
    }
}

4. Then you need the commander class

 public class Director {
    private Builder builder;

    public Director(Builder builder){
        this.builder = builder;
    }

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

    //航空母舰的构建与组装方法
    public HangMu construct(){
        builder.buildJiaBan();
        builder.buildPaoDao();
        builder.buildFeiJI();
        return builder.getResult();
    }
}

5. Final test class test

 public class Test {
    public static void main(String[] args) {
        Builder builder = new SDBuilder();
        Director director = new Director(builder);
        HangMu hangMu = director.construct();
        System.out.println(hangMu.getJiaBan());
        System.out.println(hangMu.getPaoDao());
        System.out.println(hangMu.getFeiJI());
    }
}

在这里插入图片描述

Interviewer: Yes, yes, when can you come to the company to build an aircraft carrier?

Me: Let’s go in two days. I’ve been a little seasick in the past few days, so I can’t make it.

2. Summary

The relevant content here has not been sorted out, and the article will continue to be updated later, and it is recommended to collect it.

The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.

If you think my article is not bad, please like it. In addition, you can search for [Xiaoqi JAVA Interview] on WeChat to read it for the first time, and reply to [Information] and there are more benefits I have prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.

This article participated in the Sifu technical essay , and you are welcome to join.

小奇Java面试
12 声望7 粉丝

引用和评论

0 条评论