Tutorial 7
Blog Posts
Task 1 Part f)
Code Smells
If you think you have found a code smell, make sure to double check here: Code Smells (refactoring.guru)
Task 2
Suppose we have
File can store data like text or images etcDirectory can store both files and other directoriesSuppose we have
File can store data like text or images etcDirectory can store both files and other directoriesFileSystem
This is a tree-like structure!
UML Diagram
File (Leaf) and Directory (Composite) both implement a common interface
Directory can store both files and directories (i.e. add and remove)
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:
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);
Tree representation
Expression exp1 = 42;Just a Leaf containing the value
Tree representation
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);Expression exp2 = ((1 + 2) - ((3 / 4) * 5);
Tree representation
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);
Tree representation
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);
Tree representation
Expression exp2 = ((1 + 2) - ((3 / 4) * 5);
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
File
Directory
In Unix operating systems, a Directory is a File!
When to use?
Suppose we have
ImageViewer application that lets users delete photos from their mobile deviceButton for each OS
if OS == IOS then create <IOSButton>
else create <AndroidButton>Example adapted from Fireship.io
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
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
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
Suppose we have:
Human class that can wear clothes to boost their fashion, warmth, etcHuman wearing a sweater and coat?
SweaterCoatHuman that extends from Human
Human object can wear a combination of any clothes or change their clothes at runtime?
Human at runtimeUML Diagram
Human is what we want to decorate HumanDecorator implements the Human interface and composes Human
Human and a regular Human can be treated same by client codedoSomething in the decorator classes can now "enhance" the implementation of its wrappee Code Demo
Let's try implementing this in Java!
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