Books
- The timeless way of building
- Gang of Four, Design Patterns: Elements of Reusable Object-Oriented Software
- Head First Design Patterns
- A Pattern Language
Advantages
- More flexible
- Easier to add/remove/modify behavior
- Easier to reuse behavior
- Easier to understand
- Less code to write/debug/maintain
- More efficient method of creating software
- More value per line-of-code
Strategy, proxy, decorator
Strategy
- Strategy
- Strategy pattern is just polymorphism.
- “Code to interfaces, not to their implementations”
- Proxy
- is the delegation strategy: A strategy which uses a strategy.
- Lets you swap implementation without the knowledge of clients
- Decorator
- A proxy which adds behavior.
- Add new behaviors to objects dynamically at runtime, flexible alternative to subclassing/inheritance.
- Decoration reuses an Object (dynamic); inheritance reuses a Class (static, compile time).
- Separation of concerns: breaking a program into distinct features that overlap as little as possible. A concern can be considered as a feature of behavior.
- Implement features separately, small, focused classes.
- Avoids # class explosion.
- 10 classes, adding 5 behaviors
- Inheritance: 10 * 2^5
- Decorator: 10 + 5
- 10 classes, adding 5 behaviors
- Advantages:
- Efficient to implement behavior
- Less code to write/maintain.
- Separation of Concerns
- Easier to extend
- Testable
// Decoration
public class SynchronizedRunnable extends RunnableProxy {
public SynchronizedRunnable(Runnable delegate) { // Takes an object
super(delegate);
}
synchronized public void run() {
this.delegate.run();
}
}
// Inheritance
public class SynchronizedMyRunnable extends Runnable {
public SynchronizedMyRunnable() {}
synchronized public void run() {
this.super();
}
}
Path to MVC
- Observer Pattern
- Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Observerble{attach(Observer); detach(Observer); notify()}; Observer{update()}
- Mediator Pattern
- A mediator serves as an intermediary that keeps objects in a group from referring to each other explicitly.
(Mediated object 1) -> (Mediator) <- (Mediated object 2)
- Adaptor and Bridge
- Special types of strategy.
- Rely on another object to do the job(interface), which has a different interface.
- Composite Pattern
- Compose objects into tree structures to represent part-whole hierarchies. Clients can then treat individual objects and compositions of objects uniformly.
- Any idea is better when made recursive – Brian Randell.
- Flyweight
- Use sharing to support large numbers of fine-grained objects efficiently.
- Often implemented as a stateless Singleton.
- Object to be worked with is passed as an argument.
- Useful for creating extensions to existing classes.
- Java comparator example:
public interface Comparator { int compare(Object o1, Object o2)}
MVC
- Model: Data model
- Adaptor/Bridge strategy representing data, state and logic
- View: UI
- Adaptor strategy for interacting with users
- Controller: control logic, mediator
- Mediator between model and view
Good to always use Factory to create objects, which is more flexible and can have different creation options.