This chapter presents a set of criteria, activities, and techniques used to design classes and methods. Together they are used to ensure the objectoriented design communicates how the system needs to be coded.
Trang 1Chapter 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
• Be able to identify the reuse of predefined
classes, libraries, frameworks, and
components.
• Be able to specify constraints and contracts.
• Be able to create a method specification.
Trang 3BASIC 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 5Encapsulation & 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 Binding
• Same message triggers different methods in different objects
• Dynamic binding means specific method is
selected at run time
• Implementation of dynamic binding is
language specific
• Need to be very careful about run time errors
• Need to ensure semantic consistency
Trang 7Polymorphism 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
Trang 9Rumbaugh’s Rules
1 Query operations should not be redefined
2 Methods that redefine inherited ones
should only restrict the semantics of the inherited ones
3 The underlying semantics of the inherited
method should never be changed
4 The signature (argument list) of the
inherited method should never be
changed
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 11• 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
Law of Demeter
Messages should be sent only by an object:
to itself
to objects contained in attributes of itself or a superclass
to an object that is passed as a parameter to the method
to an object that is created by the method
to an object that is stored in a global variable
Trang 13Types 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 15Types 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 17• 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 19OBJECT 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 21Signatures 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 23Identify 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
Trang 25Optimizing 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 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 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 31Sample 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 33Method 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 35Pseudocode Example
(Get CD-info module)
Accept (CD_title) {Required}
Accept (CD_artist) {Required}
Accept (CD_category) {Required} Accept (CD_length)
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