+ All Categories
Home > Documents > CSCI-383 Object-Oriented Programming & Design Lecture 3.

CSCI-383 Object-Oriented Programming & Design Lecture 3.

Date post: 03-Jan-2016
Category:
Upload: solomon-morton
View: 226 times
Download: 3 times
Share this document with a friend
Popular Tags:
26
CSCI-383 Object-Oriented Programming & Design Lecture 3
Transcript

CSCI-383

Object-Oriented Programming & Design

Lecture 3

OOP: The General Principle

“ An object oriented program is structured as a community of interacting agents called objects. Action is initiated in object oriented programming by the transmission of a message to an agent (an object). The message encodes the request for action and is accompanied by additional information (arguments) needed to carry out the request. The receiver is the agent to which the message is sent. If the receiver accepts the message it accepts the responsibility to carry out the indicated action. In response to a message the receiver will perform some method to satisfy the request. “

OOP: A Way of Viewing the World

“ Chris wants to send some flowers to his friend Robin who lives in a distant city. Because of the distance, Chris cannot pick up the flowers and bring them to Robin in person. Chris goes down to Fred a local florist and tells him the number and type of flowers he wants together with the address they need to be delivered to. Chris can rest assure that the flowers will be delivered. ”

Agents, Responsibility, Messages, Methods

Chris finds an appropriate agent (Fred) And passes to her a message containing a request It is the responsibility of Fred to satisfy the request There is some method -a set of operations- used by Fred

to do this Chris does not need to know the particular method he will

use, this information is hidden from inspection

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Behavior and Interpretation

Although different objects may accept the same message, the actions (behavior) the object will perform will likely be different When Fred would ask his wife Beth to send some

flowers to Robin for her birthday she might use a different method than the florist Fred

determination of what behavior to perform may be made at run-time, a form of late binding

The fact that the same name can mean two entirely different operations is one form of polymorphism, a topic we will discuss at length in subsequent chapters

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Elements of OOP - Recursive Design

Every object has it's own memory, which consists of other objects

Each object is like a miniature computer itself - a specialized processor performing a specific task

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Non-interference

It is important that objects be allowed to perform their task however they see fit, without unnecessary interactions or interference with other objects “Instead of a bit-grinding processor ... plundering

data structures, we have a universe of well-behaved objects that courteously ask each other to carry out their various desires” -- Dan Ingalls

“Ask not what you can do to your data structures, but ask what your data structures can do for you”

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Elements of OOP - Classes Every object is an instance of a class. A class

groups similar objects The class is the repository for behavior associated

with an object The behavior I expect from Fred is determined from

a general idea I have of the behavior of Florists We say Fred is an instance of the class Florist Behavior is associated with classes, not with

individual instances. All objects that are instances of a class use the same method in response to similar messages

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Hierarchies of Categories

But there is more that I know about Fred than just that he is a Florist. I know he is a ShopKeeper, and a Human, and a Mammal, and a Material Objects, and so on

At each level of abstraction I have certain information recorded. That information is applicable to all lower (more specialized) levels

Elements of OOP - Inheritance

The principle that knowledge of a more general category is also applicable to a more specific category is called inheritance

Classes can be organised into a hierarchical inheritance structure. A child class (or subclass) will inherit attributes from a parent class (super class) higher up in the hierarchy. An abstract parent class is a class for which there are no direct instances, it is only introduced to group subclasses

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Hierarchies of Categories

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Class Hierarchies

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Elements of OOP - Overriding Subclasses can alter or override information

inherited from parent classes: All mammals give birth to live young A platypus is an egg-laying mammal

The search for a method to use in response to a message starts with the receiver’s class and continues up the parent chain. When methods with the same name are available higher in the class hierarchy the method that executes is said to override the inherited behavior

Inheritance combined with overriding are where most of the power of OO originates

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Problem Solving

Because the OOP view is similar to the way in which people go about solving problems in real life (finding another agent to do the real work!), intuition, ideas, and understanding from everyday experience can be brought to bear on computing

On the other hand, common sense was seldom useful when computers were viewed in the process-state model, since few people solve their everyday problems using pigeon-holes

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

From Newsweek “Unlike the usual programming method -- writing

software one line at a time-- NeXT's “object-oriented” system offers larger building blocks that developers can quickly assemble the way a kid builds faces on Mr. Potato Head.”

Chapter 2

Abstraction

Coping with Complexity Is the complexity of a programming project linear in terms

of the number of programmers involved? E.g., Can two programmers do in one month what one

programmer can do in 2 months ?

Interconnectedness, the dependence of one portion of code on another portion, is responsible for this phenomena

Abstraction mechanisms try to cope with this and object oriented techniques offer yet another step forward

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Abstraction

What is abstraction? Abstraction is the purposeful suppression, or hiding,

of some details of a process or artifact, in order to bring out more clearly other aspects, details, or structure

Think of a model, give an example of a model Do we need it? Why?

Yes, mainly because it helps us to deal with complexity

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Information Hiding

Information hiding is the purposeful omission of details in the development of an abstract representation

Information hiding is what allows abstraction to control complexity

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Abstraction in an Atlas Think of an atlas, and the various different levels of

maps A map of the world, contains mountain ranges, large

political boundaries A map of a continent, contains all political

boundaries, large cities A map of a country, contains more cities, major roads A map of a large city, roads, major structures A map of a portion of a city, buildings, occupants

Each level contains information appropriate to the level of abstraction

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Levels of Abstraction in OO Programs

At the highest level of abstraction we view a program as a community of interacting objects

Important characteristics here are the lines of communication between the various agents

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Levels of Abstraction in OO Programs (cont’d)

The next level of abstraction is found in some (but not all) OO languages. A package, Unit or Name Space allows a programmer to surround a collection of objects (a small community in itself) with a layer, and control visibility from outside the module

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Levels of Abstraction in OO Programs (cont’d)

The next level of abstraction considers the relationship between two individual objects. Typically one is providing a service, and the other is using the service

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Levels of Abstraction in OO Programs (cont’d)

We can next examine just the person providing a service, independent of the client. We define the nature of the services that are offered, but not how those services are realized

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Levels of Abstraction in OO Programs (cont’d)

Next we look at the services provided, but from the implementation side

public class LinkedList implements Stack ... {

public void pop () throws EmptyStackException { ... }

...

}

Concern here is with the high level approach to providing the designated service

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Levels of Abstraction in OO Programs (cont’d)

Finally, we consider the implementation of each method in isolationpublic class LinkedList implements Stack ... {

... public void pop () throws EmptyStackException {

if (isEmpty()) throw new EmptyStackException(); removeFirst(); // delete first element of list

} ...

} Every level is important, and often you move

quickly back and forth between levels


Recommended