More on Design Patterns 10-18-2013 - Clarkson...
Transcript of More on Design Patterns 10-18-2013 - Clarkson...
![Page 1: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/1.jpg)
More on Design Patterns
10-18-2013
![Page 2: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/2.jpg)
ICPC
Northeast North America Preliminary sponsored by the
Clarkson Student Chapter of the ACM
Saturday, October 19, 2013
11:00 am – 5:00 pm
Applied CS Labs Science Center 334-336
![Page 3: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/3.jpg)
Design Patterns
Strategy
Iterator
Observer
Composite
Handout: form project teams
Project#1: due Wed, October 30th
![Page 4: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/4.jpg)
Strategy: defines a family of algorithms, encapsulates
each one in a class, and makes them interchangeable via an interface. Strategy lets the algorithm (or behavior) vary independently from clients that use it.
![Page 5: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/5.jpg)
Iterator: provides a way to access the elements of an
aggregate object sequentially without exposing its underlying representation.
![Page 6: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/6.jpg)
Observer: defines a one-to-many dependency
between objects so that when one object changes state, all of its dependents are notified and updated automatically
![Page 7: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/7.jpg)
Composite: allows you to compose objects into tree
structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
![Page 8: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/8.jpg)
The client uses the
Component interface to
manipulate the objects
in the composition
![Page 9: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/9.jpg)
The Component
defines an interface
for all objects in the
composition (both
composite and leaf
nodes); default
behavior may be
implemented here
![Page 10: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/10.jpg)
A Leaf defines the
behavior for the elements
in the composition. It
does this by implementing
the operations the
Composite supports
A Leaf has no children
Some methods in
Component don’t make
sense for a leaf
![Page 11: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/11.jpg)
The Composite’s role is
to define behavior of
the components having
children and to store
child components
Composite also
implements the Leaf-
related operations
some methods in
Component don’t make
sense for Composite
![Page 12: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/12.jpg)
![Page 13: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/13.jpg)
Waitress <<abstract>>
MenuComponent
getName()
getDescription()
getPrice()
isVegetarian()
print()
add(Component)
remove(Component)
getChild(int)
MenuItem
getName()
getDescription()
getPrice()
isVegetarian()
print()
Menu
menuComponents
getName()
getDescription()
print()
add(Component)
remove(Component)
getChild(int)
![Page 14: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/14.jpg)
public abstract class MenuComponent { public void add(MenuComponent menuComponent) { throw new UnsupportedOperationException(); } public void remove(MenuComponent menuComponent) { throw new UnsupportedOperationException(); } public String getName( ) { throw new UnsupportedOperationException(); } public String getDescription( ){ throw new UnsupportedOperationException(); } public double getPrice( ){ throw new UnsupportedOperationException(); } public boolean isVegetarian( ){ throw new UnsupportedOperationException(); } public void print( ){ throw new UnsupportedOperationException(); } }
![Page 15: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/15.jpg)
public class MenuItem extends MenuComponent { private String name; private String description; private boolean vegetarian; private double price; public MenuItem(String n, String d, boolean v, double p ) { name = n; description = d; vegetarian = v; price = p; } public String getName( ){ return name; } public double getPrice( ){ return price; } public boolean isVegetarian( ){ return vegetarian; } public void print( ){ // code to print a menu item } }
![Page 16: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/16.jpg)
public class Menu extends MenuComponent { private List menuComponents = new ArrayList(); private String name; private String description; public Menu(String n, String d) { name = n; description = d; } public void add(MenuComponent menuComponent) { menuComponents.add(menuComponent); } public void remove(MenuComponent menuComponent) { menuComponents.remove(MenuComponent); } public MenuComponent getChild(int i) { return (MenuComponent) menuComponents.get(i); } public String getName( ){ return name; } public void print( ){ // code to print an entire menu } }
![Page 17: More on Design Patterns 10-18-2013 - Clarkson Universitypeople.clarkson.edu/~jsearlem/cs242/fa13/lectures/23.composite.pdfStrategy: defines a family of algorithms, encapsulates each](https://reader034.fdocuments.in/reader034/viewer/2022050206/5f58f80040460102342f5e05/html5/thumbnails/17.jpg)
public class Menu extends MenuComponent { private List menuComponents = new ArrayList(); private String name; private String description;
public Menu(String n, String d) { … } public void add(MenuComponent menuComponent) { … } public void remove(MenuComponent menuComponent) { … } public MenuComponent getChild(int i) { … } public String getName( ){ … }
public void print( ) { System.out.println( ‘’\n’’ + getName() ); System.out.println( ‘’, ‘’getDescription() ); System.out.println( ‘’-------------------------’’); Iterator it = menuComponents.iterator(); while (it.hasNext() ) { MenuComponent menuComponent = (MenuComponent) iterator.next( ); menuComponent.print( ); } } }