What is it?
Behavioural pattern which defines a subscription mechanism that notifies observers about any event that happens to the subject that they are observing
What problem does it solve?
Modelling a n-to-many relationship may be difficult. If
not done well, it typically leads to tight coupling
When should we use this pattern?
When changing the state of one object requires changing other objects in turn
Subject
Maintains a list of observers and notifies them of state changes. During this notification, they are passing data (push/pull)
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers(data); // Calls update() of observersObserver
Register (and unregister) themselves on a subject and to update their state when they are notified.
// Two possible options for update()
public void update(Subject obj); // PULL data from subject
public void update(data); // PUSH data to observersModel the system in Java!
What is it?
Structural design pattern that allow objects to be composed into a tree structure such that individual objects and composites of such objects can be treated in the same way.
When to use?
Model a group of classes that forms a hierarchy
In Unix based operating systems, directories are just files that can contain other files
Leaf Nodes
Composite Nodes
UML Diagram
Leaf and Composite both implement the same interfaceLeaf and Composite can be treated as the sameComposite can hold more than one LeafProduces a tree-like structure that leans itself very well towards recursion
Use the composite pattern to design a simple calculator that can be used to evaluate an arithmetic expression.
Your calculator should be able to:
Tree representation
exp1 = 42
exp2 = ((1 + 2) - ((3 / 4) * (5))
UML Diagram
composite nodes
leaf node
Why use it?
What is it?
Creational design pattern that lets you define an interface for creating objects of a family in a superclass but allows subclasses to alter the type of object that will be created.
Imagine we are developing a game with different characters!
public class Game {
public static void main(String[] args) {
Game game = new Game();
game.addCharacter(new King(0, 0));
game.addCharacter(new Dragon(0, 1));
game.addCharacter(new Queen(2, 2));
game.play();
}
}Refactor the code such that when the characters are created, they are put in a random location in a grid of length of length 5.
Here, because our number of characters that are needed to be created is relatively small, we can place all in a single CharactorFactory class.
CharacterFactory.createKing etc, which makes our code more clean and less likely for Game to cause problems with creating the objects itself.How does the Factory Pattern allow us to abstract construction of objects, and how will it improve our design with this new requirement?
There is another version of the Factory pattern where we create a factory for each concrete product.
EntityFactory
KingFactory
QueenFactory
Character
King
Queen
When to use it?
What is it?
Creational design pattern that lets you produce families of related objected without specifying concrete classes.
Say we decide to further extend our game, so that we have several variants.
For example, products King + Queen + Knight + Dragon are available in different variants:
KingdomA, KingdomB
The abstract factory is the interface with all the creation methods in the product family.
For each family variant, we create a separate factory class
KingdomACharacterFactory
KingdomBCharacterFactory
CharacterFactory
KingdomAKing
KingdomBKing
King