25T3 Week 7
Tuesday 10AM - 1PM (T10C)
Slides by Christian Tolentino (z5420628)
Concurrency is the ability of a program to execute different sections of code seemingly simultaneously. It is the basis behind all of modern computing, and it is how your computer is able to do so much all at once. It's how your clock is able to tick while you watch a youtube video, and it's how your operating system allows multiple programs to actively coexist.
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.
Heist.java
Data race
Thrones.java - Factory Pattern
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:
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.
main method of Game.java to use these factories.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)
Thrones.java - Factory Pattern
Suppose a requirements change was introduce that necessitated support for different sorts of armour.
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.