csci-383 object-oriented programming & design lecture 10

20
CSCI-383 Object-Oriented Programming & Design Lecture 10

Upload: alaina-flora-summers

Post on 14-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI-383 Object-Oriented Programming & Design Lecture 10

CSCI-383

Object-Oriented Programming & Design

Lecture 10

Page 2: CSCI-383 Object-Oriented Programming & Design Lecture 10

Reflexive Associations

• It is possible for an association to connect a class to itself

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

Directionality in Associations

• Associations are by default bi-directional • It is possible to limit the direction of an association by adding

an arrow at one end

• In a specification view this would indicate that a Day has a responsibility to tell which notes it is associated with, but a Note has no corresponding ability to tell which day it is associated with

• In an implementation view, one would indicate, that Day contains pointer(s) to Note(s), but a Note would not point to any Day

Page 4: CSCI-383 Object-Oriented Programming & Design Lecture 10

Generalization

• Specializing a superclass into two or more subclasses. They must follow the isa rule

• Our idealization of inheritance is captured in a simple rule-of-thumb. Try forming the English sentences ``An A is-a B''. If it “sounds right” to your ear, then A can be made a subclass of B– A dog is-a mammal, and therefore a dog inherits from mammal – A car is-a engine sounds wrong, and therefore inheritance is not

natural but a car has-a engine

Page 5: CSCI-383 Object-Oriented Programming & Design Lecture 10

Generalization

• Represented using a small triangle pointing to the superclass– The discriminator is a label that describes the criteria used in the

specialization

Page 6: CSCI-383 Object-Oriented Programming & Design Lecture 10

Generalization

• Generalization captures similarities between several classes in a superclass. Specialization refines and adds differences in subclasses

Page 7: CSCI-383 Object-Oriented Programming & Design Lecture 10

Inheritance is both Extension and Contraction

• Because the behavior of a child class is strictly larger than the behavior of the parent, the child is an extension of the parent (larger)

• Because the child can override behavior to make it fit a specialized situation, the child is a contraction of the parent (smaller)

Page 8: CSCI-383 Object-Oriented Programming & Design Lecture 10

Avoiding Unnecessary Generalizations

Inappropriate hierarchy ofclasses, which should beinstances

Page 9: CSCI-383 Object-Oriented Programming & Design Lecture 10

Handling Multiple Discriminators

What about sharks?

Page 10: CSCI-383 Object-Oriented Programming & Design Lecture 10

Handling Multiple Discriminators

• Creating higher-level generalization

• Results in duplication of features• Another possible solution is multiple inheritance. However, it

adds too much complexity and is not supported by many OOP languages (e.g., Java, C++)

The platypus, a difficult case for single inheritance Why?Egg-laying mammal

Page 11: CSCI-383 Object-Oriented Programming & Design Lecture 10

Avoiding Having Instances Change Class

• An instance should never need to change class

• How to solve this problem?

Page 12: CSCI-383 Object-Oriented Programming & Design Lecture 10

More Advanced Features: Aggregation

• Aggregations are special associations that represent ‘part-whole’ relationships– The ‘whole’ side is often called the assembly or the aggregate – This symbol is a shorthand notation association named isPartOf

Page 13: CSCI-383 Object-Oriented Programming & Design Lecture 10

More Advanced Features: Aggregation

• Example: “A CPU is part of a computer”

• Example: “A car has an engine and doors as its parts”

Page 14: CSCI-383 Object-Oriented Programming & Design Lecture 10

When to Use an Aggregation

• As a general rule, you can mark an association as an aggregation if the following are true – You can state that

• the parts ‘are part of’ the aggregate• or the aggregate ‘is composed of’ the parts

– When something owns or controls the aggregate, then they also own or control the parts

Page 15: CSCI-383 Object-Oriented Programming & Design Lecture 10

When to Use Aggregation

• Aggregation vs. attributes– Attributes describe properties of objects (e.g., speed, price,

length)– Aggregation describe assemblies of objects

• Aggregation vs. association– Is a company an aggregation over its employees or is it an

association between its employees?

Page 16: CSCI-383 Object-Oriented Programming & Design Lecture 10

Composition

• A composition is a strong kind of aggregation – if the aggregate is destroyed, then the parts are destroyed as well

– A one-to-one composition often corresponds to a complex attribute. Two alternatives for addresses (i.e., as an attribute or as a composition)

Page 17: CSCI-383 Object-Oriented Programming & Design Lecture 10

Interfaces

• An interface is a (abstract) class with no implementation– An interface is implemented (refined) by (different) classes

– Example: A portable text editor displays its windows using a window interface that is implemented differently for Windows 95 and Mac OS

Page 18: CSCI-383 Object-Oriented Programming & Design Lecture 10

Abstract Classes

• An abstract class is a class without a (full) implementation– Some methods are deferred (i.e., they are not implemented)

– The deferred methods are implemented by subclasses only

– Example: The window move operation is implemented by using hide and show methods which are implemented by subclasses

Page 19: CSCI-383 Object-Oriented Programming & Design Lecture 10

Example Class Diagram

EXAMPLE: UML class diagram for airline reservation system (done in class)

Page 20: CSCI-383 Object-Oriented Programming & Design Lecture 10

How to Use Class Diagrams

• Class diagrams are the backbone of nearly all object-oriented methods. Especially they facilitate code generation

• The trouble with class diagrams and their rich notation is that they are extremely detailed and therefore confusing– Do not try to use all the notations available to you, if you do

not have to– Don’t draw diagrams for everything; instead concentrate on

the key areas