Oosad 04

34
Object Oriented Concepts Chapter 04

description

 

Transcript of Oosad 04

Page 1: Oosad 04

Object Oriented Concepts

Chapter 04

Page 2: Oosad 04

The four basic OO concepts

• Class – software abstraction of an object, including data and functionality

• Object – person, place, thing, event, concept, screen, or report

• Attribute - variable• Method – function or procedure

Page 3: Oosad 04

• It is optional in UML to show attributes and methods of a class

• There are two types of attributes – Instance attributes – applicable to a single object– Static attributes – applicable to all instances of a single

class – Underlined

• Instance methods – operate on a single instance – Enroll a student

• Static methods – operate on all instances – Search

Page 4: Oosad 04

Abstraction, Encapsulation and Information Hiding

• Abstraction – Process of including features, attributes and

methods that are of interest to your application and ignore the rest

– It is an analysis issue that deals with what a class knows or does

– Depends on the context in which you define the abstraction

– The act of defining the interface of something– Act of painting a clear box around something

Page 5: Oosad 04

Encapsulation

• Deals with – how you intend to modularize the features of a system. – How functionality is included is compartmentalized within

a system

• You can build anything you want and then you can later change the implementation and it will not affect other components within the system

• Act of painting the box black• Define something is going to be done, but you are

not telling the rest of the world how you are going to do it

Page 6: Oosad 04

Information Hiding

• Restricting access to data attributes and some methods, to make your application maintainable

• If one class wants information about another class, it should have to ask for it, instead of taking it.

• Prevents highly coupled codes

Page 7: Oosad 04

Inheritance

• Two or more classes often share the same attributes and/or methods

• Don’t need to write same code repeatedly• Models “is a” and “is like” relationships• Enables you to reuse existing data and code easily • E.g. – STUDENTS have names, addresses and telephone

numbers, vehicles • PROFESSORS have also the same

• Create a class PERSON having those attributes and make STUDENT and PROFESSOR inherit from it

• Everything a superclass knows the subclass knows or does it for free

Page 8: Oosad 04

Modeling Inheritance

• Line with closed arrowhead• B inherits from A

Class A(the super class)

Class B (The subclass)

Page 9: Oosad 04

Inheritance Tips and Techniques

• Look for similarities• Look for existing classes• Follow the sentence rule• Avoid implementation inheritance

Page 10: Oosad 04

Single and Multiple Inheritance

• Single – when a class inherits from only one other class, we call this single inheritance

• Multiple – when a class inherits from two or more other classes

Page 11: Oosad 04

Single Inheritance Airplane

maximumAltitudemaximumFuelmaximumSpeedmaximumNumberOfPassengersnumberOfEngines speed

accelerate bankclimbdecelerate decend

Car

make maximumFuelmaxiumumNumberOfPassengersmodel speed

accelerate decelerate turn

Car

make model

turn

Airplane

maximumAltitudemaximumSpeednumberOfEngines

bankClimbdecend

Vehicle

maximumFuelmaxiumumNumberOfPassengersspeed

accelerate Decelerate

Page 12: Oosad 04

Multiple Inheritance

Amphibian

maximumSpeednamenumberofLegsscaleColorsjumpSpan

jumpeatswim

Fish

wingSpantypename

eatfly

Lizard

numberOfLegsscaleColorsmaximumSpeed

eat fly

Lizard

maximumSpeedscaleColorsnumberOfLegs

eat fly

Fish

wingSpanType

eatfly

Amphibian

name

jumpswim

Page 13: Oosad 04

Abstract and Concrete Classes

• Abstract classes are in italics • Difference – objects are instantiated from

concrete classes, but not from abstract classes • Your software will instantiate airplane and car

objects but will never create vehicle objects

Page 14: Oosad 04

Association

• Exist between objects– Students TAKE courses, professors TEACH courses – Criminals ROB banks etc…

• Identify the relationship and describe it– Students TAKE courses

• How many students?, How many courses? Etc

– Identify the Cardinality and Optionality • Multiplicity

Page 15: Oosad 04

Modeling Associations

• Thin line connecting two classes• Depict three things

– Label– Cardinality – Role

UML multiplicity indicators

Indicator Meaning

0..1 Zero or one

1 One only

0..* Zero or more

1..* One or more

n Only n (where n>1)

0..n Zero to n (where n>1)

1..n One to n (where n>1)

Class AClass B

Cardinality A

label

Cardinality B

role A role B

Page 16: Oosad 04

Student Semester

Employee Position

Course0..* 1..*registers

assists

0..1 0..*

0..* 1consists of

0..10..1 1

0..*manages

holds

student

teaching assistant

staff member

manager

Page 17: Oosad 04

How associations are Implemented

• Associations are maintained through combination of attributes and methods

• The attributes store the information necessary to maintain the relationship and methods keep the attributes current

• Students would have an attribute takes, array that stores information of courses the student is currently taking– Add Course, Remove Course

• Course may have attribute student– Add Student, Remove Student

Page 18: Oosad 04

Aggregation

• Models “is part of” association – An airplane is made up of fuselage, wings, engines, landing

gear, flaps and so on.– A delivery Shipment contains one or more packages

• Modeling aggregation – Diamond , also model multiplicity and roles – Two way association– Two flavors

• Hollow diamond – • Solid diamond – indicates composition, strong form

Page 19: Oosad 04

Aggregation tips and techniques

• Apply the sentence rule• It should be part in the real world• You should be interested in the part• Show multiplicity and roles• Aggregation is inherited

Page 20: Oosad 04

Collaboration & Messages

• Collaboration occurs between objects when one object asks another for information or to do something.

• Eg. The airplane collaborates with its engines to fly• Objects collaborate with one another by sending each other

messages • Message is either a request to do something or a request for

information • Messages are modeled in UML sequence diagram and UML

collaboration diagrams • Discussed Later

Page 21: Oosad 04

Collaboration Tips and Techniques

• Some sort of association must exist• A corresponding method must exist in the target

object• There might be a return value• There may or may not be parameters• Messages show collaboration, not data flows• Sometimes the target needs to collaborate• Each method should do something• An object can collaborate with itself

Page 22: Oosad 04

aStudent: Student :Course:Seminar

enrollStudent(aStudent)

isStudentEligible(aStudent)

GetSeminarHistory()

seminarHistoryEligibilityStatus

EnrollmentStatus ()

Page 23: Oosad 04

Persistence

• How to store objects to permanent storage • The values of the objects as well as information

needed to maintain the relationships must be saved permanently

• Also concerned with their retrieval and deletion • Two types of objects exist: Persistent and Transient • Persistent: Customer• Transient: Customer editing screen

Page 24: Oosad 04

Persistence Tips and Techniques

• Business/domain classes are usually persistent • User Interface classes are usually transitory • You need to store both attributes and

association

Page 25: Oosad 04

Persistent Vs Transitory Associations

• Persistent associations are those that are permanent– Information to maintain it is saved – Eg. TAKE, TEACH

• Transitory associations are not saved to permanent storage and usually involve at least one transitory objects– If you are not persisting the object then you wont

be persisting its relationship with other objects

Page 26: Oosad 04

Class Exercise • Explain classes, attributes, methods and associations in your projects• What is multiplicity

– Give example

• When do we include Roles when we model associations, give example from your project point of view

• Describe the implementation of associations in your project• Give two examples of flavors of aggregation • Give example for one rule in aggregation• What is collaboration and how is collaboration achieved?• What is the difference between persistence and transient objects,

associations?

Page 27: Oosad 04

Customer Product00..n 0..n

Page 28: Oosad 04

Exercise 21. Object

2. Class

3. Method

4. Instance

5. Static

6. Inheritance

7. Association

8. Collaboration

9. Role

10. Label

11. Abstract classes

12. Concrete classes

13. Modeling inheritance

14. Modeling aggregation

15. Modeling association

16. Information hiding

17. encapsulation

18. abstraction

a. To the whole object

b. “Is part of” associations

c. Result of instantiation

d. Line with closed arrowhead

e. Definition of a object

f. Asking for information

g. Function and procedure

h. Line with diamond

i. Optional in association

j. Object can be instantiated

k. Thin line

l. Access restriction

m. Paint a box black

n. Relation

o. Object cant be instantiated

p. Paint a box clear

q. To one object

r. In case of recursion

Page 29: Oosad 04

Coupling

• Measure of how much two items such as classes or methods are interrelated – Coupled: when one class depends on another class– Loosely Coupled: when one class interacts with another

class but doesn’t know any of the implementation details of the other class

– Highly Coupled: when one class relies on the implementation of the other class

• High coupling is a major reason for maintenance • Increasing the coupling between two items may

make sense for performance reasons

Page 30: Oosad 04

Coupling Tips and Techniques

• Avoid high coupling if you can• Document high coupling thoroughly

Page 31: Oosad 04

Cohesion

• Measure of how much an item, such as a class or method, makes sense

• Method - Highly cohesive – it does one thing and one thing only – Enroll a student in a course

• Small and cohesive methods that do one thing and on thing only are easier to understand and maintain

• Highly cohesive class – one type of object and one type of object only – University Information System – Professors instead of

employees

Page 32: Oosad 04

Polymorphism

• You can treat instances of various classes the same way within your system.

• You can send a message to an object without first knowing what type it is and the object will still do “the right thing”, at least from its point of view.

• Eg. The poker game – Darn polymorphism • The different objects respond to the message in their own

way • Eg: Polymorphism at the University

– How the university handles the hiring of a new staff

Page 33: Oosad 04

Interface

• Definition of a collection of one or more operation signatures

• Implemented by classes and components• To implement an interface, a class or component

must include methods that have the operation signatures defined by the interface

• Eg. Class STUDENT implements the “Serializable” interface and the “Searchable” interface – The Lollipop notation – The box notation

Page 34: Oosad 04

Components• Modular, extensible unit of independent deployment that has

contractually specified interfaces(s) and explicitly defined dependencies• Components should be modular, extensible, and open

– Modular – components contains everything it needs to fulfill its responsibilities

– Extensible – a component can be enhanced to fulfill more responsibilities than it was originally intended to

– Open – it can operate on several platforms and interface with other components through a single programming interface

• Component diagrams show the software components that make up a larger piece of software, their interfaces, and their interrelationships.

• For the sake of our discussion, a components may be any large-grain item