This chapter presents a set of criteria, activities, and techniques used to design classes and methods. Together they are used to ensure the object-oriented design communicates how the system needs to be coded.
Trang 1PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Chapter 9:
Class and Method Design
Trang 2PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Objectives
• Become familiar with coupling, cohesion, and connascence.
• Be able to specify, restructure, and
optimize object designs.
• Be able to identify the reuse of predefined classes, libraries, frameworks, and
Trang 3PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
BASIC CHARACTERISTICS OF OBJECT ORIENTATION
Trang 4PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Trang 5PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Encapsulation & Info Hiding
• Hiding the content of the object from
outside view
• Communication only through object’s methods
• Key to reusability
Trang 6PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Polymorphism & Dynamic
Trang 7PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Polymorphism Example
Trang 8PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
• Most inheritance conflicts are
due to poor classification
Trang 9PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Trang 10PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
DESIGN CRITERIA
Trang 11PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Coupling
• Interdependency among modules
• Interaction coupling through message passing
Trang 12PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Trang 13PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Types of Interactive Coupling
Trang 14PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Cohesion
• “Single-mindedness of a module”
• Method cohesion
• Class cohesion
Trang 15PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Types of Method Cohesion
Trang 16PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Types of Class Cohesion
Trang 17PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Connascence
• Creating the need to change another module as a result of changing one
– Minimize overall connascence
– Minimize across encapsulation boundaries – Maximize within encapsulation boundary
Trang 18PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Types of Connascence
Trang 19PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
OBJECT DESIGN ACTIVITIES
Trang 20PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Additional Specification
• First review the current set of models
– Sufficient but only necessary classes to solve problem
– No missing attributes or methods
– No extraneous attributes or methods
• Examine visibility
Trang 21PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Signatures for each method
• Name of the method
• Parameters or arguments to pass
• Type of value the method will return to the calling method
Trang 22PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Trang 23PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Identify Opportunities for Reuse
• Patterns
• Framework
• Class libraries
• Components
Trang 24PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Restructure the Design
• Factoring
• Map design to current language
• Normalization
• Assure all inheritance relationships
support only generalization/specialization semantics
Trang 25PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Optimizing the Design
• Review access paths
• Review attributes of each class
• Review direct and indirect fan-out
• Consider execution order of statements in often-used methods
• Avoid re-computation by creating derived attributes and triggers
Trang 26PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
CONSTRAINTS AND
CONTRACTS
Trang 27PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Trang 28PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Invariants on a Class Diagram
Trang 29PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Contracts
• Contracts document message passing
between object
• A contract is created for each method that
is publicly visible in a class
• Should contain enough information for the programmer to understand what the
method is supposed to do
Trang 30PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Elements of a Contract
Trang 31PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Sample Contract Form
Trang 32PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
METHOD SPECIFICATION
Trang 33PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Method Specification Syntax
• No formal syntax specification
• General information
• Events
• Message Passing
• Algorithm Specification
Trang 34PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Structured English
Trang 35PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Return
Trang 36PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition
Copyright © 2009 John Wiley & Sons, Inc All rights reserved
Summary
• Basic Characteristics of Object Orientation
• Design Criteria
• Object Design Activities
• Constraints and Contracts
• Method Specification