|Software design patterns|
clasification on usage
+ - Creational Patterns
Provide an interface for creating families of related or dependent objects
without specifying their concrete classes.
Separate the construction of a complex object from its representation so that
the same construction process can create different representations.
creation of an aggregate's elements
Define an interface for creating an object, but let subclasses decide which
class to instantiate. Factory Method lets a class defer instantiation to
virtual creation through inheritance
Descriptive names- avoid overloading
limitations: extensibility -> inheritance
Specify the kinds of objects to create using a prototypical instance, and create
new objects by copying this prototype.
Ensure a class only has one instance, and provide a global point of access to
public static Singleton getInstance()
~>Initialization on demand holder idiom
class initialization phase is guaranteed by the JLS to be serial
+ - Structural Patterns
Convert the interface of a class into another interface clients expect. Adapter
lets classes work together that couldn't otherwise because of incompatible
wrap a legacy component
Decouple an abstraction from its implementation so that the two can vary
decouple interface and implementation
Compose objects into tree structures to represent part-whole hierarchies.
Composite lets clients treat individual objects and compositions of objects
Composite Compose objects into tree structures to represent part-whole
hierarchies. Composite lets clients treat individual objects and compositions of
scrollbar is the "decoration" to the webpage
Provide a unified interface to a set of interfaces in a subsystem. Facade
defines a higher-level interface that makes the subsystem easier to use.
simple front-end to a complicated subsystem
Use sharing to support large numbers of fine-grained objects efficiently.
use sharing to optimize lots of "little" objects
use hashmap (week) to cache, override equals, hashcode
Provide a surrogate or placeholder for another object to control access to it.
Chain of Responsibility
Avoid coupling the sender of a request to its receiver by giving more than one
object a chance to handle the request. Chain the receiving objects and pass the
request along the chain until an object handles it.
single processing pipe, many possible handlers
+ - Command
Encapsulate a request as an object, thereby letting you parameterize clients
with different requests, queue or log requests, and support undoable operations.
Analogy: meal order at a restaurant
encapsulate a request as an object
thread pool class works with an internal queue of tasks waiting to be done.
Tasks are Command objects.
Define a family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from clients
that use it.
algorithms can be selected on-the-fly at runtime;
uses composition instead of inheritance.
Example - Routing Strategies: Static, Dinamic
Define a one-to-many dependency between objects so that when one object changes
state, all its dependents are notified and updated automatically.
Analogy: news subsscription
Allow an object to alter its behavior when its internal state changes. The
object will appear to change its class.
object's behavior morphs as its state changes
State is more dynamic than strategy
Without violating encapsulation, capture and externalize an object's internal
state so that the object can be restored to this state later.
checkpoint/rollback an object's internal state
Provide a way to access the elements of an aggregate object sequentially without
exposing its underlying representation.
Analogy: secr accessing her file system
decouple traversal of an aggregate object
Given a language, define a represention for its grammar along with an
interpreter that uses the representation to interpret sentences in the language.
Analogy: musician reading music
Represent an operation to be performed on the elements of an object structure.
Visitor lets you define a new operation without changing the classes of the
elements on which it operates.
decouple operations on an aggregate object
ability to add new operations to existing object structures without modifying
~>The Visitor Design Pattern
Define the skeleton of an algorithm in an operation, deferring some steps to
subclasses. Template Method lets subclasses redefine certain steps of an
algorithm without changing the algorithm's structure.
constant algorithm, replaceable steps
Define an object that encapsulates how a set of objects interact. Mediator
promotes loose coupling by keeping objects from referring to each other
explicitly, and it lets you vary their interaction independently.
Analogy: airport control tower
+ - Concurrency patterns
Thread pool pattern
will work with volatile in >= j2se5.0
The "Double-Checked Locking is Broken" Declaration
Just define the singleton as a static field in a separate class.
It will work for 32-bit primitive values
Read/write lock pattern
ThreadLocal local = new ThreadLocal();
Hoare vs. Mesa semantics
Design Patterns (GOF)
a pattern that tells how to go from a problem to a bad solution.
~>+ - Spaghettiness
spaghetti code often is all code that is not our own because it was generated by
minds that think differently than us.
+ - Organizational
~>Untested But Finished
~>+ - Give Me Estimates Now
estimate how much time and extra info is need to produce the desired estimate
boss doesn't buy it. Lower accuracy is suggested instead. Funny thing is however
inaccurate the estimate is said to be, it turns into a deadline.
~>If It Is Working Dont Change
~>Design For The Sake Of Design
+ - Over Use Of Patterns
Developers, having seen the light regarding the usefulness of patterns, try to
use them wherever they can, regardless of whether the pattern is appropriate.
The resulting code tends to be full of factories that only ever return a single
thing, that thing being a proxy to a delegate that wraps a class that has a
method that has the three lines of code you actually want to execute.
+ - tools
includes 16 design patterns of the Gang of Four (GoF).
~>Design Pattern Toolkit
create patterns and generates applications from patterns
~>go4expert.com > Forums > Showthread ? ...
~>Design Patterns http://www.vincehuston.org/dp/
~>Design Patterns (exciton)
~>Design Class Diagrams
~>Unlike components, does not provide reuse