Tutorial 5
give
What's next?
Design patterns will be the primary focus from now on!
Suppose some of the subclasses implement the fly method in the same way
What changes do we need to make to implement this feature without duplicating code?
Suppose only some of the subclasses implement the fly method in the same way.
Attempt:
Since some subclasses share the same implementation, let's introduce two new abstract classes in between Duck and our existing subclasses to maximise code re-use.
What changes do we need to make to implement this feature?
Attempt: introduce two new abstract classes
Requirements update:
GrassDuck and FireDuck share the same implementation for Quack
Requirement update: Suppose now given an instance of any type of Duck, we need to be able to change the way it flies at runtime
How would we implement this feature?
We currently don't have any tools that can achieve this :(
Solution: extract each implementation of flying into a separate class and let the Duck class compose it
What is it?
Introduce new behaviours without violating the Open-Closed principle
Allow behaviour to be re-used without duplication
Why use it?
Behavioural Design pattern that lets you define a family of methods that are interchangeable at runtime
How does the code violate the open/closed principle?
switch statement.Let's refactor it!
Currently, the code uses switch statements to handle each of the different cases.
public interface ChargingStrategy {
// Calculate the cost of their order.
public double cost(List<Meal> order, boolean isMember);
// Modifying factors of charges for customers.
public double standardChargeModifier();
}Find out more at Strategy (refactoring.guru)
Suppose we have:
TicketServer that will periodically release tickets for a concert
Clients listening to the TicketServer, buying tickets as soon as they become available It's a subscription system!
Why do we need to use it?
What is it?
Behavioural design pattern to model a push-based subscription mechanism between observers and a subject
UML Diagram
Model the system in Java!
public class Producer implements Subject {
private List<Observer> subscribers = new ArrayList<>();
private String name;
public Producer(String name) {
this.name = name;
}
@Override
public void addSubscriber(Observer subscriber) {
this.subscribers.add(subscriber);
}
@Override
public void upload(Video video) {
for (Observer subscriber : this.subscribers) {
subscriber.alertNewVideo(video);
}
}
}
public interface Subject {
public void upload(Video video);
public void addSubscriber(Observer subscriber);
}
public interface Observer {
public void subscribe(Subject producer);
public void alertNewVideo(Video video);
}
Implementation of interfaces and the Producer
public class User implements Observer {
private String name;
public User(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
@Override
public void subscribe(Subject producer) {
producer.addSubscriber(this);
}
@Override
public void alertNewVideo(Video video) {
System.out.println("Video: " + video.getName() + "was posted");
}
}
Implementation of the User
Suppose we have:
VideoPlayer with a play and lock buttonIt's a state machine!
What is it?
if / switch statements (i.e. if state is a do b, else if state is c do d, etc)Behavioural design pattern to model a state machine where the behaviour of an object changes depending on its internal state
Transition Diagram
UML Diagram
Find out more at State (refactoring.guru)
"Composition over inheritance"