OOAD Builder Pattern
OOAD Builder Pattern
1

Pattern Classification
• The Gang of Four Core Four classified patterns in three ways
• The behavioral patterns are used to manage variation in behaviors, interaction,
and distribution of responsibility
• The structural patterns are useful to compose classes or objects in larger
structures
• The creational patterns are used to create objects and decouple clients from
instantiations
2
Pattern Classification
• Are the patterns related to Classes or Objects?
3
Pattern Libraries & Documenting Patterns
5
Factories & Their Role in OO Design
• If you mix this type of code with the use of the instantiated objects, your
code becomes cluttered with responsibilities
• often the use scenarios can happen in a few lines of code
• if combined with creational code, the operational code gets buried behind the
creational code
• Similar issues arise when mixing user interface creation and
management with underlying business logic or data handling
• One of the reasons for MVC, for instance
6
Factories provide Cohesion
7
The Object Creation/Management Rule
An object should either:
• create/manage a set of objects OR
• use other objects
• But as a guideline, the rule is useful
• Look for ways to separate out the creation of objects from the code that makes use of
those objects
• encapsulate the creation process and you can change it as needed without impacting the code that
then uses those objects
• Demonstration of the advantages of the rule are in the following two diagrams
8
Client Perspective
9
Factory Perspective
10
Factories help to limit change
11
Polymorphic Factory Thoughts
13
Builder Pattern Overview
• Intent:
• Separate the construction of a complex object from its representation so that
the same construction process can create different representations
• Eliminate the telescoping constructor problem of n * m different constructor
methods
• Use when:
• the algorithm for creating a complex object should be independent of the parts
that make up the object and how they’re assembled
• the construction process must allow different representations for the object
that’s constructed
• the construction of a product is complex and we want toallow it to be
constructed in steps
14
Telescoping Constructors
public Maze() {}
ConnectionStrategy strategy) {}
etc…
Android Dialog Example Usage
Inner class
builder
.setMessage("Do you want to cancel the download?”)
.setTitle("Cancel Download?")
.setNegativeButton("No!", ...)
.setPositiveButton("YES!", …);
return builder.create();
16
In the Android example this
could be the Main app Builder UML Diagram
class method
AlertDialog.Builder
AlertDialog Box
17
What about Polymorphia?
What about this?
Maze.Builder
18
Builder in Head First: Vacation Schedule
Vacation Schedule UML
public ContainingClass() {
System.out.println("ContainingClass constructor");
id = 246;
}
class InnerClass {
String name = “InnerClass";
public ContainingClass() {
System.out.println("ContainingClass constructor");
id = 246;
}
InnerClass createInnerClass() {
return new InnerClass();
}
ContainingClass.InnerClass innerClass = new ContainingClass.StaticInnerClass();
}
}
No containing class
pointer here in the static
inner class
Inner Class Builder Example
public class BankAccount { Can access the private
public static class Builder { Builder is an inner class instance variables since
// This is important, so we'll pass it to the constructor Builder is an inner class
private long accountNumber;
private String owner;
private String branch;
private double balance; Returning the builder each
private double interestRate; Return fully initialized,
time creates a “fluent
consistent object
public Builder(long accountNumber) { interface”
this.accountNumber = accountNumber;
}
public Builder withOwner(String owner) {
this.owner = owner;
return this; public BankAccount build(){
} BankAccount account = new BankAccount();
account.accountNumber = this.accountNumber;
public Builder atBranch(String branch) { account.owner = this.owner;
this.branch = branch; account.branch = this.branch;
return this; account.balance = this.balance;
} account.interestRate = this.interestRate;
return account;
public Builder openingBalance(double balance) { }
this.balance = balance; }
return this;
} //Fields omitted for brevity.
BankAccount
constructor is private BankAccount() {
Duplicate instance variables private // probably doesn’t do much
of BankAccount? }
//Getters and setters omitted for brevity.
}
https://dzone.com/articles/design-patterns-the-builder-pattern
Inner Class Builder Example 2
public class BankAccount { just create the final object
initially
//Bank account fields omitted for brevity.
public static class Builder {
private BankAccount account = new BankAccount();
.append(errorCode);
System.out.println(messageBuilder);
HttpRequest example
void testHttpRequestGeneration2() {
HttpRequest request = new HttpRequest(uri,
listOfHeaders?,
timeOut, Lots of
action, arguments to the
body, constructor?
...
)
}
void testHttpRequestGeneration2() {
HttpRequest request = new HttpRequest(Map.of(
“header”: header,
“timeOut”: timeOut, One Map
“action”: action, argument?
“body”: body,
...
))
}
HttpRequest example
@Test
void testHttpRequestGeneration2() {
HttpRequest request = new HttpRequest();
request.setUri(uri);
request.addHeader("Content-Type", "text/plain;charset=UTF-8");
request.setAction(HttpActions.POST);
request.setBody(body);
request.setTimeout(myTimeOut);
...
}