This file contains my personal annotations on the Design Patterns subjects that I will be reading on the “Head First: Design Patterns” book.
Source code of the book can be found on: http://wickedlysmart.com/head-first-design-patterns.
Source code of my study: https://github.com/aang7/DesignPatternsExamples/.
Design principles on STRATEGY
pattern:
The Strategy Pattern
defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
The Observer Pattern defines a one-to-many relationship between a set of objects. When the state of one object changes, all of its dependents are notified.
When two objects are loosely coupled, they can interact, but they typically have very little knowledge of each other. Loosely coupled designs often give us a lot of flexibility.
Design principles found in this chapter:
Decorators are meant to add behavior to the object they wrap.
Design principles found in this chapter: 1.Classes should be open for extension, but closed for modification. (This is called the Open-Closed Principle)
the Java.io
package is one example of many where the Decorator Pattern is largely used.
Java I/O also points out one of the downsides of the Decorator Pattern: designs using this pattern often result in a large number of small classes that can be overwhelming to a developer trying to use the Decorator-based API. But now that you know how Decorator works, you can keep things in perspective and when you’re using someone else’s Decorator-heavy API, you can work through how their classes are organized so that you can easily use wrapping to get the behavior you’re after.
Good points:
Bad points:
Inside the Factory Pattern we have a few variants:
Examples of the pattern can be found on my repository1 under this path.
We can say that in this pattern we have the creator and the product. Where the creator is the factory itself and the product is the one that requires the creator. So the we have the creator classes and the product classes.
This is easily the shortest pattern so far in the book. The singleton pattern basically handle the creation of only one instance of a determined class.
In java, depending on the version will be the implementation to use. There are basically two ways of implement this pattern
getInstance
which basically handles the creation and return of the only once instantiated object.For more details please refer to this article.