Slides designed by Felix Grund, based on Paul Carter’s slides
We make mistakes
We identify our mistakes
We learn from our mistakes
We prevent mistakes
We Evolve Design Patterns
Lessons Learned!
"General, reusable solution to a commonly occurring problem within a given context in software design"
"Guidelines for software design that help you avoid mistakes that others repeatedly made in the past"
Other
definition:
Product receipt = new Product("Receipt", 0.0);
Product hammer = new Product("Hammer", 20.00);
Product phone = new Product("Phone", 500.00);
Product headphones = new Product("Headphones", 50.00);
Product charger = new Product("Charger", 35.00);
Box toolBox = new Box("Tool Box");
toolBox.addItem(hammer);
Box techBox = new Box("Tech Box");
techBox.addItem(phone);
techBox.addItem(headphones);
Box chargerBox = new Box("Charger Box");
chargerBox.addItem(charger);
Box electronicsBox = new Box("Electronics Box");
electronicsBox.addItem(techBox);
electronicsBox.addItem(chargerBox);
Box orderBox = new Box("Order Box");
orderBox.addItem(toolBox);
orderBox.addItem(electronicsBox);
orderBox.addItem(receipt);
double price = orderBox.getPrice();
System.out.println("TOTAL PRICE: " + price);
public abstract class Item {
protected String name;
public Item(String name) {
this.name = name;
}
abstract double getPrice();
}
Component
public class Box extends Item {
private List<Item> items;
public Box(String name) {
super(name);
this.items = new ArrayList<>();
}
public void addItem(Item item) {
this.items.add(item);
}
public double getPrice() {
double sum = 0;
for (Item item : items) {
sum += item.getPrice();
}
return sum;
}
}
Composite
public class Product extends Item {
private double price;
public Product(String name, double price) {
super(name);
this.price = price;
}
public double getPrice() {
return price;
}
}
Leaf
I have a tree structure and I don't know all the types of objects in the tree.
I want to run some operations recursively through my tree without knowing the exact types in my tree.
Composite node: arbitrarily many children
Leaf node:
no children
What hierarchical relationship do these classes have to one another?
We identify Offshoot as the component, Branch as the composite, and Leaf as the the leaf
Which of the following fields would exist in Offshoot, Branch and Leaf?
The aggregation from Branch to Offshoot would be implemented by adding a field of type List<Offshoot> to the Branch class.
Imagine that you want to change the color of all the leaves to red. You want to do this by adding a changeColor(Color) method. Where might the method be specified but not implemented?
The changeColor(Color) method is one that we would want to call on both a Leaf (to change the color of that individual leaf) and on a Branch (to change the color of all the leaves attached to that branch). This is therefore common behaviour that we want all offshoots (whether they be a branch or a leaf) to support. The method is therefore specified in the Offshoot class.
Where would the changeColor method be implemented?
tree changing color to RED
--b3 changing color to RED
----b2 changing color to RED
------l3 changing color to RED
----l4 changing color to RED
--b1 changing color to RED
----l1 changing color to RED
----l2 changing color to RED
Let's add indentation:
Folder
File
FileSystemResource
addChild(...)
print()
Folder
File
FileSystemResource
addChild(...)
print()