COMP2511 Week 7
Today
- Admin Stuff
- Composite Pattern
- Factory Pattern
Admin Stuff
- Congrats on finishing assignment 1!
- Marks and feedback will be coming soon!
- Assignment 2 should be underway!
- Incorporate feedback from assignment 1
Assignment 2 Tips
Assignment 2 Tips
Blog Posts
- Follow the template in the spec and attach the merge request to the correct task
- 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
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
Design Smells
- If you think you have found a smell, make sure to double check here: Code Smells (refactoring.guru)
Assignment 2 Tips
Task 2
- Must complete task a)
- Choose N tasks from b-f for N members in the group
- Earlier subtasks are easy but doing them will not result in a high mark
Composite Pattern
Composite Pattern
What is it?
Structural design pattern that allow objects to be composed into a tree structure such that individual objects and composites of such objects can be treated in the same way.
When to use?
Model a group of classes that forms a hierarchy
Composite Pattern

In Unix based operating systems, directories are just files that can contain other files
Composite Pattern
UML Diagram

Leaf
andComposite
both implement the same interface- Instances of
Leaf
andComposite
can be treated as the same - A
Composite
can hold more than oneLeaf
-
Produces a tree-like structure that leans itself very well towards recursion
Composite Pattern
Use the composite pattern to design a simple calculator that can be used to evaluate an arithmetic expression.
Your calculator should be able to:
- Add two expressions
- Subtract two expressions
- Multiply two expressions
- Divide two expressions
Composite Pattern
Tree representation
exp1 = 42
exp2 = ((1 + 2) - ((3 / 4) * (5))


Composite Pattern
UML Diagram

Factory Pattern
Factory Pattern
Why use it?
- Reduce coupling between client code (creation logic) and the specific classes being instantiated
- Single responsibility principle: construction of every class in the family is handled at the same place
- Open-Closed principle: introducing new classes into the family simply requires a new factory subclass
What is it?
Creational design pattern that lets you define an interface for creating objects of a family in a superclass but allows subclasses to alter the type of object that will be created.
Factory Pattern

Product
declares a common interface to all objects creatable by theCreator
and its subclasses- Concrete products: different implementations of
Product
- Creator class declares the factory method that can be overriden
- Concrete Creators override the
Creator
to construct new types of products
Factory Pattern

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.
Finding Nemo
Design Pattern Edition
In groups, determine a possible design pattern that could used to solve the following problems
Finding Patterns
Finding Patterns
1. Sorting collections of records in different orders
Finding Patterns
1. Sorting collections of records in different orders
- Strategy pattern. In Java, this could be providing a
Comparator
toCollections.sort
Finding Patterns
1. Sorting collections of records in different orders
- Strategy pattern. In Java, this could be providing a
Comparator
toCollections.sort
2. Modelling shapes and the ability to combine shapes
Finding Patterns
1. Sorting collections of records in different orders
- Strategy pattern. In Java, this could be providing a
Comparator
toCollections.sort
2. Modelling shapes and the ability to combine shapes
- Composite pattern. Regular shapes can form the leaves and compound shapes can be formed by combining shapes.
Finding Patterns
3. Updating a UI component when the state of a program changes.
Finding Patterns
3. Updating a UI component when the state of a program changes.
- Observer pattern. The state of the program is the subject and the UI is the observer.
The End
COMP2511 Tutorial 7
By Matthew Liu
COMP2511 Tutorial 7
- 118