COMP2511
Tutorial 7
Overview
- Assignment 2 tips
- Composite Pattern
- Factory Pattern
- Decorator Pattern
Good & Bad News
- Congrats on finishing assignment 1!
- Marks and feedback will be coming soon!
- Assignment 2 should be underway (sorry 🙇)
- Incorporate feedback from assignment 1

Assignment 2 Tips
Assignment 2 Tips
Blog Posts
- Follow the templates in the spec and attach the merge request to the correct task
- Individual blog template: https://tinyurl.com/bdp5fj3w
- Pair blog template: https://tinyurl.com/mu8uy8cr
- Tutors will not have enough time to look through your codebase for things not mentioned in the blogs
- Make it easy for us to award you marks :D
Assignment 2 Tips
Task 1 Part f)
- Address all 3 problems mentioned in the spec
- Find and resolve more problems for higher marks
- Do not intentionally write bad code in earlier parts and refactor them in this section
- Do not refactor to resolve a problem that does not exist yet
Code Smells
If you think you have found a code smell, make sure to double check here: Code Smells (refactoring.guru)

Assignment 2 Tips
Task 2
- All groups must complete part a
- Choose N tasks from b-f, where N is the number of members in your group
- Earlier subtasks are easier but doing them will not result in a high mark
- We give you the flexibility to decide what a desirable grade is to your group
Composite Pattern
Composite Pattern
Suppose we have
- A
Filecan store data like text or images etc - A
Directorycan store both files and other directories


Composite Pattern
Suppose we have
- A
Filecan store data like text or images etc - A
Directorycan store both files and other directories - Together files and directories form a hierarchy that makes up a
FileSystem

This is a tree-like structure!
Composite Pattern
UML Diagram
-
File(Leaf) andDirectory(Composite) both implement a common interface- They can be treated as the "same thing"
- A
Directorycan store both files and directories (i.e.addandremove)

Composite Pattern
Use the composite pattern to design a simple calculator that can be used to evaluate an arithmetic expression
Code Demo
Your calculator should be able to:
- Add two expressions
- Subtract two expressions
- Multiply two expressions
- Divide two expressions
Composite Pattern
Types of expressions
Goal: have one function that works for both
Calculator.calculate(exp1);
Calculator.calculate(exp2);Expression exp1 = 42;
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);
Composite Pattern
Tree representation

Expression exp1 = 42;Just a Leaf containing the value
Composite Pattern
Tree representation
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);Expression exp2 = ((1 + 2) - ((3 / 4) * 5);

Composite Pattern
Tree representation
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);

Composite Pattern
Tree representation
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);

Composite Pattern
Tree representation
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);

Composite Pattern
What is it?
Model a family of classes in a tree-like hierarchy to represent the part-whole relationship
Structural design pattern that allow a group of objects to be treated like single instances of the object
- Single instance:
File - Group of objects:
Directory
In Unix operating systems, a Directory is a File!
When to use?
Factory Pattern
Factory Pattern
Suppose we have
- An
ImageViewerapplication that lets users delete photos from their mobile device - We need a separate delete
Buttonfor each OS- IOS
- Android
- We want to avoid code like this nn a constructor:
if OS == IOS then create <IOSButton>
else create <AndroidButton>- Problematic if there are more type of buttons with different conditions of initialisation


Example adapted from Fireship.io
Factory Pattern
Inside src/thrones, there is some code to model a simple chess-like game. The game has different types of characters that can move and attack in different ways
For this week, the implementation logic of the game does not matter
Instead, we focus on the creation logic of these character classes!
Code Demo
Factory Pattern
Refactor the code such that when the characters are created, they are put in a random location in a grid of length of length 5
What would be the main benefit of using the factory pattern?
Code Demo
The business logic of constructing characters are completely hidden from client code
- They don't need to know about the grid of length 5
- They won't know even if we change the logic (i.e. grid of length 7)
Factory Pattern
- The factory does not need to be a completely separate class
- It could be a method in an abstract class which is then over-ridden by subclasses
- It is a pattern I find relatively difficult to teach
- There is a more "advanced" version of this pattern known as the abstract factory pattern
Factory Pattern
Why use it?
What is it?
Creational design pattern that lets you abstract (hide away) the business logic (i.e. checking the OS) of constructing an object
- Reduce coupling between client code and the specific classes being instantiated
- Users don't need to know how objects are created (they simply call a method from the factory)
- Follows the single responsibility principle
- Construction of every class in the family is handled at the same place
Decorator Pattern
Decorator Pattern
Suppose we have:
- A
Humanclass that can wear clothes to boost their fashion, warmth, etc - What OOP technique can we use to represent a
Humanwearing a sweater and coat?- Inheritance: create a new sub class
SweaterCoatHumanthat extends fromHuman
- Inheritance: create a new sub class
- What if a
Humanobject can wear a combination of any clothes or change their clothes at runtime?- Can't keep making subclasses... there are infinite combinations
- Inheritance is also static so we can't add clothing to a
Humanat runtime


Decorator Pattern

UML Diagram
-
Humanis what we want to decorate - The
HumanDecoratorimplements theHumaninterface and composesHuman - Decorated
Humanand a regularHumancan be treated same by client code - Implementation of
doSomethingin the decorator classes can now "enhance" the implementation of its wrappee

Decorator Pattern
Code Demo

Let's try implementing this in Java!
Decorator Pattern
What is it?

Structural design pattern that allows the behaviour of objects to be enhanced at runtime by placing them into special wrapper objects
What problem does it solve?
Enhance the behaviour of an object at runtime without violating open-closed principle
The End
COMP2511 Tutorial 7
By matthewliu-2
COMP2511 Tutorial 7
- 114