25T2 Week 4
Friday 9AM - 12PM (F09A)
Slides by Christian Tolentino (z5420628)
"Principle of least knowledge"
What is it?
Law of Demeter (aka principle of least knowledge) is a design guideline that says that an object should assume as little as possible knowledge about the structures or properties of other objects.
It aims to achieve loose coupling in code.
What does it actually mean?
A method in an object should only invoke methods of:
E.g., don't do this
o.get(name).get(thing).remove(node)This is not a hard-and-fast rule. Sometimes it is unavoidable.
Law of Demeter
In the unsw.training package there is some skeleton code for a training system.
In the TrainingSystem class there is a method to book a seminar for an employee given the dates on which they are available. This method violates the principle of least knowledge (Law of Demeter).
In the unsw.training package there is some skeleton code for a training system.
In the TrainingSystem class there is a method to book a seminar for an employee given the dates on which they are available. This method violates the principle of least knowledge (Law of Demeter).
/**
* An online seminar is a video that can be viewed at any time by employees. A
* record is kept of which employees have watched the seminar.
*/
public class OnlineSeminar extends Seminar {
private String videoURL;
private List<String> watched;
}
/**
* An in person all day seminar with a maximum of 10 attendees.
*/
public class Seminar {
private LocalDate start;
private List<String> attendees;
public LocalDate getStart() {
return start;
}
public List<String> getAttendees() {
return attendees;
}
}
public class TrainingSystem {
private List<Trainer> trainers;
public LocalDate bookTraining(String employee, List<LocalDate> availability) {
for (Trainer trainer : trainers) {
for (Seminar seminar : trainer.getSeminars()) {
for (LocalDate available : availability) {
if (seminar.getStart().equals(available) &&
seminar.getAttendees().size() < 10) {
seminar.getAttendees().add(employee);
return available;
}
}
}
}
return null;
}
}
/**
* A trainer that runs in person seminars.
*/
public class Trainer {
private String name;
private String room;
private List<Seminar> seminars;
public List<Seminar> getSeminars() {
return seminars;
}
}
How and why does it violate this principle?
What other properties of this design are not desirable?
/**
* An online seminar is a video that can be viewed at any time by employees. A
* record is kept of which employees have watched the seminar.
*/
public class OnlineSeminar extends Seminar {
private String videoURL;
private List<String> watched;
}
/**
* An in person all day seminar with a maximum of 10 attendees.
*/
public class Seminar {
private LocalDate start;
private List<String> attendees;
public LocalDate getStart() {
return start;
}
/**
* Try to book this seminar if it occurs on one of the available days and
* isn't already full
* @param employee
* @param availability
* @return The date of the seminar if booking was successful, null otherwise
*/
public LocalDate book(String employee, List<LocalDate> availability) {
for (LocalDate available : availability) {
if (start.equals(available) &&
attendees.size() < 10) {
attendees.add(employee);
return available;
}
}
return null;
}
}
public class TrainingSystem {
public List<Trainer> trainers;
/**
* Try to booking training for an employee, given their availability.
*
* @param employee
* @param availability
* @return The date of their seminar if booking was successful, null there
* are no empty slots in seminars on the day they are available.
*/
public LocalDate bookTraining(String employee, List<LocalDate> availability) {
for (Trainer trainer : trainers) {
LocalDate booked = trainer.book(employee, availability);
if (booked != null)
return booked;
}
return null;
}
}
/**
* A trainer that runs in person seminars.
*/
public class Trainer {
private String name;
private String room;
private List<Seminar> seminars;
public List<Seminar> getSeminars() {
return seminars;
}
/**
* Try to book one of this trainer's seminars.
* @param employee
* @param availability
* @return The date of the seminar if booking was successful, null if the
* trainer has no free slots in seminars on the available days.
*/
public LocalDate book(String employee, List<LocalDate> availability) {
for (Seminar seminar : seminars) {
LocalDate booked = seminar.book(employee, availability);
if (booked != null)
return booked;
}
return null;
}
}TrainingSystem no longer has knowledge of SeminarWhat is it?
Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application.
*inheritance arrows are the other way around
Solve the problem without inheritance
Design principle: Favour composition over inheritance
If you favour composition over inheritance, your software will be more flexible, easier to maintain, extend.
/**
* An online seminar is a video that can be viewed at any time by employees. A
* record is kept of which employees have watched the seminar.
*/
public class OnlineSeminar extends Seminar {
private String videoURL;
private List<String> watched;
}
/**
* An in person all day seminar with a maximum of 10 attendees.
*/
public class Seminar {
private LocalDate start;
private List<String> attendees;
public LocalDate getStart() {
return start;
}
public List<String> getAttendees() {
return attendees;
}
}
Where does OnlineSeminar violate LSP?
OnlineSeminar doesn't require a list of attendees
What type of design pattern is composite?
Structural
Structural design pattern are patterns that ease the design by identifying a simple way to realize relationships among entities.
They explain how to assemble objects and classes into large structures, while keeping structures flexible and efficient
Composite pattern is useful for aggregating different objects/data. The aim is to be able to manipulate a single instance of an object just as you would manipulate a group of them.
Tree like structure of objects
Calculator.java - Composite pattern
Inside src/calculator, use the Composite Pattern to write a simple calculator that evaluates an expression. Your calculator should be able to:
{1 + [2 * (4 + 3)]}
Expression 1
Expression 2
Expression 3
What type of design pattern?
Creational
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
Factory method provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
Reduces coupling and shotgun surgery, as all classes are created using the same method.
Thrones.java - Factory Pattern
Inside src/thrones, there is some code to model a simple chess-like game. In this game different types of characters move around on a grid fighting each other. When one character moves into the square occupied by another they attack that character and inflict damage based on random chance. There are four types of characters:
We want to refactor the code so that when the characters are created, they are put in a random location in a grid of length 5.
main method of Game.java to use these factories.