DOC PREVIEW
CU-Boulder CSCI 5448 - Object Fundamentals Part Three

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

CU-Boulder CSCI 5448 - Object Fundamentals Part Three

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Object Fundamentals Part Three
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object Fundamentals Part Three and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Fundamentals Part Three 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?