

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


































Mattox Beckman
Illinois Institute of Technology Department of Computer Science
CS 331 / August 29, 2008
(^1) Introduction Objectives
(^2) Abstraction
(^3) ADTs
(^4) Objects
(^5) Common Models
(^6) Representation Primitive Data Aggregate Data
Abstraction
First, you need a phenomenon to model. Second, you ask some questions about the phenomenon. Question 1: What are the elements of the phenomenon? Question 2: What are the properties of the elements? Question 3: Which of these elements and actions do we care about? This is the interface or view. Third, you simulate the phenomenon. Create an implementation for your model. Use the interface to restrict access to the model. Important question: what are the limitations of the model?
Abstraction
Phenomenon: the integers. Questions: Elements: zero, and positive and negative whole numbers. Properties: infinite set, can add, increment, subtract, etc. Don’t care: maybe it’s not that important to count to ∞. Simulation: use 16 binary digits. First one is a sign bit. Use builtin assembly operations to manipulate the bits. Limitations: can’t count past 32767.
Abstraction
ADTs
Here is some code from the Linux kernel.
1 char (^) * strcat(char * dest, const char * src) 2 { 3 char tmp = dest; 4 while (dest) 5 dest++; 6 while ((*dest++ = *src++) != ’\0’); 7 return tmp; 8 }
What is a string? (What are the elements? ... actions?) How do dest and src act like strings? How do they not act like strings?
ADTs
User
Phenomenon
Interface Implementation
From the user’s perspective, interaction with the phenomenon and interaction with the model (via the interface) is indistinguishable. The implementation can be changed at will without affecting the user’s experience.
ADTs
An implementation itself can be thought of as a phenomenon. E.g., using integers to model booleans values... or states in a machine... Computer Programming is very much like Wizardry. “Any technology, sufficiently advanced, is indistinguishable from magic.” — Isaac Asimov
Objects
Two levels of abstraction Classic modeling method: Objects are nouns Methods are verbs Example: dictionary. How do you model a dictionary?
Objects
This is the greatest strength of OOP. Separates and hierarchicalizes properties. Classic examples: graphics libraries, animals. You may promise more, but you may not deliver less. This is the greatest weakness of OOP. Not all “subclasses” follow this relation. Semantics of inheritance is not well understood. Note Well: OOP is a powerful, popular development model. But it’s not the only one!
Objects
A simple tool does one thing well. Complicated things are easy to break. The interface is what we evaluate here. Ways to break simplicity: Have one variable represent more than one piece of data. Have operations that need multiple steps, and if one is left out, the object’s state will be inconsistent. Make all the data public. Don’t follow established protocols. (E.g., a temperature class which needs strings like “F 94” instead of “94 F”.)
Objects
Ways to break reusability: Make the class way too specific. A DogColor class to keep track of different colors of dogs. Don’t follow established protocols. Just about anything from a certain well-known operating system vendor. Don’t document the class. Don’t add all the functionality one would expect. A Shape class that has a rotate method, but no translate method. “But I didn’t need one of those when I wrote it.”
Common Models
Sets contain objects. Order is not considered. Sets can contain at most one “copy” of an element. Operations: insertion, deletion, membership testing not “how many” Implementations: arrays, lists, trees ... Modification: Allow order to matter (ordered lists, stacks queues). Allow multiple copies of an object (bags).
Common Models
Arrays contain objects. Position is very important. Arrays can contain multiple “copies” of an element. Operations: insertion, lookup Implementations: arrays, hash-tables (sort of)