5-design-patterns-java-structural-m5-slides
5-design-patterns-java-structural-m5-slides
Bryan Hansen
twitter: bh5k | http://www.linkedin.com/in/hansenbryan
Concepts
▪ Also called a wrapper
▪ Add behavior without affecting others
▪ More than just inheritance
▪ Single Responsibility Principle
▪ Compose behavior dynamically
▪ Examples:
▪ java.io.InputStream
▪ java.util.Collections#checkedList
▪ UI components
Design
Inheritance based
Utilizes composition and inheritance (is-a,
has-a)
Alternative to subclassing
Constructor requires instance from hierarchy
UML
Everyday Example - InputStream
File file = new File("./output.txt");
file.createNewFile();
OutputStream oStream = new FileOutputStream(file);
DataOutputStream doStream = new DataOutputStream(oStream);
doStream.writeChars("text");
Exercise Decorator
Component, ConcreteComponent,
Decorator, ConcreteDecorator
Create Decorator
Implement another Decorator
Not a Creational Pattern
Pitfalls
▪ New class for every feature added
▪ Multiple little objects
▪ Often confused with simple inheritance
Contrast
Composite Decorator
▪ Tree structure ▪ Contains another entity
▪ Leaf and Composite have same ▪ Modifies behavior (adds)
interface ▪ Doesn’t change underlying object
▪ Unity between objects
Decorator Summary