Bài giảng Công nghệ phần mềm nâng cao - Phạm Ngọc Hùng - Class & Object Diagrams
UML Class Diagrams
The heart of the UML
Describes the classes in the system and
the relations among them
Supports most OO concepts
associations
aggregation
inheritance (including multiple)
Class & Object Diagrams Software Design Methodology 2UML Class Diagrams The heart of the UML Describes the classes in the system and the relations among them Supports most OO concepts associations aggregation inheritance (including multiple) 3Classes Classes are denoted by rectangles. Attributes Operations Responsibilities Class attribute: type operation operation(parameters) responsibility 4Classes Attribute: A named property of a class that describes a range of values that instances of the property may hold. Operation: Implementation of a service that can be requested. Responsibility: A contract or an obligation of a class. As the model is refined the responsibilities are transformed into attributes and operations. 5Object • Object - a concept, an abstraction, a thing meaningful to the domain • Joe Smith Person • Lassie Dog • flight #713 Flight • the top window Window • Object Class - a collection of all objects having the same set of features. • Each object has a unique identity and is an instance of its object class. 6Class Diagrams vs. Object Diagrams Person name employeeID title Smith:PersonJoe p:Person name=“Yahoo” employeeID=1212 title=“VP of production” 7Relationships A relationship is a connection among things. Common relationships: Dependencies, Associations, Generalization and Aggregations. 8Dependency A dependency states that a change in specification of one thing may effect another thing that uses it Course CourseSchedule add(c:Course) remove(c:course) Event Dean Window open() close() Student grade CalculateAverage(grade) A class takes an object of another class as a parameter A class accesses an object of another class A class calls a class operation in another class 9An association is structural relationship that specifies that objects of one thing are connected to objects of another. Association Country Name: string Population: integer Area: Km2 City Name: string Population: integer Has-capital Binary association 10 Association Name Printed at the center of the line Describes the nature of the relationship (usually a verb) A direction triangle can be added 11 Association Multiplicity Used when it is important to state how many objects may be connected across an instance of an association. When a number is stated at an end of an association it is specified that for each object at the class at the opposite end there must be that many objects at the near end. An * denotes many - which can be any number between zero and infinity. 12 Multiplicity Example Office Employee Department 1..* 1..10 1 0..1 At least one employee in a department An employee belongs to exactly one department An employee has zero or one office An office is assigned to a number of one up to 10 employees 13 Multiplicity symbols (participation constraints) Line Name: string Point Name: string X-Coordinate: integer Y-Coordinate: integer Intersects0,2..* * Is Endpoint Of 2* 14 Association Classes Denoted as a class attached to the association Specify properties of the association Does not belong to any of the connected class Car Reg. Num. Model Year Person Name Year Purchased * 1.. *Owner of 15 Association Attributes Works-for Person Name: string ID: string Address: string Company Name: string Address: string salary: float job: string 1..* * submit Student Name ID Faculty Assignment Number Subject Submission date Grade 140 4 16 Association Role Names Names may be added at each end of the association Provide better understanding of the association meaning Especially helpful in self-associated classes Person Manager Worker Manages Company employee employer * 1..0 *..1 * 17 Role Names, Constraints and Qualifiers Ordering Qualifier: an attribute that reduces the association’s multiplicity. owner User Directory contents authorized user container 1 * * * * 0..1 {ordered}Window Screen visible-on * 1 Directory Filefile name 0..1* Role 18 Classes versus Objects Class Diagram Object Diagram Classes name attributes operations responsibility Association name multiplicity role Association Classes Objects values for attributes Link Link Object 19 Class Diagram Example holds Reader Name ID Address Copy Code Shelf location Status Loan duration Return date 0..1 * Book Code Name Edition Author of reserved 1 * * *{ordered} 20 Object Diagram Example 357:Reader Name=“Joe” ID=357 Address=“Jerusalem” 57168-1:Copy Code=57168-1 Shelf location=681.3.06 Status=“at loan” Loan duration=2 weeks holds Return date=16.11.00 UML:Book Code=57168 Name=“The Unified Modeling Language User Guide” Edition=1 Author=“Booch, Rumbaugh, Jacobson” of reserved 482:Reader Name:”Michel” ID=482 Address=“Haifa” 936:Reader Name=“David” ID=936 Address=“Haifa” 57168-2:Copy Code=57168-2 Shelf location=681.3.06 Status=“at loan” Loan duration=1 month holds Return date=24.11.00 of 21 Aggregation “whole-part” relationship between classes Assemble a class from other classes Combined with “many” - assemble a class from a couple of instances of that class May be replaced by associations... SentenceParagraph Picture Document text font pages * * * 22 Aggregation (whole-part; part-of; and) Monitor Microcomputer System box Mouse Keyboard RAM CPU Fan Chassis aggregate; whole constituents; parts 0..* 1 0..1 1 * 1 0..1 1 23 Constraint A functional relationship between entities (objects, classes, attributes, links, associations) of an object model. Employee salary Window length width boss {salary < boss.salary} {0.8 ≤length/width≤1.5} Company Office Person {ordered} Member-of Person Committee Chair-of {subset} 0..1 * * * * * * 24 Constraint (Cont.) Person gender:{female, male} Portfolio {self.wife.gender=female and self.husband.gender=male} {secured}=Across the association communicated is encrypted. BankAccount Corporation{secured} {xor} 0..1 wife 0..1 husband 25 Inheritance (Generalization) Denoted by a triangle - connects one superclass to many subclasses Multiple inheritance is allowed a subclass may be connected to more than one superclass not recommended - hard to understand, hard to implement Four applicable standard constraints: Complete Incomplete disjoint overlapping 26 Generalization (gen-spec; kind-of; or) Figure color center position pen type Move Rotate Display 0 Dimensional 1 Dimensional Orientation Scale 2 Dimensional Orientation fill type fill Scale Radius Angle Arc Display Control pts Spline Display Endpoints Line Display Diameter Circle Display Endpoints Polygon Display superclass subclasses 27 Multiple Inheritance CheckingAccount BankAccount InterestBearingItem InsurableItem Asset RealEstate Security SavingAccount Stock Bond 28 Abstract Class - a class that has no direct instances Class Concrete Class subclass Abstract Class Non-leaf Class Leaf Class Instance has direct instances superclass has subclasses superclass has subclasses 1..* 1..* 1..* 1..* 29 Recursive Aggregation Program Block Compound statement Simple statement 2..* 1..* 30 Recursive Terms (Multiple Binary term use) Constant Value Expression Binary Operator Variable Name First Operand Second Operand ** 1 1 Term 31 (x+y/2)/(x/3+y) di1:Expression Binary op=“/” pl2:Expression Binary op=“+” pl1:Expression Binary op=“+” di3:Expression Binary op=“/” di2:Expression Binary op=“/” y:Variable Name=“y” two:Constant Value=2 x:Variable Name=“x” three:Constant Value=3 2nd 1st1st1st 1st 1st 2nd 2nd2nd 2nd 32 Recursive Terms (Unary &Binary & Single Term Use) Second Operand Constant Value Expression Variable Name First Operand 1 1 Term Binary Unary 33 Abstract Operations Hourly Employee hourly rate overtime rate compute pay Salaried Employee weekly rate compute pay Exempt Employee monthly rate compute pay Employee year-to-date earnings compute pay {abstract} The origin class defines the protocol: •semantics; •attribute types; •number and type of arguments for methods 34 Generalization as Extension and Restriction Extension: addition of new features. (e.g. Person vs. Student) Restriction: constraining ancestor attributes. (e.g. nullifying some attributes) 35 Overriding Operations Tension between use of inheritance for abstraction vs. implementation reuse. Reasons for overriding: Extension (e.g. Person has Report_Marriage; Employee has Report_Marriage that extends the code of Report_Marriage) Restriction (e.g. Subclass operation use some of superclass sub-operations) 36 Reasons for overriding (cont.) Optimization e.g. IntegerSet has findMax; SortedIntegerSet has another implementation for findMax. Convenience Look for a similar class and use it as superclass. This is ad hoc use which is semantically wrong. Better: define a third common class, from which both will inherit. Rule for method overriding: All methods that implement an operation must have the same protocol.
File đính kèm:
- Bài giảng Công nghệ phần mềm nâng cao - Phạm Ngọc Hùng - Class & Object Diagrams.pdf