Software Engineering Dr. K. T. Tsang Lecture 7 Advanced class modeling http://www.uic.edu.hk/~kentsang/SWE/SWE.htm 1 Multiplicity • Multiplicity - a constraint on cardinality of a set • Multiplicity also applies to attributes • Multiplicity for an attribute specifies the number of possible values for each instantiation of an attribute Person name: string [1] birthday: date [1] phoneNumber: string [*] address: string [1..*] 2 Scope & visibility of features • Scope – whether a feature applies to an object (object scope) or an entire class (static, class scope) • Visibility – public(+), protected(#) or private() 3 Scope – example 1 PhoneMessage maxDuration maxDaysRetained dateRecord timeRecorded priority message hasBeenReceived * * {ordered} source 0..1 PhoneMailbox maxMsgCount 1 phoneNumber owner passWord greeting * owner 1 Person Not a good model name 4 Scope – example 2 Preferred model Person name 1 categoryName msgMaxDuration msgDaysRetained msgMaxCount 1 owner PhoneMessage * dateRecord timeRecorded priority {ordered} message * hasBeenReceived * MailCategory * 0..1 PhoneMailbox source phoneNumber passWord owner greeting 1 5 N-ary Associations • • • • Binary Associations – 2 ends Ternary Associations – 3 ends N-ary Associations – n ends, avoid them Most N-ary Associations can be decomposed into binary associations 6 Restating an N-ary Association • Decompose non-atomic ternary into binary associations stock Person * name Company * name * Purchase quantity date cost Person name 1 * Purchase quantity date cost * stock 1 Company name 7 Example - A genuine ternary association p.65 B&R, Fig.4.6 An atomic n-ary association is one that cannot be subdivided into binary associations without losing information. Project name Language * * name programmer Person * name Class diagram 8 Example - A genuine ternary association.. P135:Project Java:Language name=“P135” name=“Java” Mary:Person name=“Mary” library:Project name=“library” C:Language name=“C” object diagram 9 Example 2 - A ternary association p.65 B&R, Fig. 4.7 Professor * Semester DeliveredCourse * roomNumber * Course * * TextBook A class associates with an association? 10 Example 2 – An n-ary association Professor * DeliveredCourse * Semester roomNumber * * Course TextBook A better way?? 11 Promoting an n-ary association to classes Professor A regular class * Semester DeliveredCourse * roomNumber * TextBook * Course Programming languages cannot express n-ary association, so we must promote them to class. 12 Aggregation • Aggregation is a special form of association in which an aggregate object is made of constituent parts (is a part of). • An aggregate object is an extended object treated as a unit in many operation. • Properties of Aggregation – Transitive: if A is part of B and B is part of C, then A is also part of C – Anti-symmetric: if A is part of B then B is not part of A 13 Aggregation - example UML symbol-Hollow diamond * 1 Engine Auto-mobile * * 1 * Body Wheel * * Seat 14 Aggregation & Composition • There are 2 forms of part-whole relationships in UML. • The general form is Aggregation. • The more restricted form is Composition: – a part can belong to at most to one assembly – once assigned, the part has the same lifetime as the assembly • Deletion of the assembly object triggers the deletion of all objects in the composition. 15 Composition - example UML symbol- solid diamond 1 Company * Division 1 * Department 1 * Person 16 Propagation of operations • The automatic application of an operation to a group of objects when the operation is applied to a starting object. • Examples – – Moving an aggregate moves its parts – Copying a document copies all its paragraphs and all the characters in a paragraph 17 Propagation of operations Document copy 1 copy * Paragraph copy 1 copy * Character copy * owns 1 Person The copy operation propagates from document to paragraphs to characters, but not in the reverse direction. p.68 B&R, Fig.4.11 18 Abstract & concrete classes • An abstract class has no direct instance but its descendant classes have direct instances. • A concrete class is a class that can have direct instances (instantiable). • Abstract class can have abstract operation, designated by italics or the keyword {abstract}. • An abstract operation defines the signature of an operation for which each concrete subclass may provide its own implementation. • A concrete class may not contain any abstract operation. 19 Abstract operation - example Employee yearTodateEarnings computePay FullTimeEmployee PartTimeEmployee weeklyRate hourlyRate computePay computePay Note: Avoid concrete super-class 20 Multiple inheritance • Allows a class to have more than one superclass, and inherit features from all parents. • Some OOP languages does not support multiple inheritance, e.g. Java. • Multiple inheritance may create ambiguities. Be careful with its usage. 21 Multiple inheritance - example Employee yearTodateEarnings computePay FullTimeEmployee PartTimeEmployee Partner weeklyRate hourlyRate ownership computePay computePay computePay FullTimeEmployeePartner 22 Multiple classification • An object is an instance of all ancestors of its class. 23 Multiple classification - example Person 1 * UniversityMember {overlapping} Staff Faculty Student Instructor Overlapping means a person may belong to more than one kind of UniversityMember 24 Delegation using composition of parts p.74 B&R, Fig. 4.19 1 1 Employee 1 1 Worker employmentStatus FullTime Management managerialStatus PartTime Manager Director Workaround for Multiple inheritance: recast a superclass with multiple inheritance as a composition in which each part replaces a subclass (generalization). 25 Workaround 2 – nested generalization Employee FullTime FullTimeManager FullTimeWorker PartTime PartTimeManager PartTimeWorker 26 Metadata • Metadata is data that describes other data. • Example: A class definition is metadata. • Class is meta-object. Class that describes other class is meta-class. CarModel 1 modelName year basePrice * describes PhysicalCar serialNumber color options * * 1 manufacturer 1 Company owner Person 27 Reification • Reification is the promotion of something that is not an object to an object. Substance substanceName Reification: Promote attribute to a class Substance * alias 1..* SubstanceName substanceName 28 Constraints on objects Window Employee boss 0..1 length width (0.8<length/width<1.5) salary * (salary<boss.salary) Job priority (priority never increases) 29 Constraint on generalization set (subclasses) • Disjoint – subclasses are mutually exclusive • Overlapping – an object can belong to more than one subclass • Complete – the generalization lists all possible subclasses • Incomplete – the generalization may miss some subclasses 30 Constraint on links • Multiplicity for an association restricts the number of objects related to a given object. • An ordinary association has no presumed order on the objects of the “many” end. The constraint {ordered} indicates that objects of the “many” end have an order that must be preserved. 31 Subset constraint between associations * Member of * (subset) Person 1 Chair of Committee * The chair of a committee must be a member of the committee. 32 Derived data • Classes, attributes and associations may be derived from others. • The notation for a derived element is a slash (/) in front of the element name. • The constraint that determines the derivation should also be shown. Person CurrentDate birthdate / age {age=currentDate – birthdate} Derived attribute 33 Derived object & association Machine 1 * Assembly 1 * Part 1 / NetOffset offset offset 1 / Offset offset = MachineAssembly.offset X PartAssembly.offset Derived data can complicate implementation. Use them only if they are truly necessary. 34 Package • A package is a group of elements (classes, associations, generalizations and packages of smaller sizes) with a common theme or purpose. • Packages partition a model, making it easier to understand or manage. • Large applications may have several tiers of packages. Notation of a package PackageName 35 Reading for this lecture • Chapter 4 Blaha & Rumbaugh 36 Software Engineering Dr. K. T. Tsang Lecture 8 State modeling http://www.uic.edu.hk/~kentsang/SWE/SWE.htm 37 Reading for this lecture • Chapter 5 Blaha & Rumbaugh 38