Oosad 04
-
Upload
getamesay-berihun -
Category
Documents
-
view
765 -
download
3
description
Transcript of Oosad 04
Object Oriented Concepts
Chapter 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
• 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
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
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
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
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
Modeling Inheritance
• Line with closed arrowhead• B inherits from A
Class A(the super class)
Class B (The subclass)
Inheritance Tips and Techniques
• Look for similarities• Look for existing classes• Follow the sentence rule• Avoid implementation inheritance
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
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
Multiple Inheritance
Amphibian
maximumSpeednamenumberofLegsscaleColorsjumpSpan
jumpeatswim
Fish
wingSpantypename
eatfly
Lizard
numberOfLegsscaleColorsmaximumSpeed
eat fly
Lizard
maximumSpeedscaleColorsnumberOfLegs
eat fly
Fish
wingSpanType
eatfly
Amphibian
name
jumpswim
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
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
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
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
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
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
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
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
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
aStudent: Student :Course:Seminar
enrollStudent(aStudent)
isStudentEligible(aStudent)
GetSeminarHistory()
seminarHistoryEligibilityStatus
EnrollmentStatus ()
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
Persistence Tips and Techniques
• Business/domain classes are usually persistent • User Interface classes are usually transitory • You need to store both attributes and
association
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
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?
Customer Product00..n 0..n
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
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
Coupling Tips and Techniques
• Avoid high coupling if you can• Document high coupling thoroughly
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
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
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
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