+ All Categories
Home > Documents > Fundamental Concepts (Principles) of Object Oriented...

Fundamental Concepts (Principles) of Object Oriented...

Date post: 31-Jan-2018
Category:
Upload: leduong
View: 231 times
Download: 0 times
Share this document with a friend
24
1 Fundamental Concepts (Principles) of Object Oriented Programming These slides are based on: [1] Timothy A. Budd, Oregon State University, Corvallis, Oregon, [Available] ClickMe, September 2001.
Transcript
Page 1: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

1

Fundamental Concepts (Principles) of Object

Oriented Programming

These slides are based on: [1] Timothy A. Budd, Oregon State University, Corvallis, Oregon, [Available] ClickMe, September 2001.

Page 2: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

2

What is Object Oriented Programming?

• OOP was invented to mitigate the increasing complexity of software – trying to make software simpler and more manageable.

• The Object Oriented approach is to view software the same way we view the world – easier for us to understand.

• OOP is a way of thinking about how to create computer programs to solve problems • Everything is an object, and objects communicate between each

other and collaborate to implement tasks.

• Consider how we might go about handling a real-world situation and then ask how we could design software to do the same.

Page 3: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

3

Illustration Of OO Concepts Sending Flowers to a Friend

• Consider the problem of sending flowers to a friend who lives in a different city. • Chris is 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) Robin’s address, 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.

Page 4: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

4

Community of Agents (Objects) Helping Deliver Flowers

Robin

Chris Fred:Florist

Robin’sFlorist

:FlowerArranger

:Wholesaler :Grower :Gardener

:DeliveryPerson UML Instance Diagram

Page 5: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

5

Object Interaction

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

Each object has a role to play.

Each object provides a service, or performs an action, that is used by other members of the community.

Page 6: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

6

Messages and Methods

The chain reaction that ultimately resulted in the solution to Chris’s problem began with a request

given to the florist Fred.

This request lead to other requests, which lead to still more requests, until the flowers ultimately reached

Chris’s friend Robin.

Page 7: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

7

Messages and Methods

• Action is initiated in by the transmission of a message to an agent (an object).

• The message encodes the request for an action and may be accompanied by any additional information (arguments) needed to carry out the request.

• 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 8: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

8

Object Protocol • Each agent has responsibilities that it can do –

requests that it can fill out • Set of responsibilities is called the object’s protocol.

• An object’s protocol is the set of methods

(responsibilities and behavior) it can do and knows how to do, and the set of responses it can and will send back to the requestor in response to a request to perform a behavior.

Page 9: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

9

Information Hiding and Abstraction

• A receiving agent does not need to advertise or inform the requesting agent how the request will be implemented • For example, Fred will not tell Chris how he intends to deliver the flowers, and

whether there will be intermediaries involved, horse and buggy, planes, etc.

• This gives Fred flexibility to implement his methods without the interference from Chris.

• This results in simpler systems because there is less coupling between objects.

• Chris’s world is simpler because he does not take on the burden on worrying how the request will be implemented.

• Fred’s world is simpler because he does not need to follow Chris’s view of how a request should be implemented, if Chris were to specify one.

Page 10: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

10

Polymorphism • A receiving agent can implement a request differently.

• For example, on one hand, Fred may implement Chris’ delivery request by

sending a message to Robin’s florist. On the other hand, Robin’s florist, may send a message to the delivery person to send the flowers to Robin.

• Furthermore, perhaps Fred has multiple ways of implementing the same delivery request, and he chooses the best one according to the situation.

• This gives Fred flexibility to implement his methods in his own optimal way. It may be too expensive for Fred to deliver the flowers directly to Robin; it could be more efficient to contract Robin’s florist to do so. If the flowers were to be delivered to a destination near Fred’s business, then Fred may choose to deliver the flowers himself by car/bicycle.

• Different receiving agents may implement a request in different ways.

Page 11: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

11

Reusable Components • If there is a task to perform, the first thought of the client is to find somebody else

he or she can ask to do the work.

• For example, Fred may not know an efficient way of delivering the flowers to a foreign country. Instead of learning how to do this, Fred will choose to rely on the experience of others who have been doing this in the past and are very efficient at it.

• Instead of the client inventing the method to do the work, it is natural to look for some other object that can do the work, that has the experience in doing it, and is trusted to do it properly.

• This concept is one of “Building on the Experience of Others”

• When reusable components are used, the software becomes more reliable, less complex for the individual agents, and quicker to develop.

Page 12: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

12

Classes of Objects • Agents of the same type are similar, and they may be

classified as a class

• Although Chris has only dealt with Fred a few times, Chris has a rough idea of the transaction that will occur inside Fred's flower shop. Chris is able to make certain assumptions based on previous experience with other florists, and hence Chris can expect that Fred, being an instance of this category, will fit the general pattern. • Other Florists, like Jane, Samantha, and Fernando • Other Gardeners, etc. • Class for Florists, Gardeners, Delivery Person, etc.

Page 13: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

13

Inheritance Hierarchy • Each class can be organized into a hierarchy

• Chris knows, for example, that a transfer of money will be part of

the transaction, and that in return for payment Fred will offer a receipt. These actions are true of grocers, stationers, and other shopkeepers.

• Fred is a Florist, Florist is Shopkeeper, Shopkeeper is a Human, Human is a Mammal, Mammal is an Animal, Animal is a Material Object.

• Classes inherit properties of upper layer classes

Page 14: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

14

Categories Surrounding Fred

Florist

Shopkeeper

Human

Mammal

Animal

MaterialObject

UML Class Diagram (Showing Inheritance) Non-standard Diagram

Page 15: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

15

A Class Inheritance Hierarchy UML Class Diagram

Mammal

Animal

MaterialObject

Human Dog Platypus

Artist Dentist Shopkeeper

Florist Potter

Flower

Plant

Carnation

Page 16: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

16

Principles of Object-oriented (OO) Programming

1. Everything is an object

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

– Each object has a set of responsibilities (behavior), a set of things

it can do.

– Each object has a role to play and provides a service, or performs an action, that is used by other members of the community.

Page 17: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

17

Principles of Object-oriented (OO) Programming

2. Objects send messages (requests) to one another

• Action is initiated in object-oriented programming by sending a message to an object responsible for the action.

• Messages are usually implemented as methods, a way in which a request is implemented.

• Additional information is encoded in the argument list of the method, if required.

• In response, the receiver will perform some method to satisfy the request.

Page 18: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

18

Principles of Object-oriented (OO) Programming

3. All objects are instances of a class

– Objects of similar type are grouped into a class

– Objects within the same class will implement methods in the same way, and they will also have the same set of features (variables)

Page 19: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

19

Principles of Object-oriented (OO) Programming

4. Encapsulation

– Encapsulation is the packing of state (variables) and behavior (methods) into a single component (object).

– An object should have full control over its state and how it implements its behavior.

– It enforce protecting variables, functions from outside of class.

– Everything that belongs to objects of a certain class should be included in the class, and everything else should be left out.

Page 20: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

20

Principles of Object-oriented (OO) Programming

5. Information Hiding and Abstraction

– The implementation of an object’s methods are hidden from external view

– This means the system will be more flexible for change and optimization, and will also be simpler, because there will be less coupling between classes.

Page 21: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

21

Principles of Object-oriented (OO) Programming

6. Class Hierarchies

– Classes may be organized in an inheritance hierarchy, where higher layers contain more general information and lower layers contain more specific information about the class.

– Lower layer classes in the hierarchy inherit properties of higher layer classes in the hierarchy.

Page 22: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

22

Principles of Object-oriented (OO) Programming

7. Method Binding

– The search for a method to invoke in response to a given message begins with the class of the receiver.

– If no appropriate method is found, the search is conducted in the parent class of this class.

– The search continues up the parent class chain until either a method is found and executed

– If a method is not found, an error message is issued.

Page 23: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

23

Principles of Object-oriented (OO) Programming

8. Reuse and Reusable Components

– Reusing components increases reliability and quality.

– Building on the experience of others.

– Some characteristics that make software more easily reusable are modularity, loose coupling, high cohesion, information hiding and separation of concerns.

Page 24: Fundamental Concepts (Principles) of Object Oriented ...ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · 1 Fundamental Concepts (Principles) of Object Oriented

24

Principles of Object-oriented (OO) Programming

9. Polymorphism

– Different receiving agents may implement a request in different ways.

– This gives objects flexibility to implement methods in their own optimal way.


Recommended