more on design patterns 10-18-2013 - clarkson...

17
More on Design Patterns 10-18-2013

Upload: others

Post on 19-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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
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

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

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

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

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

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( ); } } }