COMP2511
25T3 Week 7
Tuesday 10am-1pm (T10A)
Start 10:05am
This Week
- General Admin
- Assignment 1 Feedback
- Concurrency Overview
- Singleton Pattern
- Abstract Factory Pattern
- Decorator Pattern
General Admin
- Assignment 2 is out - please start early! (due Week 10 Wednesday, 19th November)
- Make sure to update your blogs as you go - no blogs = no marks
- Please keep each task to one PR only
- Assignment 1 manual marking is underway! You will be able to find feedback on the Marking merge request in your repo
- Please take this feedback into consideration for assignment 2
- Any questions feel free to ask in lab
- Final assignment marks should be out by early next week
- You should also be able to check whether your project compiled on the compilation branch on your repo. If not, you can submit a rerun request on the forum with a penalty.
Assignment 1 Feedback
-
Some tips/feedback from marking the assignments:
-
Coupling/Cohesion:
- The getInfo methods in TrainsController could've been moved to their subclasses to reduce coupling and feature envy
- Talk more about why you made certain design decisions in your blog :)
- Careful about magic numbers throughout your code (use constants!)
- Avoid type checking using hard coded strings - prefer instanceof or polymorphism to avoid type checking completely
-
Coupling/Cohesion:
Concurrency
Concurrency
- Concurrency is the ability of a program to execute different sections of code seemingly simultaneously.
- There are many ways to achieve concurrency:
- Single-threading - imagine a single chef in a kitchen who has to wait for a steak to finish cooking before moving onto other ingredients.
- Multi-threading - having multiple chefs preparing different dishes at the same time.
- Asynchronous - works a little differently, but imagine a single chef who is able to start the steak, then while it's cooking, do something else at the same time. We make better use of our time.
- It is also important to know that, under the hood, the operating system chooses which programs should run and when, at the millisecond scale.
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
Overcooked
Abstract Factory Pattern
- Abstract factories can be used to create different types of families of objects
- Consider making a GUI app with a light and dark mode. We might create an abstract factory to provide a common interface for our different factory types
// Abstract Products
interface Button { void render(); }
class LightButton implements Button { public void render() { System.out.println("🟡 Light Button"); } }
class DarkButton implements Button { public void render() { System.out.println("⚫ Dark Button"); } }
// Abstract Factory
interface GUIFactory { Button createButton(); }
class LightFactory implements GUIFactory { public Button createButton() { return new LightButton(); } }
class DarkFactory implements GUIFactory { public Button createButton() { return new DarkButton(); } }
// Client Code
public class AbstractFactoryExample {
public static void main(String[] args) {
GUIFactory factory = new DarkFactory(); // Switch to LightFactory for light mode
factory.createButton().render(); // ⚫ Dark Button
}
}Code Demo
Thrones.java Part 1 - Abstract Factory Pattern
Code Demo
We want to simulate the board game with the added change that pieces can be made from different materials, either Wood, Plastic or Metal. We want to be able to pick and choose which material we create a board with. Each material has its own construction requirements:
- Metal pieces are heavy and hence can only be set with
0 <= x <= 5, andy = 0 - Plastic pieces are lightweight and can be placed anywhere within a given
bound(provided to the factory) - Wooden pieces are large and bulky, and take up a
nxngrid, meaning they can only be placed on tiles withxandyvalues which dividen(nis provided to the factory)
Decorator Pattern
Decorator Pattern
- The Decorator Pattern is essentially a wrapper around a class
- It is a structural pattern
- We can make our base item and wrapper implement the same interface
- This allows us to add and change behaviour dynamically
- It also allows us to have multiple layers of decorators
-
Consider:
- It's cold outside, and you are wearing a t-shirt. What do you do?
- Put on a jacket (decorator), and you "extend" your basic state and are now warmer 😎
- You're still cold, so you now wear a scarf 🧣
interface Coffee { double cost(); }
// Concrete Component
class SimpleCoffee implements Coffee {
public double cost() { return 5.00; }
}
// Decorator
abstract class CoffeeDecorator implements Coffee {
protected Coffee coffee;
public CoffeeDecorator(Coffee coffee) { this.coffee = coffee; }
public double cost() { return coffee.cost(); }
}
// Concrete Decorators
class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee coffee) { super(coffee); }
public double cost() { return coffee.cost() + 1.50; }
}
class SugarDecorator extends CoffeeDecorator {
public SugarDecorator(Coffee coffee) { super(coffee); }
public double cost() { return coffee.cost() + 0.75; }
}
public class DecoratorExample {
public static void main(String[] args) {
Coffee coffee = new SimpleCoffee();
coffee = new MilkDecorator(coffee);
coffee = new SugarDecorator(coffee);
System.out.println("Total cost: $" + coffee.cost()); // $7.25
}
}Code Demo
Thrones.java Part 2 - Decorator Pattern
Code Demo
Now we want our game to support adding different armour types for our pieces:
- Helmet - reduces incoming damage by 1
- Chain mail - halves incoming damage (rounded down)
- 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).
We can use the Decorator Pattern implement this really easily!
COMP2511 Week 7 25T3
By Sam Zheng
COMP2511 Week 7 25T3
- 114