The Road to Java Design Patterns "Four" Builder Pattern

Builder pattern (Builder)

 

The factory class mode provides a mode for creating a single class, while the builder mode is to centralize various products for management and use to create composite objects. The so-called composite object refers to a class with different attributes. In fact, the builder mode It is obtained by combining the previous abstract factory pattern and the final Test.

 

public class Builder {
    private List<Sender> list = new ArrayList<Sender>();
    public void produceMailSender(int count){
        for(int i=0; i<count; i++){
            list.add(new MailSender());
        }
    }
    public void produceSmsSender(int count){
        for(int i=0; i<count; i++){
            list.add(new SmsSender());
        }
    }
}

 

From this point of view, the builder pattern integrates many functions into a class, which can create more complex things. So the difference with the engineering pattern is: the factory pattern is concerned with creating a single product, while the builder pattern is concerned with creating conforming objects, multiple parts. Therefore, whether to choose the factory mode or the builder mode depends on the actual situation.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326403895&siteId=291194637