Blog Posts
Task 1 Part d)
Design Smells
Task 2
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
CharacterFactory
MetalFactory
PlasticFactory
MetalKing
MetalQueen
PlasticKing
PlasticQueen
King
Queen
Game
In tutorial 04, we discussed the factory pattern. We'll now look at a slightly modified scenario where the abstract factory pattern is more appropriate
What is it?
Structural design pattern that allows the behaviour of objects to be enhanced placing them into special wrapper objects
What problems does it solve?
Directly subclassing to combine different behaviours can lead to a large number of subclasses.
Why not just use inheritance?
Component
is common interface for both wrapper and wrapped objectsBaseDecorator
has-a (composition) with Component
. It then delegates operations to wrappee object
wrappee
wrapper
public void execute() {
wrappee.execute();
}
Updated Requirements:
Component Interface (Character
)
damage()
, canMove()
)Concrete Component (CharacterBase
)
Character
CharacterBase
CharacterDecorator
Helmet
What is this?
Creational design pattern that specifies that a class can only have one instance. Accessing the instance must be via a global access point.
Why use it?
The instance is only created when the getInstance
method is first called.
Currently, the threads can all access the bank account at the same time. This can lead to undesirable behaviour like
Denver is accessing the bank.
Tokyo is accessing the bank.
The final balance is: $100
Rio is accessing the bank.
Rio successfully withdrew $20
Tokyo successfully withdrew $6
Denver successfully withdrew $49
Tokyo successfully withdrew $6
Denver failed to withdraw $49.
Rio successfully withdrew $20
Rio failed to withdraw $20.
Denver is leaving the bank, the balance is -1
Tokyo failed to withdraw $6.
Rio failed to withdraw $20.
Tokyo failed to withdraw $6.
Rio failed to withdraw $20.
Tokyo failed to withdraw $6.
Tokyo failed to withdraw $6.
Rio is leaving the bank, the balance is -1
Tokyo is leaving the bank, the balance is -1
Bank account only had $100 but $101 was taken out!
Currently, the threads can all access the bank account at the same time. This can lead to undesirable behaviour like
The undesired behaviour is caused by race conditions
This is when two or more threads can access shared data and they try to change it at the same time.
Because the thread scheduling can swap between threads at any time, you don't know the order the threads will access the shared data
How does Singleton Pattern fix this?
Refactor using the Singleton Pattern to ensure only one person can access the bank account at a time!