H12A
Key Characteristics
private SortStrategy strategy;
BubbleSort
BubbleSort
MergeSort
QuickSort
HeapSort
Strategies
DataSorter
<<SortStrategy>> {sort()}
private SortStrategy strategy;
strategy.sort();
void setStrategy (SortStrategy);
When to Use
Key Characteristics:
When to Use
While both patterns involve changing an object’s behavior, the key difference lies in their intent:
Give an example of a real-life subclass that would violate the given contract:
List<String> singers = List.of("Taylor Swift", "Jimi Hendrix", "Beyoncé", "Tyler the Creator", "Adele");
List<String> lineup = singers.stream()
.filter(s -> hasGrammy(s)) // 🎤 Filter Grammy winners
.map(s -> styleOutfit(s)) // 💄 Give stage outfit
.toList(); // 🚌 Collect them for their performance
Note: by real-life subclass, I mean some kind of creature that is a type of Bird.
Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically.