Download presentation
Presentation is loading. Please wait.
Published byVeronica Barrett Modified over 9 years ago
2
Systems Requirements SE 3821 Design? Algorithms CS 2852
4
SimUDuck v5 Another approach: Isolate behaviors that vary Encapsulate these as attributes Benefits: Eliminates implementation inheritance Prevents class explosion
6
Collections.sort() Why Comparator<>? How to use?
7
Collections.sort() Why Comparator<>? How to use? Benefit: comparison strategy is decoupled from the sort() method
8
MethodBehavior InsertionSort ? TreeSort ?
9
MethodBehavior InsertionSortO(n 2 ), easy to verify TreeSortO(n log n), but not guaranteed MergeSortO(n log n), guaranteed QuickSortO(n log n) w/ random pivot
10
Elements: Context: class that encapsulates, uses specific behavior Strategy: interface that defines the behavior ConcreteStrategy: implements a specific behavior
15
// create some behaviors SwimBehavior csb = new CircularSwimming(); QuackBehavior sqb = new StandardQuacking(); SwimBehavior rsb = new RandomFloating(); // daffy has circular swimming, std quacking Waterfowl daffy = new Duck(“daffy”, csb, sqb); // donald has random floating, std quacking Waterfowl donald = new Duck(“donald”, rsb, sqb); daffy.swim(); donald.quack();
17
public abstract class Waterfowl { private SwimBehavior swimBehavior; private QuackBehavior quackBehavior; private String name; // constructor public void Waterfowl(String name, SwimBehavior sb, QuackBehavior qb) { this.name = name; swimBehavior = sb; quackBehavior = qb; } // centralize implementation of behaviors in top-level classes // if possible; avoid duplication of behavior in subclasses. // Note we can make this method final to prevent subclasses from overriding it! public void swim() { swimBehavior.swim(); // invoke the specific behavior }...
18
Strategy is a Behavioral Design Pattern
28
Access Levels ModifierClassPackageSubclassWorld publicYYYY protected???? /*package*/???? privateYNNN
29
Access Levels ModifierClassPackageSubclassWorld publicYYYY protectedYYYN /*package*/YYNN privateYNNN
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.