Design PatternsCategory: /knowledge /development
Tags: development design patterns
- you solve similar problems again and again.
- for these similar problems, you need to figure out what are difference and what stay the same, then isolate the changes in your code.
- if all programmers understand this set of common names, communication and conversation are a lot easier.
Q: what is singleton?
A: A singleton is a class that can be instantiated only one time in a JVM per class loader. This class has only one instance, and provide a global point of access.
Q: when to use?
A: Use it when only a single instance of an object is required in memory for a single point of access. E.g.
- Accessing application properties through a single object.
- Accessing in-memory object cache, or resource pools like sockets, connections. etc.
Factory pattern returns one of the several production subclasses. It makes the system independent of the object creation process.
One level of abstraction higher than a factory method pattern, which means it returns the factory classes.
It’s good for one publiser, a list of unknown subscribers to be notified for the update. E.g. when a course information gets modified, students, professors and college adminstration office all need to be alerted for the update.
java.util.Observableis a class, not an interface, addObserver/removeObserver/notifyObservers methods are relative static.
java.util.Observeris an interface, since
update()usually changes for sure.
Command pattern: Command objects encapsulate an action and its parameters
Memento pattern: Provides the ability to restore an object to its previous
Mediator pattern: Provides a unified interface to a set of interfaces in a
State pattern: A clean way for an object to partially change its type at
Interpreter pattern: Implement a specialized computer language to rapidly
solve a specific set of problems
Chain of responsibility pattern: Command objects are handled or passed on to
other objects by logic-containing processing objects