Blog Posts
Task 1 Part d)
Design Smells
Part 2
Part 2 is purely software architecture
Concurrency is the ability of a program to execute different sections of code simultaneously.
It basis behind all of modern computing and is how the operating system on your computer is allows multiple programs to actively coexist.
This is how your computer is able to watch Youtube while the clock on your desktop ticks.
For this course, concurrency is important in understanding other concepts we will cover like the singleton pattern and microservice / event-driven architecture.
There are multiple ways of achieving concurrency:
Up until now, most of the code you have written is single-threaded; it executes line after line, without interruption.
Tasks are processed one after another, if one task is very slow or "blocks" the thread, all other tasks must wait.
Think of this like having one chef in a kitchen, who cooks a steak, and waits for it to be finished before moving onto the potatoes, then the veggies.
At the end, only the veggies are warm and we wasted time sitting around, as we waited for each prior dish to be finished before continuing.
This is where 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.
Think of this like having multiple chefs to prepare different dishes at the same time. One can prepare the steak, another the potatoes, and another the veggies.
The benefit, is all the food can come out warm and in a timely manner.
Instead of spawning multiple threads to run code at the same time, we have one thread that manages tasks.
The thread manages it's tasks by having them yield control when they are busy doing something else. Once the task is ready to run again, we continue.
Think of this like having one chef, that starts cooking the 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.
The benefit is we make better use of our time, and the dish comes out warm and timely.
It's important to know that, under the hood, the operating system delegates execution time to your program as it sees fit. It chooses which programs should run, and when, at the millisecond scale.
As such, this "context-switching" makes it seem like programs are executing code at the same time, when in reality the OS is alternating who gets to run their code very, very quickly.
COMP3231 (OS) teaches this in more detail!
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 oven at the same time. This can lead to undesirable behaviour like
Preheating oven to 180°C...
Loaded the oven with 10 cakes...
---
Jamie puts on the oven mitts and opens the oven...
Julia puts on the oven mitts and opens the oven...
Gordon puts on the oven mitts and opens the oven...
Jamie took a cake out! (9 left)
Julia took a cake out! (8 left)
Gordon took a cake out! (7 left)
Julia took a cake out! (6 left)
Gordon took a cake out! (4 left)
Jamie took a cake out! (5 left)
Julia took a cake out! (3 left)
Jamie took a cake out! (1 left)
Gordon took a cake out! (2 left)
Gordon closes the oven and takes off the mitts.
Jamie took a cake out! (-1 left)
Julia took a cake out! (0 left)
Julia closes the oven and takes off the mitts.
Jamie found the oven empty!
Jamie closes the oven and takes off the mitts.
---
Cakes left in oven: -1
Oven only has 10 cakes, but 11 were taken out!
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 chef can access the oven at a time!
If we control the amount of Mitts in the kitchen, we can control when Chefs access the oven.
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