COMP2511
25T2 Week 7
Friday 9AM - 12PM (F09A)
Slides by Christian Tolentino (z5420628)
This week
- Singleton pattern
- Factory pattern
- Decorator pattern
Singleton Pattern
Singleton Pattern
What type of pattern?
Creational pattern
The singleton pattern ensures that a class has only one instance. It provides a global access point to this instance.
It helps avoid initialisation overhead when only 1 copy of an instance is needed.

Code Demo
Heist.java

Code Demo
Thrones.java - Factory Pattern
Code Demo
Inside src/thrones, there is some code to model a simple chess-like game. In this game different types of characters move around on a grid fighting each other. When one character moves into the square occupied by another they attack that character and inflict damage based on random chance. There are four types of characters:
- A king can move one square in any direction (including diagonally), and always causes 8 points of damage when attacking.
- A knight can move like a knight in chess (in an L shape), and has a 1 in 2 chance of inflicting 10 points of damage when attacking.
- A queen can move to any square in the same column, row or diagonal as she is currently on, and has a 1 in 3 chance of inflicting 12 points of damage or a 2 out of 3 chance of inflicting 6 points of damage.
- A troll can only move up, down, left or right, and has a 1 in 6 chance of inflicting 20 points of damage.
Code Demo
We want to refactor the code so that when the characters are created, they are put in a random location in a grid of length 5.
- How does the Factory Pattern (AKA Factory Method) allow us to abstract construction of objects, and how will it improve our design with this new requirement?
- Abstract the construction of the character objects. We don't deal with the constructor, instead call a general factory method that handles the number.
- Use the Factory Pattern to create a series of object factories for each of the character types, and change the
mainmethod ofGame.javato use these factories.
Decorator Pattern
Decorator Pattern
What type of design pattern?
Behavioural
Behavioural patterns are patterns concerned with algorithms and the assignment of responsibility between object
The decorator pattern allows you to dynamically add new behaviours or responsibilities to objects without modifying their existing code.
Gives code more flexibility, reduces coupling and separates functionality into smaller, reusable classes (single-responsibility)

Code Demo
Thrones.java - Factory Pattern
Code Demo
Suppose a requirements change was introduce that necessitated support for different sorts of armour.
- A helmet reduces the amount of damage inflicted upon a character by 1 point.
- Chain mail reduces the amount of damage by half (rounded down).
- A chest plate caps the amount of damage to 7, but also slows the character down. If the character is otherwise capable of moving more than one square at a time then this armour restricts each move to distances of 3 squares or less (by manhattan distance).
Use the Decorator Pattern to realise these new requirements. Assume that, as this game takes place in a virtual world, there are no restrictions on the number of pieces of armour a character can wear and that the "order" in which armour is worn affects how it works. You may need to make a small change to the existing code.
Assignment-ii
Admin
- No blogs, no marks. (Including access, layout, MRs).
- Any contribution issue should be raised at the end of each week (either in person, or via email is fine)
COMP2511 Week 7 25T2
By Christian Tolentino
COMP2511 Week 7 25T2
- 46