24T3 Week 9
Wednesday 3PM - 6PM (W15A)
Thursday 11AM - 2PM (H11C)
Slides by Christian Tolentino (z5420628)
In groups, determine a possible pattern that could be used to solve each of the following problems:
Then pick one and start to think about potential entities and draw up a rough UML diagram.
In groups, determine a possible pattern that could be used to solve each of the following problems:
Then pick one and start to think about potential entities and draw up a rough UML diagram.
In groups, discuss the following examples. Identify the code smells and any underlying design problems associated with them.
a) Mark, Bill and Jeff are working on a PetShop application. The PetShop has functionality to feed, clean and exercise different types of animals. Mark notices that each time he adds a new species of animal to his system, he also has to rewrite all the methods in the PetShop so it can take care of the new animal.
Text
public class MathLibrary {
List<Book> books;
int sumTitles() {
int total = 0
for (Book b : books) {
total += b.title.titleLength;
}
return total;
}
}
public class Book {
Title title; // Our system just models books as titles (content doesn't matter)
}
public class Title {
int titleLength;
int getTitleLength() {
return titleLength;
}
void setTitleLength(int tL) {
titleLength = tL;
}
}
What type of pattern?
Behavioural
Allows you to separate algorithms from the objects on which they operate. By doing so, it enables you to add new operations to existing object structures without modifying those structures (avoid LSP violation). This pattern is particularly useful when you need to perform various unrelated operations (decouples operations) across a complex object structure.
Computer.java
In this scenario we have Computers, Keyboards and Mouses which all are of type ComputerComponent. We want to be able to 'visit' different types of Computer components by logging the following messages:
Looking at computer Corelli with memory 500 GB.
Looking at keyboard Mechanical keyboard which has 36 keys.
Looking at mouse Bluetooth mouse.
In particular though, anyone which is visiting a Computer must be validated prior to being able to visit.
Extend/modify the starter code to use the Visitor Pattern to allow different computer components to be visited.
Visitor Pattern
If anyone has any questions about one of them we can discuss together :)