A pipeline contains:
What is a stream?
A sequence of elements which runs through the pipeline that transforms from intermediate operations.
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
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
Currently, the code uses switch statements to handle each of the different cases.
How does the code violate the open/closed principle?
Currently, the code uses switch statements to handle each of the different cases.
How does the code violate the open/closed principle?
switch
statement.
How does this make the code brittle?
Currently, the code uses switch statements to handle each of the different cases.
How does the code violate the open/closed principle?
switch
statement.
How does this make the code brittle?
Let's refactor it!
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.
Find out more at Strategy (refactoring.guru)
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
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
Model the system in Java!
Which design pattern should we use for each of the following?
Represent tasks with subtasks, all supporting markComplete()
Which design pattern should we use for each of the following?
Represent tasks with subtasks, all supporting markComplete()
Composite Pattern
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
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
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
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