cs302 topic: intro. to object oriented pgmg

54
CS302 Topic: Intro. to Object Oriented Pgmg. Tuesday, Sept. 6, 2005

Upload: others

Post on 18-Dec-2021

14 views

Category:

Documents


0 download

TRANSCRIPT

Using WiFi Signal Strength for Mobile Robot LocalizationTuesday, Sept. 6, 2005
Announcements Lab 1 due this Friday
Lab 2 is now available (online) (due Friday, Sept. 16) Uses red-black trees
Still in C
Don’t procrastinate!!! Finish Lab 1 early, then get early start on Lab 2.
This week’s lab meetings will discuss Lab 2 assignment
Today’s movie case study: Movies of swarms, and relationship to OOP
Computational Models Modeling is an essential part of many disciplines in science and engineering, including software engineering
The importance of having a “good” model increases with the complexity of the project
Computational Models
Main reason for developing models:
Understanding the system being developed
We build models of complex systems because we cannot comprehend such a system in its entirety
Principles of Modeling The choice of the model has a significant impact on how the problem is approached and how a solution is devised
A model can be expressed at different levels of precision
A good model is connected to reality
A single model is not sufficient to represent most systems; A set of nearly independent models are required
Models in Software Engineering
A modeling technique suitable for the programming approach should be used
Since object-oriented approach has proven to be superior to other approaches, models designed for this approach are widely used in software engineering
Basic Principles of Object Orientation
Abstraction
Encapsulation
Modularity
Hierarchy
Inheritance
An example of an item purchasing abstraction:
A model that includes most important aspects of a given problem while ignoring less important details
What is Encapsulation?
What is Modularity?
The breaking up of something complex into manageable pieces or modules
Queue
What is Inheritance?
Process by which one object acquires properties of another object
Supports concept of hierarchical classification
With inheritance, object only needs to define what makes it unique within its class
In OOP, Inheritance means inheriting another object’s interface, and possibly its implementation
Example of Interface Inheritance
Suppose you want to define Search_Object_1 that: Stores (key, value) pairs Allows values to be looked up using keys Supported operations for Search_Object_1:
insert: Adds a (key, value) pair to the object delete: Deletes a (key, value) pair from the object lookup: Given a key, retrieves the value associated with that key from the object
Later, define new object (Search_Object_2) that additionally allows traversing (key, value) pairs in sorted order
Supported operations for Search_Object_2: All of above, plus: rewind: returns us to beginning next: returns next (key, value) pair
Accomplish this by having Search_Object_2 inherit Search_Object_1’s interface
Example of Implementation Inheritance
Suppose: Search_Object_1 is implemented using binary search tree You already have a binary search tree implemented, including implementations for the previous operations, but using different names
To inherit binary search tree implementation, we make Search_Object_1 be a subclass of the binary tree.
We then make the insert, delete, and lookup operations call the appropriate binary tree operations
But, there’s a problem with Implementation Interface…
Remember: Object is supposed to hide its implementation
Implementation should be interchangeable with other objects that implement the same interface
Problem: Search object’s interface is tied to the binary search tree’s interface
By making search object inherit from binary search tree, we’ve also made its implementation dependent on the binary tree
NOT GOOD!!
More detailed look at Object Orientation…
1. Object
2. Class
3. Attributes
4. Operation
5. Interface (Polymorphism)
6. Generalization Relationships
1. What is an Object? An object is a “smart” data structure
Set of state variables
Set of methods for manipulating state variables Examples – physical, conceptual, or software entities:
Physical entity:
Conceptual entity:
Software entity:
Objects (con’t.)
An object advertises: The types of data it will store The types of operations it allows to manipulate its data (i.e., its interface)
An object hides: Its implementation of the above
An object is something that has: State Behavior (i.e., operations) Identity
An Object Has State
The state of an object is one of the possible conditions in which an object may exist
The state of an object normally changes over time
Represented by: Attribute values + Links (relationship instances)
Object: L. Parker L. Parker
9738239
An Object Has Behavior
Behavior determines how an object acts and reacts to requests from other objects
Behavior is represented by the set of messages it can respond to (i.e., the operations the object can perform)
Add me to CS302 with L. Parker
(Returns: confirmation) Registration System CS302 Course
Representing Objects
As an example, we can represent an object as rectangles with underlined names
: Professor L. Parker
L. Parker : Professor
2. What is a Class?
A class is a description of a group of objects with common properties (attributes), behavior (operations), relationships, and semantics
An object is an instance of a class
A class is an abstraction in that it: Emphasizes relevant characteristics
Suppresses other characteristics
The Relationship Between Classes and Objects A class is an abstract definition of an object • It defines the structure and behavior of each
object in the class
• It serves as a template for creating objects
Objects may be grouped into classes A particular object of a class is an instance
Professor
Sample Class
Representing Classes
As an example, we can represent a class using a compartmented rectangle
Professor
Class Compartments
For example, we can represent a class as being comprised of three sections: • The first section contains the class name
• The second section shows the structure (attributes)
• The third section shows the behavior (operations)
Professor Name
save() change()
Class Compartments (cont.)
The second and third sections may be suppressed if they need not be visible on the diagram
Professor Professor
save() change()
CS302 CS 311
Classes of Objects
3. What is an Attribute?
Object
Class
Attributes
CourseOffering
5. What is Polymorphism?
The ability to hide many different implementations behind a single interface
Manufacturer A
Manufacturer B
Manufacturer C
OO Principle:
5. (con’t.) What is an Interface?
A named set of operations that characterize the behavior of an element.
The interface formalizes polymorphism
6. Relationships: Generalization
A relationship among classes where one class shares the structure and/or behavior of one or more classes
Defines a hierarchy of abstractions in which a subclass inherits from one or more super classes
Single inheritance
Multiple inheritance
Example: Single Inheritance
Ancestor
FlyingThing Animal
multiple inheritance
A subclass may: Add additional attributes or operations
Redefine inherited operations (use caution)
Common attributes, operations, and/or relationships are shown at the highest applicable level in the hierarchy
Example: What Gets Inherited
GroundVehicle weight licenseNumber register()
Facilitates architectural and code reuse
Models more closely reflect the real world More accurately describe corporate data and processes
Are decomposed based on natural partitioning
Can be easier to understand and maintain
Stability A small change in requirements does not mean massive changes in the system under development
Contrast 2 Programming Styles: (1) Procedural and (2) Object-Oriented
Procedural programming: Organize system around procedures that operate on data
Object-oriented programming Organize system around objects that receive messages
An object encapsulates data and operations
Computation as Simulation Procedure programming consists of procedures acting on data
Object-oriented programming consists of objects interacting
Main() creates a web of objects and starts them interacting
The Object Oriented Programming Process
Step 1: Identify the data and the operations on the data These begin to form the classes
Step 2: Determine how the classes interact
(Iterate)
Step n: Create program on top of the classes
Using classes and instances to design a system Suppose we want to build a “star trek wars” simulator
We can begin by thinking about what kinds of objects we want (i.e., what classes, their state information, and their interfaces)
Ships
Planets
Foreign creatures
We can then extend to thinking about what particular instances of objects are useful
Millenium Falcon
Further extensions to our simulator…
Animate the world: Add a clock that moves time forward in the universe
Keep track of things that can move
Clock sends “CLOCK-TICK message” to objects to have them update their state
Add TORPEDO class to system
A simple Sales Order Example
Order
Product
Sale
Effect of Requirements Change
Airplane
Movies…
How much do you know? Let’s quiz you…
What concept does each of the following refer to? “Abstractions arranged in order of rank or level”
ANSWER: Hierarchy
ANSWER: Modularity
“Extracting the essential details about an item or group of items, while ignoring the unessential details”
ANSWER: Abstraction
ANSWER: Encapsulation
How much do you know? Let’s quiz you…
What concept does each of the following refer to? “Process by which one object acquires properties of another object”
ANSWER: Inheritance
“The ability to hide many different implementations behind a single interface”
ANSWER: Polymorphism
“An instance of a class” ANSWER: An object
History and Languages 1967 Simula 1970-83 Smalltalk 1979 Common LISP Object System 1980 Stroustrup starts on C++ 1983 Objective C 1986 C++ 1987 Actor, Eiffel 1991 C++ release 3.0 199x Plethora of OOP books/articles 1996 Java 1983-89 Language books with OOP concepts 1989-92 Object-oriented design books 1992-present Object-oriented methodology books
OO Programming Languages We’ll be using C++, but there are lots of other object- oriented languages:
Java Self Python Perl Prograph Modula 3 Oberon Scheme Smalltalk Venders Prolog++ Ada 95 Object Pascal (Delphi) …
That’s all, folks… Your homework:
Keep working on Lab 1!! Due Friday, Sept. 9.
If you haven’t already, read handout for today on Object- Oriented Programming
See “Schedule/Readings/Notes” link for today: http://www.cs.utk.edu/~parker/Courses/CS302-fall05/Schedule.html
Before class next time: 3 readings for next class
See “Schedule/Readings/Notes” link for today: http://www.cs.utk.edu/~parker/Courses/CS302-fall05/Schedule.html
Announcements
What is Abstraction?
What is Encapsulation?
What is Modularity?
What is Hierarchy?
What is Inheritance?
More detailed look at Object Orientation…
1. What is an Object?
Objects (con’t.)
The Relationship Between Classes and Objects
Sample Class
Representing Classes
Class Compartments
5. What is Polymorphism?
6. Relationships: Generalization
Example: Single Inheritance
Computation as Simulation
Using classes and instances to design a system
Further extensions to our simulator…
A simple Sales Order Example
Class Diagram for the Sales Example
Effect of Requirements Change
History and Languages
OO Programming Languages