Principles of Software Construction: Objects, Design, and Concurrency��Design Practice��Hammad Ahmad and Jeremy Lacomis
1
17-214/514
Administrivia
2
17-214/514
What did we talk about on Monday?
3
17-214/514
Learning goals
Explain the decorator pattern in typical terms and recognize/understand when and how to apply it (especially in terms of the way it addresses certain limitations of inheritance).
Practice reasoning about a software design problem, recognizing how and where to consider using patterns, and gain confidence with techniques discussed in class so far.
4
17-214/514
Decorator Pattern
5
17-214/514
Limitations of inheritance
6
17-214/514
Limitations of inheritance
Goal: arbitrarily composable extensions
7
17-214/514
Limitations of inheritance
Cannot combine features
Intermediate functionality required
8
17-214/514
Workarounds?
Combining hierarchies?
9
17-214/514
Workarounds?
Multiple Inheritance?
10
17-214/514
Decorator Pattern
11
17-214/514
The Decorator design pattern
Common interface for both wrappers and wrapped objects
Class of objects being wrapped. Defines basic behavior that can be altered by decorators
Define extra behaviors that can be added to components dynamically. Override methods of the base decorator and execute their behavior either before or after calling the parent method.
Has a wrappee field for referencing wrapped objects. The type is the interface so it can contain both concrete components and decorators. The base decorator delegates all operations to the wrapped object.
The client can wrap components in multiple layers of decorators, as long as they work with all objects via the component interface.
12
17-214/514
Using the Decorator for our Stack example
13
17-214/514
The Decorator design pattern
14
17-214/514
Decorators from java.util.Collections
15
17-214/514
The UnmodifiableCollection (simplified excerpt)
public static <T> Collection<T> unmodifiableCollection(Collection<T> c) {
return new UnmodifiableCollection<>(c);
}
class UnmodifiableCollection<E> implements Collection<E>, Serializable {
final Collection<E> c;
UnmodifiableCollection(Collection<> c) { this.c = c; }
public int size() { return c.size(); }
public boolean isEmpty() { return c.isEmpty(); }
public boolean contains(Object o) { return c.contains(o); }
public Object[] toArray() { return c.toArray(); }
public <T> T[] toArray(T[] a) { return c.toArray(a); }
public String toString() { return c.toString(); }
public boolean add(E e) { throw new UnsupportedOperationException(); }
public boolean remove(Object o) { throw new UnsupportedOperationException(); }
public boolean containsAll(Collection<?> coll) { return c.containsAll(coll); }
public boolean addAll(Collection<? extends E> coll) { throw new UnsupportedOperationException(); }
public boolean removeAll(Collection<?> coll) { throw new UnsupportedOperationException(); }
public boolean retainAll(Collection<?> coll) { throw new UnsupportedOperationException(); }
public void clear() { throw new UnsupportedOperationException(); }
}
16
17-214/514
Design Practice!
17
17-214/514
Trouble Rules
Home
Finish
18
17-214/514
Trouble Rules
19
17-214/514
Trouble Rules
Rolling a 4 means the green player can move to the last empty space
Rolling a 3 means that the green player can move to where the red player’s peg piece is, sending it back to the red home
20
17-214/514
Trouble Rules
Finish
21
17-214/514
Trouble Rules Summary
2-4 players are trying to move their pieces from their home to their finish zone. Players roll a D6 and move their pieces clockwise, rolling a 6 lets them roll again. To exit their home zone, they must roll a 6 and move a piece from their home to their start position. Landing on an opponent’s piece sends it back to the opponent’s home zone. Players may not have two of their pieces in the same position. Exact rolls are required when moving to the finish zone. The first player to have all of their pieces in their own finish zone is the winner.
22
17-214/514
Trouble Rules Summary
2-4 players are trying to move their pieces from their home to their finish zone. Players roll a D6 and move their pieces clockwise, rolling a 6 lets them roll again. To exit their home zone, they must roll a 6 and move a piece from their home to their start position. Landing on an opponent’s piece sends it back to the opponent’s home zone. Players may not have two of their pieces in the same position. Exact rolls are required when moving to the finish zone. The first player to have all of their pieces in their own finish zone is the winner.
23
17-214/514
Domain Model
24
17-214/514
Domain Model
25
17-214/514
Domain Model
26
17-214/514
System Sequence Diagram
27
17-214/514
System Sequence Diagram
28
17-214/514
Object Model
29
17-214/514
Interaction Diagram
30
17-214/514
Summary
31
17-214/514