🍄 🐘 🦆 🐟 🐍 🦋 🐸 🦖🌳 🌸 🍓 🥒 🍎 🥜🌿
We make mistakes
We identify our mistakes
We fix our mistakes
We learn to prevent them in future
We Evolve Design Patterns
Lessons Learned!
Maybe...
Then we iterate!
"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"
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", hammer);
Box techBox = new Box("Tech Box", phone, headphones);
Box chargerBox = new Box("Charger Box", charger);
Box electronicsBox = new Box("Electronics Box", techBox, chargerBox);
Box orderBox = new Box("Shipment Box", toolBox, electronicsBox, receipt);
orderBox.printContent();
orderBox.printTotalPrice();
private void addButtonPanel() {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4,2));
buttonPanel.add(new JButton(new AddCodeAction()));
buttonPanel.add(new JButton(new RemoveCodeAction()));
buttonPanel.add(new JButton(new ArmAction()));
buttonPanel.add(new JButton(new DisarmAction()));
buttonPanel.add(new JButton(new AddSensorAction()));
buttonPanel.add(new JButton(new ClearLogAction()));
buttonPanel.add(new JButton(new PrintLogAction()));
buttonPanel.add(createPrintCombo());
controlPanel.add(buttonPanel, BorderLayout.WEST);
}
Alarm Controller UI
Swing Containers/Components form a Component hierarchy, and Swing can paint them (and various other actions) recursively!
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
Image source: https://refactoring.guru/
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?
See part 3 answer!