"Principle of least knowledge"
What is it?
A method in an object should only call methods of:
Only talk to your immediate friends
Avoid doing this! (You will lose marks)
o.get(name).get(thing).remove(node)
Purpose?
Achieve loose coupling in code
In the unsw.training
package there is some 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).
Where and how is the Law of Demeter being violated?
TrainingSystem
is getting instances of Seminar
from instances of Trainer
and calling its methods
Interacting with classes beyond its friends
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;
}
What other properties of this design are not desirable?
What other properties of this design are not desirable?
TrainingSystem
is needlessly tightly coupled with Trainer
and Seminar
TrainingSystem
has low cohesion as it relies on classes that are not its closest friends to achieve its purposeWhat other properties of this design are not desirable?
TrainingSystem
is needlessly tightly coupled with Trainer
and Seminar
TrainingSystem
has low cohesion as it relies on classes that are not its closest friends to achieve its purposeLet's refactor it together!
What is it?
Design principle that states instances of the superclass must be replaceable by instances of the subclasses without breaking changes.
How does OnlineSeminar
violate LSP?
/**
* 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;
}
How does OnlineSeminar
violate LSP?
/**
* 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;
}
Does not have a list of attendees, so clients won't be able to use it in the same way as Seminar
.
i.e making a booking
What it is?
Java's way of enabling functional programming
Common stream methods:
forEach, filter, map, reduce
Why bother?
What is it?
Software design approach that specifies how a component
should interact with other components
A contract should address the following 3 conditions:
public class Calculator {
public static Double add(Double a, Double b) {
return a + b;
}
public static Double divide(Double a, Double b) {
return a / b;
}
public static Double sin(Double angle) {
return Math.sin(angle);
}
public static Double tan(Double angle) {
return Math.tan(angle);
}
}
Specify a contract for each function
i.e. preconditions & postconditions
public class Calculator {
/**
* @preconditions a, b != null
* @postconditions a + b
*/
public static Double add(Double a, Double b) {
return a + b;
}
/**
* @preconditions a, b != null, b != 0
* @postconditions a / b
*/
public static Double divide(Double a, Double b) {
return a / b;
}
/**
* @preconditions angle != null
* @postconditions sin(angle)
*/
public static Double sin(Double angle) {
return Math.sin(angle);
}
/**
* @preconditions angle != null, angle != Math.PI / 2
* @postconditions tan(angle)
*/
public static Double tan(Double angle) {
return Math.tan(angle);
}
}