Download presentation
Presentation is loading. Please wait.
Published byFerdinand Potter Modified over 9 years ago
2
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these two implementations of lists in a uniform way? Example here uses restaurant items
3
Look at code for (book page 316) Class MenuItem Class PancakeHouseMenu Class DinerMenu
4
Now if we want to… printMenu() Print every item on the menu printBreakfastMenu() Print just breakfast items printLunchMenu() Print just lunch items printVegetarianMenu() isItemVegetarian(name)
5
Iterating through breakfast items
6
Iterating through lunch items
7
Look at MenuTestDrive… (print out)
8
Encapsulating iteration Instead of using multiple ways to iterate through the lists, define ONE interface to iterate through the lists …
9
Using iterator to get breakfast items
10
Using iterator to get lunch items
11
Meet the iterator pattern…
12
DinerMenuIterator
14
Look at… Code for DinerMenuIterator Code for PancakeHouseMenuIterator Code for Waitress
15
Some improvements… Utilizing the Java provided implementation of iterator… Look at Eclipse…
16
Iterator Pattern defined The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
18
Design Principle A class should have only one reason to change.
19
Design principle applied to Iterator pattern Iterator pattern places the task of traversal on the iterator object, not on the aggregate, which simplifies the aggregate interface and implementation, and places the responsibility where it should be.
20
Extending the Menu example with one more aggregate Cafemenu which uses Hashtable to implement the collection. Look at Eclipse.
25
Iterators and collections
27
A look at the waitress class public class Waitress { Menu pancakeHouseMenu; Menu dinerMenu; Menu cafeMenu; public Waitress(Menu pancakeHouseMenu, Menu dinerMenu, Menu cafeMenu) { this.pancakeHouseMenu = pancakeHouseMenu; this.dinerMenu = dinerMenu; this.cafeMenu = cafeMenu; } public void printMenu() { Iterator pancakeIterator = pancakeHouseMenu.createIterator(); Iterator dinerIterator = dinerMenu.createIterator(); Iterator cafeIterator = cafeMenu.createIterator(); System.out.println("MENU\n----\nBREAKFAST"); printMenu(pancakeIterator); System.out.println("\nLUNCH"); printMenu(dinerIterator); System.out.println("\nDINNER"); printMenu(cafeIterator); }
28
Removing dependence on specific menu items… public class Waitress { ArrayList menus; public Waitress(ArrayList menus) { this.menus = menus; } public void printMenu() { Iterator menuIterator = menus.iterator(); while(menuIterator.hasNext()) { Menu menu = (Menu)menuIterator.next(); printMenu(menu.createIterator()); } void printMenu(Iterator iterator) { while (iterator.hasNext()) { MenuItem menuItem = (MenuItem)iterator.next(); System.out.print(menuItem.getName() + ", "); System.out.print(menuItem.getPrice() + " -- "); System.out.println(menuItem.getDescription()); }
32
Composite Pattern defined The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
33
Using composite pattern
34
Composite Pattern
35
Complex hierarchy of menu items
36
Composition treated as one entity or as parts
37
Operations applied to whole or parts
41
Look at Eclipse … Code for MenuComponent Code for MenuItem Code for Menu Code for Waitress TestDrive
42
Some observations The “print menu” method in the MenuComponent class is recursive. Now lets look at an alternative implementation which uses an iterator to iterate through composite classes the composite iterator
43
Look at Eclipse code for … Composite iterator Null iterator Vegetarian menu
44
Patterns and their role Strategy Adapter Iterator Façade Composite Observer Clients treat collections of objects and individual objects uniformly Provides a way to traverse a collection of objects without exposing the collection’s implementation. Simplifies the interface of a group of classes Changes the interface of one or more classes Allows a group of objects to be notified when some state changes. Encapsulation interchangeable behaviors and uses delegation to decide which one to use.
45
Summary so far.. OO Basics Abstraction Encapsulation Inheritance Polymorphism OO Principles Encapsulate what varies Favor composition over inheritance Program to interfaces not to implementations Strive for loosely coupled designs between objects that interact Classes should be open for extension but closed for modification. Depend on abstracts. Do not depend on concrete classes. Only talk to your friends Don’t call us, we will call you A class should have only one reason to change.
46
Summary so far… OO Patterns Strategy Pattern defines a family of algorithms, Encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. Decorator Pattern – attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative for sub-classing for extending functionality Abstractor Factory – Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Factory Method – Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to the subclasses.
47
OO Patterns - Continued Command Pattern – Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. The Façade Pattern provides a unified interface to a set of interfaces in a subsystem. Façade defines a higher level interface that makes the subsystem easier to use. Template Method defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. Iterator - provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.