Object FundamentalsPart ThreeKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/5448 — Lecture 4 — 09/03/20091Lecture Goals• Continue our tour of the basic concepts, terminology, and notations for object-oriented analysis, design, and programming•Some material for this lecture is drawn from Head First Java by Sierra & Bates, © O'Reilly, 20032Overview• Delegation• HAS-A• Inheritance• IS-A• Polymorphism• message passing• polymorphic arguments and return types• Interfaces• Abstract Classes• Object Identity• Code Examples3Delegation (I)• When designing a class, there are three ways to handle an incoming message• Handle message by implementing code in a method• Let the class’s superclass handle the request via inheritance• Pass the request to another object (delegation)• Note: goes hand in hand with composition (not to be confused with aggregation/composition which is a design concept)•You compose one object out of others•The host object delegates requests to its internal objects4Delegation (II)• Delegation is employed when some other class already exists to handle a request that might be made on the class being designed• The host class simply creates a private instance of the helper class and sends messages to it when appropriate• As such, delegation is often referred to as a “HAS-A” relationship• A Car object HAS-A Engine object5• Here is an example of a class delegating a responsibility to another class• Grocery List has an attribute called items and it delegates all of its work-related tasks (storing/enumerating items) to it.Simple Example (I)6# Extremely lame sorted list class; lame because it only# defines the list methods needed by the example code belowclass SortedList(object): def __init__(self, source_list): self.sorted_list = sorted(list(source_list)) def append(self, item): self.sorted_list.append(item) self.sorted_list = sorted(self.sorted_list) def __iter__(self): return self.sorted_list.__iter__()# GrocerList delegates to an internal list object called itemsclass GroceryList(object): def __init__(self, items): self.items = items def add_item(self, item): self.items.append(item) def print_items(self): for i, item in enumerate(self.items): print("{0}. {1}".format(i+1, item))# we start by creating a grocery list that just uses python's default list# object as the object that it delegates to.my_list = GroceryList([])# we add itemsmy_list.add_item("milk")my_list.add_item("bread")my_list.add_item("snickers")my_list.add_item("fish")# we print them outprint("")print("Unsorted: ")print("")my_list.print_items()print("")# now we create a sorted list that contains our grocery list# and we change our grocery list object to now delegate to this# sorted list. And this shows that we can change delegation relationships# at runtimemy_list.items = SortedList(my_list.items)# lets add new items to our listmy_list.add_item("wine")my_list.add_item("cheese")# we print out the list again; this time the items appear in sorted orderprint("")print("Sorted: ")• We can create a GroceryList using a python list object like this:• Now imagine, we no longer liked the capabilities of the default list and we wanted to switch to another class, for example, a list that keeps its items sorted• This line is creating a new sorted list, passing in the current set of items, and setting GroceryList.items to the new sorted list. This is an example of a delegation relationship changing at runtime.Simple Example (II)7# Extremely lame sorted list class; lame because it only# defines the list methods needed by the example code belowclass SortedList(object): def __init__(self, source_list): self.sorted_list = sorted(list(source_list)) def append(self, item): self.sorted_list.append(item) self.sorted_list = sorted(self.sorted_list) def __iter__(self): return self.sorted_list.__iter__()# GrocerList delegates to an internal list object called itemsclass GroceryList(object): def __init__(self, items): self.items = items def add_item(self, item): self.items.append(item) def print_items(self): for i, item in enumerate(self.items): print("{0}. {1}".format(i+1, item))# we start by creating a grocery list that just uses python's default list# object as the object that it delegates to.my_list = GroceryList([])# we add itemsmy_list.add_item("milk")my_list.add_item("bread")my_list.add_item("snickers")my_list.add_item("fish")# we print them outprint("")print("Unsorted: ")print("")my_list.print_items()print("")# now we create a sorted list that contains our grocery list# and we change our grocery list object to now delegate to this# sorted list. And this shows that we can change delegation relationships# at runtimemy_list.items = SortedList(my_list.items)# lets add new items to our listmy_list.add_item("wine")my_list.add_item("cheese")# we print out the list again; this time the items appear in sorted orderprint("")print("Sorted: ")# Extremely lame sorted list class; lame because it only# defines the list methods needed by the example code belowclass SortedList(object): def __init__(self, source_list): self.sorted_list = sorted(list(source_list)) def append(self, item): self.sorted_list.append(item) self.sorted_list = sorted(self.sorted_list) def __iter__(self): return self.sorted_list.__iter__()# GrocerList delegates to an internal list object called itemsclass GroceryList(object): def __init__(self, items): self.items = items def add_item(self, item): self.items.append(item) def print_items(self): for i, item in enumerate(self.items): print("{0}. {1}".format(i+1, item))# we start by creating a grocery list that just uses python's default list# object as the object that it delegates to.my_list = GroceryList([])# we add itemsmy_list.add_item("milk")my_list.add_item("bread")my_list.add_item("snickers")my_list.add_item("fish")# we print them outprint("")print("Unsorted: ")print("")my_list.print_items()print("")# now we create a sorted list that contains our grocery list# and we change our grocery list object to now delegate to this# sorted list. And this shows that we can change delegation relationships# at runtimemy_list.items = SortedList(my_list.items)# lets add new items to our listmy_list.add_item("wine")my_list.add_item("cheese")# we print out the list again; this time the items appear in sorted orderprint("")print("Sorted: ")Delegation (III)•
View Full Document