140 likes | 248 Views
Iterator and Composite Design Patterns. Tuc Goodwin. Agenda. Scenario Iterator Pattern Iterator Participants Composite Pattern Composite Pattern Participants. The Book we are Using. Why use an Iterator Pattern?.
E N D
Iterator and Composite Design Patterns Tuc Goodwin
Agenda • Scenario • Iterator Pattern • Iterator Participants • Composite Pattern • Composite Pattern Participants
Why use an Iterator Pattern? The Iterator pattern allows you to move through a list or collection of data using a standard interface without having to know the details of the internal representation of the data.
Iterator Pattern definition Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation
Iterator Participants The classes and/or objects participating in this pattern are: • Iterator(AbstractIterator) • defines an interface for accessing and traversing elements. • ConcreteIterator(Iterator) • implements the Iterator interface. • keeps track of the current position in the traversal of the aggregate. • Aggregate(AbstractCollection) • defines an interface for creating an Iterator object • ConcreteAggregate(Collection) • implements the Iterator creation interface to return an instance of the proper ConcreteIterator
Lou’s Diner & Mel’s Diner • Lou implemented his menus as an ArrayList • Mel implemented his menus as an Array • We need to combine the two and display all of the menu items. • Do we really want to implement this loop twice? Once for an ArrayList and once for an Array?
DEMO BEFORE PATTERNS / Interfaces
Demo II After Patterns / Interfaces
Composite Design definition Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Composite Participants The classes and/or objects participating in this pattern are: • Component (DrawingElement) • declares the interface for objects in the composition. • implements default behavior for the interface common to all classes, as appropriate. • declares an interface for accessing and managing its child components. • (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate. • Leaf (PrimitiveElement) • represents leaf objects in the composition. A leaf has no children. • defines behavior for primitive objects in the composition. • Composite (CompositeElement) • defines behavior for components having children. • stores child components. • implements child-related operations in the Component interface. • Client(CompositeApp) • manipulates objects in the composition through the Component interface.
Next Time .NET Development ------------------------------------ January 10 Beginning ASP.NET: DataGrids and Gridviews February 14 Beginning ASP.NET – Part 3 March 14 Beginning ASP.NET – Part 4 April 11 LINQ Patterns------------------------------------ January 10 State Pattern February 14 Proxy Pattern March 14 Compound Patterns April 11 Better Living with Patterns