COMP2511
24T3 Week 7
Tuesday 3PM - 6PM (T15A)
Thursday 11AM - 2PM (H11C)
Slides by Christian Tolentino (z5420628)
This week
- Composite pattern
- Factory pattern
- Decorator pattern
Composite Pattern
Composite Pattern
What type of design pattern is composite?
Structural
Structural design pattern are patterns that ease the design by identifying a simple way to realize relationships among entities.
They explain how to assemble objects and classes into large structures, while keeping structures flexible and efficient
Composite pattern is useful for aggregating different objects/data. The aim is to be able to manipulate a single instance of an object just as you would manipulate a group of them.
Tree like structure of objects
Composite Pattern

- No discrimination between a single (leaf) or a group (composite). Keeps code clean
Composite Pattern

Code Demo
Calculator.java - Composite pattern
Code Demo
Inside src/calculator, use the Composite Pattern to write a simple calculator that evaluates an expression. Your calculator should be able to:
- Add two expressions
- Subtract two expressions
- Multiply two expressions
- Divide two expressions
Code Demo
{1 + [2 * (4 + 3)]}
Expression 1
Expression 2
Expression 3
Factory Pattern
Factory Pattern
What type of design pattern?
Creational
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
Factory method provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
Reduces coupling and shotgun surgery, as all classes are created using the same method.
Factory Pattern

- The Product declares the interface, which is common to all objects that can be produced by the Creator and its subclasses.
- Concrete products are different implementations of the product interface
- The Creator class declares the factory method and returns new product objects
- Concrete Creators override the base factory method so it returns a new type of product
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 24T3
By Christian Tolentino
COMP2511 Week 7 24T3
- 170