COMP2511 Week 5
Agenda
- Admin Stuff
- Streams and Lambda
- Strategy Pattern
- Observer Pattern
Admin Stuff
- Assignment 1 is due this coming Friday
- Submissions are done via Gitlab, all you need to do is push to your main branch
- Pipelines slow down close to deadline so avoid making last minute pushes!
- Test your linting (easy marks) and do quick dryrun for sanity checks
- Lab05 is due Week 7, Monday at 10am
Streams and Lambda
Streams and Lambda
A pipeline contains:
- A source (i.e. collection, array, generator function)
- Intermediate operations which transforms a stream into another stream
What is a stream?
A sequence of elements which runs through the pipeline that transforms from intermediate operations.

Streams and Lambda
map()
: Applies a given function to elements in the stream
filter()
: Selects specific elements as per the Predicate (boolean function)
sorted()
: Used to sort the stream
Intermediate Operations
collect()
: Collects the result of a stream into a list, set, etc.
forEach()
: Iterate through every element of the stream
reduce()
: Reduce the elements of a stream into a single value
Terminal Operations
Strategy Pattern
Strategy Pattern
What is it?
Behavioural design pattern that lets you define a family of methods that are interchangeable at runtime
What problem does it solve?
Introducing new behaviours without modifying existing code, adhering to the Open-Closed Principle. Avoids conditional statements to select algorithms.
When should we use this pattern?
When we need multiple ways of achieving the same task

Different transport behaviours (strategies) that can be interchangeable
Strategy Pattern

Strategy Pattern
Currently, the code uses switch statements to handle each of the different cases.
How does the code violate the open/closed principle?
Strategy Pattern
Currently, the code uses switch statements to handle each of the different cases.
How does the code violate the open/closed principle?
- Not closed for modification / open for extension.
- Each new case requires a new
switch
statement.
How does this make the code brittle?
Strategy Pattern
Currently, the code uses switch statements to handle each of the different cases.
How does the code violate the open/closed principle?
- Not closed for modification / open for extension.
- Each new case requires a new
switch
statement.
How does this make the code brittle?
- Makes code more brittle as new requirements cause things to break/difficult to extend functionality
Let's refactor it!

Structure

Now that we have refactored, how about we want to add a new pricing strategy?
Notice how after refactoring, it is a lot easier to add additional functionality and we are no longer breaking the Open-Closed Principle.
Strategy Pattern
Find out more at Strategy (refactoring.guru)
Observer Pattern
Observer Pattern
What is it?
Behavioural pattern which defines a subscription mechanism that notifies observers about any event that happens to the subject that they are observing
What problem does it solve?
Modelling a n-to-many relationship may be difficult. If
not done well, it typically leads to tight coupling
When should we use this pattern?
When changing the state of one object requires changing other objects in turn
Observer Pattern
Subject
Maintains a list of observers and notifies them of state changes. During this notification, they are passing data (push/pull)
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers(data); // Calls update() of observers
Observer
Register (and unregister) themselves on a subject and to update their state when they are notified.
// Two possible options for update()
public void update(Subject obj); // PULL data from subject
public void update(data); // PUSH data to observers
Observer Pattern
Model the system in Java!


Structure
Mini Quiz
Which design pattern should we use for each of the following?
Represent tasks with subtasks, all supporting markComplete()
Mini Quiz
Which design pattern should we use for each of the following?
Represent tasks with subtasks, all supporting markComplete()
Composite Pattern
Mini Quiz
Which design pattern should we use for each of the following?
Represent tasks with subtasks, all supporting markComplete()
Composite Pattern
In a data analysis tool, switch between different sorting algorithms depending on the dataset needs
Mini Quiz
Which design pattern should we use for each of the following?
Represent tasks with subtasks, all supporting markComplete()
Composite Pattern
Strategy Pattern
In a data analysis tool, switch between different sorting algorithms depending on the dataset needs
Mini Quiz
Which design pattern should we use for each of the following?
News app (e.g., email alerts, mobile notifications) notifies when a new article is published
Represent tasks with subtasks, all supporting markComplete()
Composite Pattern
Strategy Pattern
In a data analysis tool, switch between different sorting algorithms depending on the dataset needs
Mini Quiz
Which design pattern should we use for each of the following?
In a data analysis tool, switch between different sorting algorithms depending on the dataset needs
News app (e.g., email alerts, mobile notifications) notifies when a new article is published
Represent tasks with subtasks, all supporting markComplete()
Composite Pattern
Strategy Pattern
Observer Pattern
COMP2511 Tute05
By rebeccahsu
COMP2511 Tute05
- 360