Lec18 Patterns 1
Lec18 Patterns 1
Kevin Zatloukal
Winter 2019
Design Patterns, Part 1
(Based on slides by M. Ernst and many others)
Reminder
• Resubmission instructions on web site
• Course evaluations
– https://uw.iasystem.org/survey/204647
Solution:
– object maintains a list of observers with a known interface
– calls a method on each observer when state changes
Disadvantages:
– code can be harder to understand
– wastes memory by maintaining a list of objects that are known
a priori (and are always the same)
Solution:
– the implementation performs traversals, does bookkeeping
– results are communicated to clients via a standard interface
(e.g., hasNext(), next())
Disadvantages:
– less efficient: creates extra objects, runs extra code
– iteration order fixed by the implementation, not the client
(you can have return different types of iterators though...)
CSE331 Winter 2019 5
Why (more) design patterns?
Design patterns are intended to capture common solutions / idioms,
name them, make them easy to use to guide design
– language independent
– high-level designs, not specific “coding tricks”
• Have an object with fields / methods that are “like public, static
fields / methods” but have a constructor decide their values
– cannot be static because need run time info to create
– e.g., have main decide which files to give CampusPaths
– rest of the code can assume it exists
Advantages:
– to switch the implementation, change only one place
• Advantages:
– no longer risks horrifying bugs
– can pass factories around at runtime
• e.g., let main decide which one to use
• Disadvantages:
– uses bit of extra memory
– debugging can be more complex when decision of which
object to create is far from where it is used
Reminder: Not shown here is also using factories for creating races
CSE331 Winter 2019 28
Builder
Builder: object with methods to describe object and then create it
– fits especially well with immutable classes when clients want to
add data a bit at a time
• (mutable Builder creates immutable object)
Example 1: StringBuilder
StringBuilder buf = new StringBuilder();
buf.append(“Total distance: ”);
buf.append(dist);
buf.append(“ meters”);
return buf.toString();
Example 2: Graph.Builder
– addNode, addEdge, and createGraph methods
– (static inner class Builder can use private constructors)
– looks reasonable to disallow removeNode here
• but you probably still need containsNode
• (This can be done in a general manner, but it’s way out of scope for this class.)
CSE331 Winter 2019 31
Builder Idioms
Builder classes are often written like this:
class FooBuilder {
public FooBuilder setX(int x) {
this.x = x;
return this;
}
public Foo build() { ... }
}
into this
myMethod(x, y, Options.create()
.setA(true)
.setB(false)
.setC(true).build());
Factory method
– call a method to create the object
– method can do any computation and return any subtype
Factory object (also Builder)
– Factory has factory methods for some type(s)
– Builder has methods to describe object and then create it
Prototype
– every object is a factory, can create more objects like itself
– call clone to get a new object of same subtype as receiver