0% found this document useful (0 votes)
64 views

Design Patterns - 6.5 - Builder Pattern

The document describes the builder pattern design pattern. It discusses how the builder pattern can be used to simulate optional and keyword parameters that Java does not support natively. It provides sample Python and Java code to illustrate constructing objects using the builder pattern versus directly in the constructor. It notes the builder can eagerly or lazily construct the object. The document also briefly mentions the performance difference between concatenating Strings versus a StringBuilder.

Uploaded by

Josh Waxman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Design Patterns - 6.5 - Builder Pattern

The document describes the builder pattern design pattern. It discusses how the builder pattern can be used to simulate optional and keyword parameters that Java does not support natively. It provides sample Python and Java code to illustrate constructing objects using the builder pattern versus directly in the constructor. It notes the builder can eagerly or lazily construct the object. The document also briefly mentions the performance difference between concatenating Strings versus a StringBuilder.

Uploaded by

Josh Waxman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

Design Patterns
LECTURE 6 ½ - BUILDER PATTERN
DR. JOSHUA WAXMAN
2
Basic Idea

 Have a Java object with many attributes. The constructor would need (perhaps) to take all
of them. Too many to remember.
 What if don’t want to specify all of them?
 What if don’t want to specify them in order?

 Java does not have optional parameters. Java does not have keyword parameters.

 Builder pattern simulates these missing features.


3
4
In Python – basic code, specify all params

class GameCharacter(object):
def __init__(self, name, hitPoints, intelligence, strength):
self.name = name
self.hitPoints = hitPoints
self.intelligence = intelligence
self.strength = strength

def main(): Why is this annoying?


gwaedhiel = GameCharacter('gwaedhiel', 100, 50, 10)

main()
5
In Python – with keyword parameters

class GameCharacter(object):
def __init__(self, name, hitPoints, intelligence, strength):
self.name = name
self.hitPoints = hitPoints In any order, readable!
self.intelligence = intelligence Java doesn’t allow. 
self.strength = strength

def main():
gwaedhiel = GameCharacter(name='gwaedhiel', intelligence=50,
hitPoints=100, strength=10)

main()
6
In Python – with default parameters

class GameCharacter(object): • Don’t need to


def __init__(self, name, hitPoints=100, intelligence=50, strength=10):
specify defaults.
self.name = name
self.hitPoints = hitPoints
self.intelligence = intelligence • Can mix with
self.strength = strength keyword params
def main(): • Java doesn’t
gwaedhiel = GameCharacter('gwaedhiel', 120)
allow. 
main()
7
UML for Builder Pattern
public class GameCharacter
String name;
{
Sample Java code for
int hitPoints; 8
int intelligence;
int strength; Builder pattern
public static class Builder {
GameCharacter gc = new GameCharacter();
public class Main {
public Builder name(String name) {
gc.name = name;
return this;
public static void main(String[] args) {
} // create the characters
GameCharacter gwae = new GameCharacter.Builder()
public Builder hitPoints(int hitPoints) { .hitPoints(100)
gc.hitPoints = hitPoints; .intelligence(50)
return this; .name("Gwaedhiel")
}
.strength(10)
public Builder intelligence(int intelligence) .build();
{
gc.intelligence = intelligence; GameCharacter onir = new GameCharacter.Builder()
return this; .hitPoints(400)
} .intelligence(2)
public Builder strength(int strength) {
.name("Onir")
gc.strength = strength; .strength(130)
return this; .build();
} }
}
public GameCharacter build() {
return gc;
}
}
}
9
Eager vs. lazy

 Inner class Builder can directly access outer class, assign immediately (eagerly), in which
case build() just returns the built object

 Or, it can be lazy, cache the values, and only make use of all of them when in place and
call build(), perhaps even calling the constructor of the outer class.

 More on this design pattern here:


https://en.wikipedia.org/wiki/Builder_pattern
10
String vs. StringBuilder

 What is the Big Oh?


StringBuilder sb = new
String s= ""; StringBuilder();
for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
s += i; sb.append(i);
String s = sb.toString();

You might also like