COMP2511

25T3 Week 7

Tuesday 10AM - 1PM (T10C)

 

Slides by Christian Tolentino (z5420628)

This week

  • Singleton pattern
  • Factory pattern
  • Decorator pattern

Concurrency

What is Concurrency?

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.

Types of Concurrency

  • Single-threading: Executes line after line, without interruption.
    • One chef in a kitchen, who cooks a steak, and waits for it to be finished before moving onto the potatoes, then the veggies. 
  • Multi-threading: Different threads run different parts of your code. Creating multiple threads in your programs allows you to execute lines simultaneously, instead of one after the other.
    • Multiple chefs to prepare different dishes at the same time. One can prepare the steak, another the potatoes, and another the veggies.
  • Asynchronous: One thread manages tasks. The thread manages it's tasks by having them yield control when they are busy doing something else.
    • The chef first cooks steak, then, while it's on the grill, moves on to boiling the potatoes. While the potatoes boil, the chef can start preparing the veggies. The chef then moves around the kitchen to the different tasks as they require attention. 

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

Data race

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.

  1. 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.
  2. Use the Factory Pattern to create a series of object factories for each of the character types, and change the main method of Game.java to 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.

COMP2511 Week 7 25T3

By Christian Tolentino

COMP2511 Week 7 25T3

  • 43