Fundamentals 6 min read

Builder Pattern in Java: Implementation, Usage, and Comparison with Simple Factory

This article explains the Builder design pattern in Java with complete code examples—including Product, abstract Builder, ConcreteBuilder, Director, and client code—demonstrates the output, compares it to the simple factory pattern, and briefly shows how ASM bytecode manipulation can generate similar constructs.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Builder Pattern in Java: Implementation, Usage, and Comparison with Simple Factory

The article introduces the Builder pattern by first defining a Product class that stores parts in an ArrayList<String> and provides add and show methods.

Next, an abstract Builder class is presented with three abstract methods: buildPartA() , buildPartB() , and Product getResult() .

A concrete implementation, ConcreteBuilder , extends Builder , holds a private Product instance, and implements the abstract methods to add the upper and lower parts of the product.

The Director class is then defined to control the construction process by invoking builder.buildPartA() and builder.buildPartB() in its construct() method.

Client test code creates a ConcreteBuilder , passes it to a Director , calls director.construct() , retrieves the built Product via builder.getResult() , and prints the result, which outputs [构建产品的上半部分, 构建产品的下半部分] .

The article then discusses the differences between the Builder pattern and the Simple Factory pattern, emphasizing that Builder adds a separate Builder class to increase flexibility, especially when object creation involves many parameters, default values, or varying method call orders.

Typical scenarios for using Builder are listed: (1) when the same methods can be called in different orders producing different results, and (2) when an object has many parameters, many of which have default values.

Finally, the article briefly touches on Java bytecode manipulation using the ASM framework, showing a snippet where a MethodVisitor inserts bytecode to call System.out.println("Hello World") :

// visit static field System.out mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); // load constant "Hello World" mv.visitLdcInsn("Hello World"); // invoke println method mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);

These examples together provide a comprehensive guide to implementing the Builder pattern in Java and illustrate its underlying mechanisms at both source and bytecode levels.

design patternsJavasoftware architectureObject CreationBuilder Pattern
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.