Download presentation
Presentation is loading. Please wait.
Published byNorah McCarthy Modified over 9 years ago
1
Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design
2
OOAD - Dr. A. Alghamdi 2 Objectives: Class Design Define the purpose of Class Design and where in the lifecycle it is performed Identify additional classes and relationships needed to support implementation of the chosen architectural mechanisms Identify and analyze state transitions in objects of state-controlled classes Refine relationships, operations, and attributes
3
OOAD - Dr. A. Alghamdi 3 Class Design in Context [Early Elaboration Iteration] [Inception Iteration (Optional)] Define a Candidate Architecture Perform Architectural Synthesis Analyze Behavior Refine the Architecture Define Components Design the Database (Optional) Class Design Designer
4
OOAD - Dr. A. Alghamdi 4 Class Design Overview Supplementary Specifications Class Design Project Specific Guidelines Design Classes
5
OOAD - Dr. A. Alghamdi 5 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Nonfunctional Requirements in General Checkpoints
6
OOAD - Dr. A. Alghamdi 6 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
7
OOAD - Dr. A. Alghamdi 7 Class Design Considerations Class stereotype Boundary Entity Control Applicable design patterns Architectural mechanisms Persistence Distribution etc.
8
OOAD - Dr. A. Alghamdi 8 A class should have a single well-focused purpose. A class should do one thing and do it well! How Many Classes Are Needed? Many, simple classes means that each class Encapsulates less of the overall system intelligence Is more reusable Is easier to implement A few, complex classes means that each class Encapsulates a large portion of the overall system intelligence Is less likely to be reusable Is more difficult to implement
9
OOAD - Dr. A. Alghamdi 9 Strategies for Designing Boundary Classes User interface (UI) boundary classes What user interface development tools will be used? How much of the interface can be created by the development tool? External system interface boundary classes Usually model as subsystem MainForm SubWindow DropDownListButton MainWindow
10
OOAD - Dr. A. Alghamdi 10 Strategies for Designing Entity Classes Entity objects are often passive and persistent Performance requirements may force some re-factoring See the Identify Persistent Classes step Analysis FatClassLazyDataHelper - rarelyUsedAttr1 - rarelyUsedAttr2 Design FatClass - privateAttr + getCommonlyUsedAttr1() + getCommonlyUsedAttr2() + getRarelyUsedAtt1() + getRarelyUsedAtt2() FatClassDataHelper - commonlyUsedAttr1 - commonlyUsedAttr2 1 0..1 FatClass - privateAttr - commonlyUsedAttr1 - commonlyUsedAttr2 - rarelyUsed1 - rarelyUsed2 >
11
OOAD - Dr. A. Alghamdi 11 Strategies for Designing Control Classes What happens to Control Classes? Are they really needed? Should they be split? How do you decide? Complexity Change probability Distribution and performance Transaction management
12
OOAD - Dr. A. Alghamdi 12 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
13
OOAD - Dr. A. Alghamdi 13 Messages displayed in interaction diagrams Other implementation dependent functionality Manager functions Need for class copies Need to test for equality Operations: Where Do You Find Them? : ClassA 1 : V/Perform Responsibility\ : ClassB : ClassA 1 : \performResponsibility (): result\ : ClassB
14
OOAD - Dr. A. Alghamdi 14 Name and Describe the Operations Create appropriate operation names Indicate the outcome Use client perspective Are consistent across classes Define operation signatures operationName([direction]parameter : class,..) : returnType Direction is in, out or inout with the default in if absent Provide short description, including meaning of all parameters
15
OOAD - Dr. A. Alghamdi 15 Guidelines: Designing Operation Signatures When designing operation signatures, consider if parameters are: Passed by value or by reference Changed by the operation Optional Set to default values In valid parameter ranges The fewer the parameters, the better Pass objects instead of “data bits”
16
OOAD - Dr. A. Alghamdi 16 Public operations Protected operations Private operations Operation Visibility Visibility is used to enforce encapsulation May be public, protected, or private
17
OOAD - Dr. A. Alghamdi 17 How Is Visibility Noted? The following symbols are used to specify export control: +Public access #Protected access -Private access Class1 - privateAttribute + publicAttribute # protectedAttribute - privateOperation () + publicOPeration () # protecteOperation ()
18
OOAD - Dr. A. Alghamdi 18 Scope Determines number of instances of the attribute/operation Instance: one instance for each class instance Classifier: one instance for all class instances Classifier scope is denoted by underlining the attribute/operation name Class1 - classifierScopeAttr - instanceScopeAttr + classifierScopeOp () + instanceScopeOp ()
19
OOAD - Dr. A. Alghamdi 19 Example: Scope Student - name - address - nextAvailID : int + addSchedule ([in] theSchedule : Schedule, [in] forSemester : Semester) + getSchedule ([in] forSemester : Semester) : Schedule + hasPrerequisites ([in] forCourseOffering : CourseOffering) : boolean # passed ([in] theCourseOffering : CourseOffering) : boolean + getNextAvailID () : int > - studentID
20
OOAD - Dr. A. Alghamdi 20 Example: Define Operations CourseOffering > Student + getTuition() : double + addSchedule ( [in] aSchedule : Schedule) + getSchedule ( [in] forSemester : Semester) : Schedule + deleteSchedule ( [in] forSemester : Semester) + hasPrerequisites ( [in] forCourseOffering : CourseOffering) : boolean # hasPassed ( [in] aCourseOffering : CourseOffering) : boolean + getNextAvailID() : int + getStudentID() : int + getName() : String + getAddress() : String > RegistrationController + submitSchedule() + saveSchedule() + getCourseOfferings() : CourseOfferingList + getCurrentSchedule ( [in] forStudent : Student, [in] forSemester : Semester) : Schedule + deleteCurrentSchedule() + new ( [in] forStudentID : String) + getStudent ( [in] anID : int) : Student > Schedule > 0..1 + registrant 0..* 1 0..1 +currentSchedule 0..* +primaryCourses 0..4 +alternateCourses 0..2 ICourseCatalogSystem + getCourseOfferings() + initialize() > 1 0..*
21
OOAD - Dr. A. Alghamdi 21 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
22
OOAD - Dr. A. Alghamdi 22 Define Methods What is a method? Describes operation implementation Purpose Define special aspects of operation implementation Things to consider: Special algorithms Other objects and operations to be used How attributes and parameters are to be implemented and used How relationships are to be implemented and used
23
OOAD - Dr. A. Alghamdi 23 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
24
OOAD - Dr. A. Alghamdi 24 Define States Purpose Design how an object’s state affects its behavior Develop statecharts to model this behavior Things to consider : Which objects have significant state? How to determine an object’s possible states? How do statecharts map to the rest of the model?
25
OOAD - Dr. A. Alghamdi 25 What is a Statechart? A directed graph of states (nodes) connected by transitions (directed arcs) Describes the life history of a reactive object State Transition Guard Condition ActionEvent State1State2 Event(args)[guard condition]/action
26
OOAD - Dr. A. Alghamdi 26 Special States Initial state The state entered when an object is created Mandatory Can only have one initial state Final state Indicates the object’s end of life Optional May have more than one State1 State2
27
OOAD - Dr. A. Alghamdi 27 Significant, dynamic attributes Existence and non-existence of certain links Identify and Define the States The maximum number of students per course offering is 10 numStudents < 10numStudents < = 10 Link to Professor Exists Link to Professor Doesn’t Exist Professor > CourseOffering > OpenClosed 0..* 0..1 AssignedUnassigned
28
OOAD - Dr. A. Alghamdi 28 Look at the class interface operations Events: addProfessor, removeProfessor Identify the Events CourseOffering > + addProfessor() + removeProfessor() 0..* 0..1 Professor >
29
OOAD - Dr. A. Alghamdi 29 Identify the Transitions For each state, determine what events cause transitions to what states, including guard conditions, when needed Transitions describe what happens in response to the receipt of an event CourseOffering > + addProfessor() + removeProfessor() 0..* 0..1 Professor > Assigned Unassigned removeProfessoraddProfessor
30
OOAD - Dr. A. Alghamdi 30 StateC Exit/someAction StateB Do/anActivity StateA Add Activities and Actions Activities Are associated with a state Start when the state is entered Take time to complete Interruptible Actions Associated with a transition Take an insignificant amount of time to complete Are non-interruptible Action Activity Entry/anAction Event[condition]/action
31
OOAD - Dr. A. Alghamdi 31 Example: Statechart addStudent / numStudents = numStudents + 1 Unassigned Assigned Full Canceled do: Send cancellation notices Committed do: Generate class roster closeRegistration [ has Professor assigned ] close / numStudents = 0 addProfessor closeRegistration removeStudent [numStudents >0]/ numStudents = numStudents - 1 cancel removeProfessor [ numStudents = 10 ] close[ numStudents < 3 ] closeRegistration[ numStudents >= 3 ] close[ numStudents >= 3 ] addStudent / numStudents = numStudents + 1 cancel removeStudent[ numStudents > 0] / numStudents = numStudents - 1 close [ numStudents = 10 ] cancel
32
OOAD - Dr. A. Alghamdi 32 Example: Statechart with Nested States and History superstate substate addStudent / numStudents = numStudents + 1 Open Unassigned Assigned H add a professor Closed Canceled do: Send cancellation notices Full Committed do: Generate class roster closeRegistration close remove a professor close[ numStudents < 3 ] [ numStudents = 10 ] closeRegistration[ numStudents >= 3 ] close[ numStudents >= 3 ] closeRegistration [ has Professor assigned ] close / numStudents = 0 removeStudent[ numStudents > 0] / numStudents = numStudents - 1 cancel
33
OOAD - Dr. A. Alghamdi 33 Which Objects Have Significant State? Objects whose role is clarified by state transitions Complex use cases that are state-controlled It is not necessary to model objects such as: Objects with straightforward mapping to implementation Objects that are not state-controlled Objects with only one computational state
34
OOAD - Dr. A. Alghamdi 34 Events may map to operations Methods should be updated with state-specific information States are often represented using attributes This serves as input into the “Define Attributes” step (Stay tuned for derived attributes) How Do Statecharts Map to the Rest of the Model? Open CourseOffering > - numStudents + addStudent() Full [numStudents=10] addStudent / numStudents = numStudents + 1
35
OOAD - Dr. A. Alghamdi 35 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
36
OOAD - Dr. A. Alghamdi 36 Attributes: How Do You Find Them? Examine method descriptions Examine states Examine any information the class itself needs to maintain
37
OOAD - Dr. A. Alghamdi 37 Attribute Representations Specify name, type, and optional default value attributeName : Type = Default Follow naming conventions of implementation language and project Type should be an elementary data type in implementation language Built-in data type, user-defined data type, or user-defined class Specify visibility Public: + Private: - Protected: #
38
OOAD - Dr. A. Alghamdi 38 Derived Attributes What is a derived attribute? An attribute whose value may be calculated based on the value of other attribute(s) When do you use it? When there is not enough time to re-calculate the value every time it is needed When you must trade-off runtime performance versus memory required
39
OOAD - Dr. A. Alghamdi 39 Example: Define Attributes RegistrationController > 0..1 Student > - name - address - nextAvailID : int - studentID : int - dateOfBirth : Date ICourseCatalogSystem > Schedule > CourseOffering > - number : String = “100” - startTime : Time - endTime : Time - day : String - /numStudents : int = () - semester : Semester 0..1 + registrant+ currentSchedule 0..* 0..40..2 0..* + alternateCourses 1 0..* + primaryCourses
40
OOAD - Dr. A. Alghamdi 40 (continued) Exercise 1: Class Design Given the following: The architectural layers, their packages, and their dependencies Design classes for a particular use case
41
OOAD - Dr. A. Alghamdi 41 Exercise 1: Class Design (cont.) Identify the following: Attributes, operations, and their complete attribute signatures Attribute and operation scope and visibility Any additional relationships and/or classes to support the defined attributes and attribute signatures Class(es) with significant state- controlled behavior The important states and transitions for the identified class (continued)
42
OOAD - Dr. A. Alghamdi 42 Produce the following: Design Use-Case Realization Statechart for one of the classes that exhibits state-controlled behavior Class diagram (VOPC) that includes all operations, operation signatures, attributes, and attribute signatures Exercise 1: Class Design
43
OOAD - Dr. A. Alghamdi 43 Exercise 1: Review Compare your results Is the name of each operation descriptive and understandable? Does the name of the operation indicate its outcome? Does each attribute represent a single conceptual thing? Is the name of each attribute descriptive and does it correctly convey the information it stores? Is the state machine understandable? Do state names and transitions reflect the context of the domain of the system? Does the state machine contain any superfluous states or transitions? Payroll System
44
OOAD - Dr. A. Alghamdi 44 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
45
OOAD - Dr. A. Alghamdi 45 What Is a Dependency? A relationship between two objects Purpose Determine where structural relationships are NOT required Things to look for : What causes the supplier to be visible to the client Define Dependency SupplierClient
46
OOAD - Dr. A. Alghamdi 46 Dependencies vs. Associations Associations are structural relationships Dependencies are non- structural relationships In order for objects to “know each other” they must be visible Local variable reference Parameter reference Global reference Field reference Association Dependency Supplier2 Client Supplier1
47
OOAD - Dr. A. Alghamdi 47 Associations vs. Dependencies in Collaborations An instance of an association is a link All links become associations unless they have global, local, or parameter visibility Relationships are context-dependent Dependencies are transient links with: A limited duration A context-independent relationship A summary relationship A dependency is a secondary type of relationship in that it doesn't tell you much about the relationship. For details you need to consult the collaborations.
48
OOAD - Dr. A. Alghamdi 48 Local Variable Visibility The op1() operation contains a local variable of type ClassB Class Diagram Collaboration Diagram ClassA:ClassA + op1 ( ) ClassB :ClassB L
49
OOAD - Dr. A. Alghamdi 49 Parameter Visibility The ClassB instance is passed to the ClassA instance Class Diagram Collaboration Diagram ClassA:ClassA + op1 ( [in] aParam : ClassB ) ClassB :ClassB P
50
OOAD - Dr. A. Alghamdi 50 Global Visibility The ClassUtility instance is visible because it is global Class Diagram Collaboration Diagram ClassA:ClassA + op1 ( ) ClassB :ClassB G + utilityOp ( )
51
OOAD - Dr. A. Alghamdi 51 Permanent relationships — Association (field visibility) Transient relationships — Dependency Multiple objects share the same instance Pass instance as a parameter (parameter visibility) Make instance a managed global (global visibility) Multiple objects don’t share the same instance (local visibility) How long does it take to create/destroy? Expensive? Use field, parameter, or global visibility Strive for the lightest relationships possible Identifying Dependencies: Considerations
52
OOAD - Dr. A. Alghamdi 52 Example: Define Dependencies (before) ICourseCatalogSystem + getCourseOfferings ( [in] forSemester : Semester) : CourseOfferingList > Student - name - address - StudentID : int + addSchedule ( [in] aSchedule : Schedule ) + getSchedule ( [in] forSemester : Semester ) : Schedule + hasPrerequisites ( [in] forCourseOffering : CourseOffering ) : boolean # passed ( [in] aCourseOffering : CourseOffering ) : boolean > RegistrationController + // submit schedule () + // save schedule () + // create schedule with offerings () + // get course offerings () > 0..1 + registrant 0..*1 + courseCatalog Schedule - semester : Semester + submit () + //save () # any conflicts? () + //create with offerings() > 0..* 1 0..1 + currentSchedule CourseOffering - number : String = "100" - startTime : Time - endTime : Time - day : String + addStudent ( [in] aStudentSchedule : Schedule) + removeStudent ( [in] aStudentSchedule : Schedule) + new () + setData () > 0..* 0..4 + primaryCourses 0..* 0..2 alternateCourses
53
OOAD - Dr. A. Alghamdi 53 Example: Define Dependencies (after) ICourseCatalogSystem + getCourseOfferings ( [in] forSemester : Semester) : CourseOfferingList > RegistrationController + // submit schedule () + // save schedule () + // create schedule with offerings () + // get course offerings () > 0..1 + registrant Schedule - semester : Semester + submit () + //save () # any conflicts? () + //create with offerings() > 0..* 0..1 + currentSchedule CourseOffering - number : String = "100" - startTime : Time - endTime : Time - day : String + addStudent ( [in] aStudentSchedule : Schedule) + removeStudent ( [in] aStudentSchedule : Schedule) + new () + setData () > 0..* 0..4 + primaryCourses 0..* 0..2 alternateCourses Global visibility Parameter visibility Field visibility Field visibility Student - name - address - StudentID : int + addSchedule ( [in] aSchedule : Schedule ) + getSchedule ( [in] forSemester : Semester ) : Schedule + hasPrerequisites ( [in] forCourseOffering : CourseOffering ) : boolean # passed ( [in] aCourseOffering : CourseOffering ) : boolean > 0..1 1
54
OOAD - Dr. A. Alghamdi 54 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
55
OOAD - Dr. A. Alghamdi 55 Define Associations Purpose Refine remaining associations Things to look for : Association vs. Aggregation Aggregation vs. Composition Attribute vs. Association Navigability Association class design Multiplicity design
56
OOAD - Dr. A. Alghamdi 56 What Is Composition? A form of aggregation with strong ownership and coincident lifetimes The parts cannot survive the whole/aggregate Whole Composition Part Whole
57
OOAD - Dr. A. Alghamdi 57 Shared Aggregation Non-shared Aggregation Aggregation: Shared vs. Non-shared By definition, composition is non-shared aggregation WholePart 1..*0..* Multiplicity > 1 Multiplicity = 1 1 WholePart 10..* WholePart 0..* Composition
58
OOAD - Dr. A. Alghamdi 58 Aggregation or Composition? Consideration Lifetimes of Class1 and Class2 aggregation composition Class1Class2 Class1Class2
59
OOAD - Dr. A. Alghamdi 59 Example: Composition 1 0..* ScheduleStudent RegisterForCoursesForm 1 1 RegistrationController
60
OOAD - Dr. A. Alghamdi 60 Attributes Vs Composition Use composition when Properties need independent identities Multiple classes have the same properties Properties have a complex structure and properties of their own Properties have complex behavior of their own Properties have relationships of their own Otherwise use attributes
61
OOAD - Dr. A. Alghamdi 61 Example: Attributes vs. Composition Composition of separate class Attribute Student - name - address - nextAvailID : int - StudentID : int - dateofBirth : Date + addSchedule () + getSchedule () + delete Schedule () + hasPrerequisites () # hasPassed () > Schedule + submit () + //save () # any conflicts? () + //create with offerings () + new () + passed () > - semester : Semester 0..* 1
62
OOAD - Dr. A. Alghamdi 62 Review: What Is Navigability? Indicates that it is possible to navigate from a associating class to the target class using the association RegistrationController > CourseOffering > Schedule >
63
OOAD - Dr. A. Alghamdi 63 Navigability: Which Directions Are Really Needed? Explore interaction diagrams Even when both directions seem required, one may work Navigability in one direction is infrequent Number of instances of one class is small ? 0..40..* + primaryCourses CourseOfferingSchedule > 0..40..* + primaryCourses CourseOfferingSchedule > 0..40..* + primaryCourses CourseOfferingSchedule >
64
OOAD - Dr. A. Alghamdi 64 Example: Navigability Refinement Total number of Schedules is small, or Never need a list of the Schedules on which the CourseOffering appears Total number of CourseOfferings is small, or Never need a list of CourseOfferings on a Schedule Total number of CourseOfferings and Schedules are not small Must be able to navigate in both directions 0..40..* + primaryCourses CourseOfferingSchedule > 0..40..* + primaryCourses CourseOfferingSchedule > 0..40..* + primaryCourses CourseOfferingSchedule >
65
OOAD - Dr. A. Alghamdi 65 Association Class A class is “attached” to an association Contains properties of a relationship Has one instance per link ScheduleOfferingInfo - status + // is selected () + // mark as selected () + // mark as cancelled () > CourseOffering > Schedule > 0..* 0..4 + primaryCourses + alternateCourses 0..* 0..2 PrimaryScheduleOfferingInfob - grade + // is enrolled in? () + // mark as enrolled in () + // mark as committed () >
66
OOAD - Dr. A. Alghamdi 66 Example: Association Class Design Design Decisions CourseOffering > Schedule > 0..* 0..4 + primaryCourses + alternateCourses 0..* 0..2 PrimaryScheduleOfferingInfob - grade + // is enrolled in? () + // mark as enrolled in () + // mark as committed () > CourseOffering > Schedule > 1 0..* - theCourseOffering + alternateCourses 0..* 0..2 PrimaryScheduleOfferingInfob - grade + // is enrolled in? () + // mark as enrolled in () + // mark as committed () > - primaryCourseOfferingInfo 0..4 1
67
OOAD - Dr. A. Alghamdi 67 Multiplicity = 1, or Multiplicity = 0..1 May be implemented directly as a simple value or pointer No further “design” is required Multiplicity > 1 Cannot use a simple value or pointer Further “design” may be required Multiplicity Design Needs a container for CourseOfferings 0..10..* > ProfessorCourseOffering + Instructor 0..10..* > ProfessorCourseOffering + Instructor
68
OOAD - Dr. A. Alghamdi 68 Multiplicity Design Options Explicit Modeling of a Container ClassDetail Container via Note 0..10..* > ProfessorCourseOffering + Instructor 0..10..* > Professor CourseOfferingList + Instructor > CourseOffering 0..10..* > Professor + Instructor > CourseOffering List
69
OOAD - Dr. A. Alghamdi 69 What is a Parameterized Class (template)? A class definition that defines other classes Often used for container classes Some common container classes: Sets, lists, dictionaries, stacks, queues ParameterizedClass Formal Arguments List Item
70
OOAD - Dr. A. Alghamdi 70 Instantiating a Parameterized Class ParameterizedClass Formal Arguments InstantiatedClass ActualArgument > (ActualArgument)
71
OOAD - Dr. A. Alghamdi 71 Example: Instantiating a Parameterized Class Before After List Item CourseOfferingList ActualArgument > (CourseOffering) > 1 0..* CourseOfferingList CourseOffering > 1 0..*
72
OOAD - Dr. A. Alghamdi 72 Multiplicity Design: Optionality If a link is optional, make sure to include an operation to test for the existence of the link ProfessorCourseOffering + isTeaching () : boolean 0..1 0..* + hasProfessor () : boolean
73
OOAD - Dr. A. Alghamdi 73 Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints Class Design Steps
74
OOAD - Dr. A. Alghamdi 74 Review: Generalization One class shares the structure and/or behavior of one or more classes “Is a kind of” relationship In Analysis, use sparingly Superclass (Parent) (Ancestor) Generalization Relationship Subclasses (Child) (Descendants) Account + balance + name + number + withdraw () + createStatement () Checking Savings + getInterest ()
75
OOAD - Dr. A. Alghamdi 75 There are no direct instances of Animal LionTiger Animal + communicate () Abstract and Concrete Classes Abstract classes cannot have any objects Concrete classes can have objects All objects are either lions or tigers Abstract class Abstract operation Communication Discriminator
76
OOAD - Dr. A. Alghamdi 76 Name clashes on attributes or operations Repeated inheritance Multiple Inheritance: Problems Resolution of these problems is implementation-dependent AnimateObject Bird Animal + color + getColor () FlyingThing + color + getColor () Bird Animal + color + getColor () FlyingThing + color + getColor ()
77
OOAD - Dr. A. Alghamdi 77 Generalization Constraints Complete End of the inheritance tree Incomplete Inheritance tree may be extended Disjoint Subclasses mutually exclusive Doesn’t support multiple inheritance Overlapping Subclasses are not mutually exclusive Supports multiple inheritance
78
OOAD - Dr. A. Alghamdi 78 Example: Generalization Constraints Asset Real EstateBank AccountSecurity Savings Checking StockBond {disjoint} {disjoint,complete} {disjoint} End of inheritance hierarchy Multiple Inheritance not supported
79
OOAD - Dr. A. Alghamdi 79 Example: Generalization Constraints (cont.) Vehicle AmphibiousVehicle WaterVehicleLandVehicle {overlapping} Multiple inheritance supported
80
OOAD - Dr. A. Alghamdi 80 Is this correct? Generalization vs. Aggregation Generalization and aggregation are often confused Generalization represents an “is a” or “kind-of” relationship Aggregation represents a “part-of” relationship Window WindowWithScrollbar Scrollbar
81
OOAD - Dr. A. Alghamdi 81 Window WindowWithScrollbar Scrollbar Generalization vs. Aggregation Scrollbar Window WindowWithScrollbar 1 1 A WindowWithScrollbar “is a” Window A WindowWithScrollbar “contains a” Scrollbar
82
OOAD - Dr. A. Alghamdi 82 Do these classes follow the “is a” style of programming? Generalization: Share Common Properties and Behavior Follows the “is a” style of programming Class substitutability List + insertTop ([in] item) + insertBottom ([in] item) + removeTop () + removeBottom () + insert ([in] item, [in] position) Stack LionTiger Animal + communicate ()
83
OOAD - Dr. A. Alghamdi 83 Generalization: Share Common Properties and Behavior (cont.) List + insertTop ([in] item) + insertBottom ([in] item) + removeTop () + removeBottom () + insert ([in] item, [in] position) Stack LionTiger Animal + communicate ()
84
OOAD - Dr. A. Alghamdi 84 Generalization: Share Implementation: Factoring Supports the reuse of the implementation of another class Cannot be used if the class you want to “reuse” cannot be changed List + insertTop ([in] item) + insertBottom ([in] item) + removeTop () + removeBottom () + insert ([in] item, [in] position) Stack SequentialContainer List + insertTop ([in] item) + removeTop () Stack + insertBottom ([in] item) + removeBottom () + insert ([in] item, [in] position)
85
OOAD - Dr. A. Alghamdi 85 Generalization Alternative: Share Implementation: Delegation Supports the reuse of the implementation of another class Can be used if the class you want to “reuse” cannot be changed List + insertTop ([in] item) + insertBottom ([in] item) + removeTop () + removeBottom () + insert ([in] item, [in] position) StackList Stack + insertBottom ([in] item) + removeBottom () + insert ([in] item, [in] position) 1 1
86
OOAD - Dr. A. Alghamdi 86 push() and pop() can access methods of List but instances of Stack cannot Implementation Inheritance Ancestor public operations, attributes, and relationships are NOT visible to clients of descendent class instances Descendent class must define all access to ancestor operations, attributes, and relationships List + insertTop ([in] item) + insertBottom ([in] item) + removeTop () + removeBottom () + insert ([in] item, [in] position) Stack >
87
OOAD - Dr. A. Alghamdi 87 Manufacturer A Manufacturer B Manufacturer C OO Principle: Encapsulation Review: What Is Polymorphism? The ability to hide many different implementations behind a single interface
88
OOAD - Dr. A. Alghamdi 88 Generalization: Implement Polymorphism Without Polymorphism if animal = “Lion” then Lion communicate else if animal = “Tiger” then Tiger communicate end With Polymorphism Animal communicate LionTiger Animal + communicate ()
89
OOAD - Dr. A. Alghamdi 89 Polymorphism: Use of Interfaces vs. Generalization Interfaces support implementation-independent representation of polymorphism Realization relationships can cross generalization hierarchies Interfaces are pure specifications, no behavior Abstract base class may define attributes and associations Interfaces are totally independent of inheritance Generalization is used to re-use implementations Interfaces are used to re-use behavioral specifications Generalization provides a way to implement polymorphism
90
OOAD - Dr. A. Alghamdi 90 Polymorphism via Generalization Design Decisions Provide interface only to descendant classes? Design ancestor as an abstract class All methods are provided by descendent classes Provide interface and default behavior to descendent classes? Design ancestor as a concrete class with a default method Allow polymorphic operations Provide interface and mandatory behavior to descendent classes? Design ancestor as a concrete class Do not allow polymorphic operations
91
OOAD - Dr. A. Alghamdi 91 Metamorphosis exists in the real world. How should it be modeled? What Is Metamorphosis? Metamorphosis 1. A change in form, structure, or function; specifically the physical change undergone by some animals, as of the tadpole to the frog. 2. Any marked change, as in character, appearance, or condition. ~ Webster’s New World Dictionary, Simon & Schuster, Inc., 1979
92
OOAD - Dr. A. Alghamdi 92 Example: Metamorphosis In the university, there are full-time students and part-time students Full-time students have an expected graduation date but part-time students do not Part-time students may take a maximum of three courses but there is no maximum for full-time students FullTimeStudent + name + address + studentID + gradDate PartTimeStudent + name + address + maxNumCourses + studentID
93
OOAD - Dr. A. Alghamdi 93 What happens if a part-time student becomes a full-time student? Modeling Metamorphosis: One Approach A generalization relationship may be created FullTimeStudent Student + name + address + studentID + gradDate PartTimeStudent + maxNumCourses
94
OOAD - Dr. A. Alghamdi 94 Inheritance may be used to model common structure, behavior, and/or relationships to the “changing” parts Modeling Metamorphosis: Another Approach Student + name + address + studentID FullTimeStudent + gradDate PartTimeStudent + maxNumCourses Student + name + address + studentID FullTimeClassification + gradDate PartTimeClassification + maxNumCourses Classification 1 1
95
OOAD - Dr. A. Alghamdi 95 Modeling Metamorphosis: Another Approach (cont.) Metamorphosis is accomplished by the object “talking” to the changing parts : Student : FullTimeClassification : StudentManager : PartTimeClassification 2 : \delete\ 3 : \create\ 1 : \changeToFullTime\
96
OOAD - Dr. A. Alghamdi 96 Metamorphosis and Flexibility This technique also adds to the flexibility of the model studentID 1 1 ResidentInformation 1 0..1 Classification ParttimeClassification + maxNumCourses FulltimeClassification + gradDate + dorm + room + roomKeyID Student + name + address + studentID
97
OOAD - Dr. A. Alghamdi 97 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
98
OOAD - Dr. A. Alghamdi 98 Resolve Use-Case Collisions Multiple use cases may simultaneously access design objects Options Use synchronous messaging => first-come first- serve order processing Identify operations (or code) to protect Apply access control mechanisms Message queuing Semaphores (or “tokens”) Other locking mechanism Resolution is highly dependent on implementation environment
99
OOAD - Dr. A. Alghamdi 99 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
100
OOAD - Dr. A. Alghamdi 100 Handle Non-Functional Requirements in General Analysis ClassAnalysis Mechanism(s) Student Schedule CourseOffering Course RegistrationController Persistency, Security Persistency, Legacy Interface Distribution Persistency, Security Design Guidelines AnalysisDesignImplementation Remote Method Invocation (RMI) Persistency Analysis Mechanism (Conceptual) Design Mechanism (Concrete) Implementation Mechanism (Actual) OODBMS RDBMS JDBC ObjectStore Java 1.2 from Sun Legacy Data New Data Distribution Persistency SomeDesignClass
101
OOAD - Dr. A. Alghamdi 101 Class Design Steps Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints
102
OOAD - Dr. A. Alghamdi 102 Checkpoints: Classes Clear class names One well-defined abstraction Functionally coupled attributes/behavior Generalizations were made. All class requirements were addressed Demands are consistent with statecharts. Complete class instance life cycle is described. The class has the required behavior.
103
OOAD - Dr. A. Alghamdi 103 Checkpoints: Operations Operations are easily understood State description is correct Required behavior is offered Parameters are defined correctly Messages are completely assigned operations Implementation specifications are correct Signatures conform to standards All operations are needed by Use-Case Realizations
104
OOAD - Dr. A. Alghamdi 104 Checkpoints: Attributes A single concept Descriptive names All attributes are needed by Use- Case Realizations
105
OOAD - Dr. A. Alghamdi 105 Checkpoints: Relationships Descriptive role names Correct multiplicities
106
OOAD - Dr. A. Alghamdi 106 Review: Class Design What is the purpose of Class Design? In what ways are classes refined? Are statecharts created for every class? What are the major components of a statechart? Provide a brief description of each. What kind of relationship refinements occur? What is the difference between an association and a dependency? What is done with operations and attributes?
107
OOAD - Dr. A. Alghamdi 107 (continued) Exercise 2: Class Design Given the following: The Use-Case Realization for a use case and/or the detailed design of a subsystem The design of all participating design elements
108
OOAD - Dr. A. Alghamdi 108 Exercise 2: Class Design (cont.) Identify the following: The required navigability for each relationship Any additional classes to support the relationship design Any associations refined into dependencies Any associations refined into aggregations or compositions Any refinements to multiplicity Any refinements to existing generalizations Any new applications of generalization Make sure any metamorphosis is considered (continued)
109
OOAD - Dr. A. Alghamdi 109 Exercise 2: Class Design (cont.) Produce the following: An updated VOPC, including the relationship refinements (generalization, dependency, association) (continued)
110
OOAD - Dr. A. Alghamdi 110 Exercise 2: Review Compare your results Do your dependencies represent context independent relationships? Are the multiplicities on the relationships correct? Does the inheritance structure capture common design abstractions, and not implementation considerations? Is the obvious commonality reflected in the inheritance hierarchy? Payroll System
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.