First, the builder model
(Builder)
The factory class pattern provides the pattern of creating a single class, while the builder pattern is to centralize the various products to manage, to create composite objects, so-called composite objects that refer to a class that has different properties, in fact the builder pattern is the combination of the previous abstract factory pattern and the last Test. Let's look at the code:
As before, a sender interface, two implementation classes MailSender and Smssender. Finally, the Builder class is as follows:
- 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 ());
- }
- }
- }
Test class:
- public class Test {
- public static void Main (string[] args) {
- Builder builder = new Builder ();
- Builder.producemailsender (10);
- }
- }
From this point of view, the builder pattern integrates many functions into a class that can create more complex things. So the difference with the engineering model is that the factory model is concerned with creating a single product, while the builder pattern is concerned with creating multiple objects and parts. Therefore, the choice of the factory model or the builder model depends on the actual situation.
The above content is quoted from https://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html
------Builder Mode for Java 23 design Patterns