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

Post on 19-Jul-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

More on Design Patterns

10-18-2013

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

Design Patterns

Strategy

Iterator

Observer

Composite

Handout: form project teams

Project#1: due Wed, October 30th

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.

Iterator: provides a way to access the elements of an

aggregate object sequentially without exposing its underlying representation.

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

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.

The client uses the

Component interface to

manipulate the objects

in the composition

The Component

defines an interface

for all objects in the

composition (both

composite and leaf

nodes); default

behavior may be

implemented here

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

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

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)

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

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

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

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

top related