- Design Patterns are commonly used ways to structure your code.
- A design pattern is meant to solve a particular problem, so they are described in terms of the type of problem to solve.
- Using a design pattern that does not match the problem is an example of very poorly design software. In other words, don't do it!
- Design patterns are ways in which to design and implement a system to be reusable,
extensible, and testable.
- Design patterns build on the Class/Object relationships
we have already discussed.
- Some common terms used when discussion Design and Patterns:
- Composition is the idea that an object "contains" other objects
as attributes.
These contained objects don't have a life of their own and if the object that "has" them
ceases to exist, they do too. This is strong association (POJO). Note that in this context, "contains" is not referring to Container classes.
-
Aggregation
is the idea that an object can be made up of other objects, but the objects can live independently too. Aggregration is done with instance variables that are references where the object is passed
into the object via a constructor or a modifier. In other words, the attribute is NOT
created in the class, but is done outside the class and passed in and the reference is stored in an instance variable.
This is composition using weak association.
Note that in Java, Container classes use Aggregation (weak association), not strong association.
- Delegation
is the idea that an object can delegate some behavior or data to other objects that it references.
This is also commonly called "indirection" in class design. This is often done via aggregation or
via method calls that take a reference to an object; like Aggregation, Delgation
is also weak association.
- High cohesion (typically "good" design) means that
the responsibilties and attributes (methods and and instance variables) in
a class are strongly related, highly focused, and properly encapsulated. A class should represent
one thing, not many things.
- Low coupling or Loose coupling
(typically "good" design") means designing classes to not rely on knowing the internals of
other classes. This leads to lower dependencies between classes so that if the internal
implementation of one class changes, it has a lower (or ideally, no) impact on the rest of
the system.
Inner classes are usually an example of a tight coupling and when
used properly can also demonstrate "good" design.
- Separation of concerns ("good" design) entails partitioning
functionaility into distinct and well-defined peices. This applies to class design, hierarchies,
modules, APIs, ... Properly done encapsulation is an example of separation of concerns; high
cohesion will also lead to separation of concerns.
- Open/closed ("good" design) means that your code (classes,
packages, sections) is open for extension but closed for modification. This is a major goal of
an implemented design.
The ability to improve, modify, or refactor one section of code without having dependencies
on the implementation details of other sections, and without having to make corresponding
changes to those sections is a major goal of good design and design patterns.
References: This is a great book on Design Patterns: Head First Design Patterns
Decent online References:
http://sourcemaking.com/design_patterns/adapter
http://java.dzone.com/articles/design-patterns-uncovered-0
https://dzone.com/refcardz/design-patterns
http://sourcemaking.com/design_patterns/proxy/java/1
http://java.dzone.com/articles/design-patterns-proxy
http://stackoverflow.com/questions/350404/how-do-the-proxy-decorator-adapter-and-bridge-patterns-differ