[Design mode] ------ Builder mode (compared with template mode)

Builder Mode

The builder mode is to build a class of execution sequences that have the same execution order but different implementations, and ultimately produce different effects.

For example, writing essays.

Every time we write a essay, we roughly write the title, write the beginning, write the content, and write the end.

public abstract class Builder {

    abstract String 写标题();

    abstract String 写开头();

    abstract String 写内容();

    abstract String 写结尾();
}

For example, if you want to write an article on a spring tour, then implement the corresponding methods of writing the title, writing the beginning, writing the content, and writing the end.

public class 春游作文 extends Builder {

    @Override
    String 写标题() {
        return "春游";
    }

    @Override
    String 写开头() {
        return "今天,我们几个人去春游。";
    }

    @Override
    String 写内容() {
        return "我们去了哪里,看到什么风景,怎么怎么样。";
    }

    @Override
    String 写结尾() {
        return "啊!春游真好!真有意义啊!";
    }
}

Write another movie

public class 看电影作文 extends Builder {
    @Override
    String 写标题() {
        return "看电影";
    }

    @Override
    String 写开头() {
        return "今天,我们几个人一起去看电影。";
    }

    @Override
    String 写内容() {
        return "去哪个电影院,看什么电影,有什么感触。。。";
    }

    @Override
    String 写结尾() {
        return "啊!电影真好看,下次还想看!";
    }
}

Finally, come to a commander (can also be understood as a student), he has a way to write essays, as long as the corresponding composition subclass is passed in, he can write the corresponding composition (this is really like the feeling of exam-oriented education , All the frames are ready, you just need to make the contents of each frame complete).

public class Director {
    public String 写作文(Builder builder){
        String 标题 = builder.写标题();
        String 开头 = builder.写开头();
        String 内容 = builder.写内容();
        String 结尾 = builder.写结尾();
        return 标题 + "\n" + 开头 + "\n" + 内容 + "\n" + 结尾;
    }
}

Test one

public class TestMain {

    public static void main(String[] args) {

        Director director = new Director();
        String 春游作文 = director.写作文(new 春游作文());
        System.out.println(春游作文);

        System.out.println("===================================");
        System.out.println("============下一篇作文===============");
        System.out.println("====================================");

        String 看电影作文 = director.写作文(new 看电影作文());
        System.out.println(看电影作文);
    }
}

Final print result:

春游
今天,我们几个人去春游。
我们去了哪里,看到什么风景,怎么怎么样。
啊!春游真好!真有意义啊!
===================================
============下一篇作文===============
====================================
看电影
今天,我们几个人一起去看电影。
去哪个电影院,看什么电影,有什么感触。。。
啊!电影真好看,下次还想看!

Comparison of builder mode and template mode

In fact, I think these two design patterns are the same meaning, there is no need to say separately

Although various opinions on the Internet, most of them insist on discussing the two separately.
But at the end of the discussion, isn't it just inheriting that thing?
For example, in the above example, if you put the method of the last Director class in the Builder, isn't it the template mode?

Therefore, I generally think of these two as a design pattern, which can also be called the template builder pattern.

Professionally speaking, the general is divided into the following steps

1. The parent class defines a series of methods
2. The sequential method specified by the parent class or the sequential method of creating a new class defines the calling sequence between each method.
3. The child class inherits the parent class and rewrites the corresponding method.
4. Calling sequential methods for different subclasses will produce different effects, but the overall execution order is within control.

Well, if you want to distinguish:

The builder mode is the second step using the sequence method of creating a new class. The method input parameter is the subclass
template mode, which is the second step above. The parent class specifies the sequence method, and the subclass specifies the call

to sum up

I hope that we can adapt to the development and have no chances to win.

Really awesome development, when he was coding, he clearly used a certain design pattern, but he did n’t realize which mode, he only knew that it was the most appropriate way to do it. The integration is through.

And the kind of people who just want to use design patterns everywhere, use them everywhere, and waste their energy in distinguishing various design patterns are equivalent to only learning some moves, and are far from the realm of inner strength.

Of course, the above two kinds of people are thousands of times stronger than those who do n’t want to practice and do not have the internal skills.

Published 203 original articles · praised 186 · 210,000 views

Guess you like

Origin blog.csdn.net/java_zhangshuai/article/details/105108055