csci 383 object-oriented programming & design lecture 3 martin van bommel

26
CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Upload: evangeline-price

Post on 19-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

CSCI 383

Object-Oriented Programming & Design

Lecture 3

Martin van Bommel

Page 2: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 2

Imperative Programming

So, what are the paradigms of programming?

Imperative programming is the “traditional” model of computation State Variables Assignment Loops

A processing unit is separate from memory, and “acts” upon memory

Page 3: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 3

Visualization of IP

Page 4: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 4

Recursive Design

Why Not Build Program out of Computers?

Alan Kay wondered why conventional computers were constructed out of pieces that were useless by themselves

Why not build a whole out of pieces that were similar at all levels of detail? (Think of fractals)

Idea: A program can be build out of little computing agents

Page 5: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 5

Recursive Design (cont’d)

The structure of the part mirrors the structure of the larger unit

Page 6: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 6

Kay's Description of OOP

Object-oriented programming is based on the principle of recursive design Everything is an object Objects perform computation by making requests of each other

through the passing of messages Every object has its own memory, which consists of other

objects Every object is an instance of a class. A class groups similar objects The class is the repository for behavior associated with an object Classes are organized into singly-rooted tree structure, called an

inheritance hierarchy

We can illustrate these principles by considering how to go about solving a problem in real life

Page 7: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 7

Illustration of OOP Concepts

To illustrate the concepts of OOP in an easily understood framework, consider Chris sending flowers to Robin. Chris can't deliver them directly.

So Chris uses the services of the local Florist Chris tells the Florist (named Fred) the address for Robin,

how much to spend, and the type of flowers to send Fred contacts a florist in Robin’s city, who arranges the

flowers, then contacts a driver, who delivers the flowers If we start to think about it, there may even be other

people involved in this transaction. There is the flower grower, perhaps somebody in charge of arrangements, and so on

Page 8: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 8

Agents and Communities

Our first observation is that results are achieved through the interaction of agents, which we will call objects

Furthermore, any nontrivial activity requires the interaction of an entire community of objects working together

Each object has a part to play, a service they provide to the other members of the community

Page 9: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 9

Elements of OOP - Objects

So we have Kay's first principle Everything is an object

Actions in OOP are performed by agents, called instances or objects

There are many agents working together in the scenario Chris, Robin, the florist, the florist in Robin’s city, the

driver, the flower arranger, and the grower. Each agent has a part to play, and the result is

produced when all work together in the solution of a problem

Page 10: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 10

Elements of OOP - Messages

And principle number 2: Objects perform computation by making requests of

each other through the passing of messages Actions in OOP are produced in response to

requests for actions, called messages. An instance may accept a message, and in return will

perform an action and return a value To send the flowers, Chris gives a message to Fred.

Fred in turn gives a message to the florist in Robin’s city, who gives another message to the driver, and so on

Page 11: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 11

Information Hiding

Notice that the user of a service being provided by an object need only know the name of the messages that the object will accept

I need not have any idea how the actions performed in response to my request will be carried out

Having accepted a message, an object is responsible for carrying it out

Page 12: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 12

Elements of OOP - Receivers

Messages differ from traditional function calls in two very important respects: In a message, there is a designated receiver that

accepts the message The interpretation of the message may be different,

depending upon the receiver

Page 13: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 13

OOP: The General Principle

An object oriented program is structured as a community of interacting agents called objects.

Action is initiated 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.

Page 14: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 14

OOP: A Way to View 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 assured that the flowers will be delivered.

Page 15: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 15

Agents, Messages, Methods

Chris finds an appropriate agent (Fred) And passes to him 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

Page 16: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 16

Behavior and Interpretation

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

Robin for her birthday, she might use a different agent than the florist Fred

determination of what behavior to perform may be made at run-time (late binding)

The fact that the same name can mean two entirely different operations is one form of polymorphism, a topic we will discuss later

Page 17: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 17

OOP Element - Recursive Design

Every object has its own memory, which consists of other objects

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

Page 18: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 18

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”

Page 19: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 19

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 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

Page 20: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 20

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

Page 21: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 21

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 (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

Page 22: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 22

Hierarchies of Categories

Page 23: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 23

Class Hierarchies

Page 24: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 24

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

Page 25: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 25

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 However, infinite recursion does not solve the problem Someone has to do the work!

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

Page 26: CSCI 383 Object-Oriented Programming & Design Lecture 3 Martin van Bommel

Fall 2010 CSCI 383 Lecture 3 M. van Bommel 26

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.”