40
Slide 1 Chapter 10 Class and Method Design

Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

  • View
    224

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 1

Chapter 10Class and Method Design

Page 2: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 2

REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Page 3: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 3

Levels of Abstraction

Package

Page 4: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 4

Elements

ClassesObjectsAttributesStatesMethodsMessages

Page 5: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 5

Encapsulation

Hiding the content of the object from outside viewCommunication only through object’s methodsKey to reusability

Page 6: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 6

Polymorphism

Same message triggers different methods in different objectsDynamic binding means specific method is selected at run timeImplementation of dynamic binding is language specificNeed to be very careful about run time errorsNeed to ensure semantic consistency

Page 7: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 7

Inheritance

Single inheritance -- one parent classMultiple inheritance -- multiple parent classesRedefinition and inheritance conflictMost inheritance conflicts are due to poor classification

Page 8: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 8

Rumbaugh’s Rules

Query operations should not be redefined

Methods that redefine inherited ones should only restrictthe semantics of the inherited ones

The underlying semantics of the inherited method should never be changed

The signature (argument list) of the inherited method shouldnever be changed

Page 9: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 9

Additional Inheritance Conflicts

Two inherited attributes or methods have same name and semanticsTwo inherited attributes or methods have different name, but same semanticsTwo inherited attributes or methods have same name and different semantics

Page 10: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 10

Inheritance Conflicts with Multiple Inheritance

Page 11: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 11

Design Criteria

Page 12: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 12

Coupling

Indicates the interdependence or interrelationships of the modulesInteraction coupling

Relationships with methods and objects through message passage

Inheritance couplingHow tightly coupled are the classes in the inheritance hierarchy?

Page 13: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 13

Interaction Coupling

Page 14: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Cohesion

Slide 14

Determines how single-minded a module (class, object, or method) is within a systemMethod Cohesion

Methods should do one and only one thing

Class CohesionLevel of cohesion between the attributes and methods of a class

Page 15: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 15

Types of Method Cohesion

Page 16: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 16

Ideal Class Cohesion

Contain multiple methods that are visible outside the classHave methods that refer to attributes or other methods defined with the class or its superclassNot have any control-flow coupling between its methods

Page 17: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 17

Types of Class Cohesion

Page 18: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 18

Connascence

Two modules (classes or methods) are so intertwined, that if you make a change in one, it is likely that a change in the other will be requiredGeneralizes the idea of cohesion and coupling

Page 19: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 19

Connascence and Encapsulation Levels

Minimize overall connascence by eliminating any unnecessary connascence throughout the system,Minimize connascence across any encapsulation boundaries, such as method boundaries and class boundaries, Maximize connascence within any encapsulation boundary.

Page 20: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 20

Types of Connascence

Page 21: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 21

Object Design Activities

1. Adding Specification2. Identifying opportunities for reuse3. Restructuring the design4. Optimizing the design5. Mapping problem classes to Implementation

Languages

Page 22: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 22

1. Additional SpecificationEnsure the classes are both necessary and sufficient for the problemFinalize the visibility of the attributes and methods of each classDetermine the signature of every method of each classDefine constraints to be preserved by objects

Page 23: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 23

2. Identifying Opportunities for Reuse

Analysis patternsProblem domain representations

Design patternsGrouping of collaborating classes that provide a solution to a commonly occurring problem

FrameworksSet of implemented classes that can be used as a basis for implementing an application. E.g: CORBA, DCOM

LibrariesSet of class libraries

ComponentsSelf-contained, encapsulated piece of software that can be plugged-in

Page 24: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 24

Sample Design Pattern(Forwarder-Receiver Pattern)

Page 25: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 25

3. Restructuring the Design

Factoring Separate aspects of a method or class into a new method or class

NormalizationIdentifies classes missing from the design

Challenge inheritance relationships to ensure they only support a generalization/specialization semantics

Page 26: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 26

4. Optimizing the Design

Review access paths between objectsReview each attribute of each classReview fan-out of each methodExamine execution order of statementsCreate derived activities

Page 27: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 27

5. Map Problem Domain Classes to Implementation Languages

Single-Inheritance LanguageConvert relationships to association relationshipsFlatten inheritance hierarchy by copying attributes and methods of additional superclass(es)

Page 28: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 28

Implement in Object-Based Language

Factor out all uses of inheritance from the problem domain class design

Page 29: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 29

Implement in a Traditional Language

Stay away from traditional languages to implement O-O designBut if necessary, factor out all uses of

PolymorphismDynamic bindingEncapsulationInformation hiding

Page 30: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 30

Constraints and Contracts

Page 31: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 31

Types of Constraints

Pre-ConditionsA constraint to be met to allow a method to execute

Post-conditionA constraint to be met after a method executes

InvariantsConstraints that must be true for all instances of a class

Page 32: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 32

Invariants

Page 33: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 33

Elements of a Contract

Page 34: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 34

Method Specification

Page 35: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 35

Method Specification

General informationEventsMessage passingAlgorithm specification

Structured EnglishPseudocodeUML activity diagram

Page 36: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 36

Applying the Concepts at CD Selections

Page 37: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 37

Revised CD Selections Class Diagram

Page 38: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 38

Back of CD CRC Card

Page 39: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 39

Get Review Method Contract

Page 40: Slide 1 Chapter 10 Class and Method Design. Slide 2 REVISITING THE BASIC CHARACTERISTICS OF OBJECT-ORIENTATION

Slide 40

Revised Package Diagram