630 likes | 766 Views
Devon M. Simmonds University of North Carolina, Wilmington. Introduction to UML Class Diagrams. CSC450 Software Engineering. Objectives. Introduce basic class diagram constructs Introduce UML relationships Generalization Association Aggregation/composition Dependency Interfaces.
E N D
Devon M. Simmonds University of North Carolina, Wilmington Introduction to UML Class Diagrams CSC450Software Engineering
Objectives • Introduce basic class diagram constructs • Introduce UML relationships • Generalization • Association • Aggregation/composition • Dependency • Interfaces
Student Id:Integer Addr:String graduate(semester:String):int What is a class? • A class is a description of a set of objects that share the same properties • attributes • Operations • UML class • Java class • public class Student { • String id; • Address addr; • int graduate (String semester) • }
Student Id:Integer Addr:String graduate(semester:String):int Structure of a class • A class has the following structure: • Name compartment (mandatory) • Attributes compartment (optional) • Operations compartment (optional) • Every class must have a distinguishing name. • An object is an instance of a class • An object of a class must have values associated with each attribute of the class Name attributes operations johnWayne:Student Id = 2007 Addr = “Wild Wild West”
Rectangle Rectangle Rectangle Rectangle name getArea height height attributes resize width width getArea operations resize operations attributes Class Notation
Attributes • An attribute is a named property. Each class instance associates value(s) with each attribute of a concept. • What should be an attribute? • Properties with types that we want to treat as primitive are modeled as attributes • Connections to other classes are to be represented as associations, not attributes. Window size:Area visibility:Boolean display() hide()
Operations • An operation is a procedure defined in a class. • Each operation has a signature and a specification of its behavior. • Signature: operation name, a list of argument types and the result type. • operationName(parameterName: parameterType …): returnType • Specification is expressed in terms of pre-and postconditions (the Object Constraint Language is used for this purpose)
Operations II • A method is the implementation of an operation. • Classes in requirements and domain models DO NOT NEED to contain operations • Rationale: in most cases, distributing operations across classes requires making design-level decision.
Objectives • Introduce basic class diagram constructs • Introduce UML relationships • Association • Aggregation/composition • Generalization • Dependency • UML Interfaces
Modeling Static Class Relationships • Association • Aggregation/composition • Generalization • Dependency
Associations • Associations represent conceptual relationships among problem concepts or classes. • Association properties include • Multiplicity • Role • Constraints
-"direction reading arrow" no -it has meaning except to indicate direction of reading the association label -often excluded works-for 1 4 1 Employee Company Association name multiplicity Associations
* zero or more; "many" Customer * 1.. One instance of a 0..1 Customer may be renting zero or more Videos. 6 Rents 1..40 One instance of a Video may be being rented by * zero or one Customers. 5 Video 3, 5, 8 exactly three, five or eight Multiplicity Student Student one or more Student one to forty Student exactly five Student
* Employee Company * 1..* * * * * * Secretary Manager Company BoardOfDirectors 0..1 * Office Employee 0,3..8 * * * * * * Person BoardOfDirectors Association Multiplicity Examples
Labelling associations • Each association can be labelled with a name that gives insight into the meaning of the association
Association Roles • When a class is part of an association it plays a role in the relationship. • You can name the role that a class plays in an association by placing the name at the class’s association end. • Formally, a class role is the set of objects that are linked via the association.
class roles managed project project leader 0..1 1 Project Person assigned project 1..* * project member
Analyzing and validating associations • Many-to-one • A company has many employees, • An employee can only work for one company. • A company can have zero employees • E.g. a ‘shell’ company • It is not possible to be an employee unless you work for a company
Analyzing and validating associations • Many-to-many • A secretary can work for many managers • A manager can have many secretaries • Secretaries can work in pools • Managers can have a group of secretaries • Some managers might have zero secretaries. • Is it possible for a secretary to have, perhaps temporarily, zero managers?
Analyzing and validating associations • One-to-one • For each company, there is exactly one board of directors • A board is the board of only one company • A company must always have a board • A board must always be of some company
Reflexive associations • It is possible for an association to connect a class to itself successor * Course isMutuallyExclusiveWith * * * prerequisite
Navigability • One can indicate that an object “knows about” another object it is linked to by using navigation arrows on associations • In UML 2.0 one can also explicitly show that one object does not know about the objects it is linked to.
The constructs in diagrams 1, 2, and 4 are new to UML 2.0 and thus are most likely not supported by UML tools as yet. • The top pair AB shows a binary association with two navigable ends. • The second pair CD shows a binary association with two non-navigable ends. • The third pair EF shows a binary association with unspecified navigability. • The fourth pair GH shows a binary association with one end navigable and the other non-navigable. • The fifth pair IJ shows a binary association with one end navigable and the other having unspecified navigability.
LibraryPatron LibraryPatron Loan borrowedDate dueDate CollectionItem returnedDate CollectionItem Actions versus associations • A common mistake is to represent actions as if they were associations * * * * * * * borrow return * * * Better: The borrow operation creates a Loan and the return operation sets the returnedDate Bad, due to the use of associations that are actions
What is a Class Diagram? • A class diagram describes a structure of classes • Key Question: What are the objects of interest in the problem/solution space? • their properties (in terms of attributes and operations)? • their relationships?
An example of a Class Diagram description of problem concepts 0..1 4 Rents * Customer VideoStore Video 4 4 Rents-from Stocks address address * 1 name name ID * 1 phoneNumber phoneNumber * 1..2 HeadOffice branchOffice The above is part of a domain model for a video rental system
Objects and Links • An object is an instance of a class • A link is an instance of an association • Can you draw a valid object diagram for this class diagram? 0..1 4 Rents * Customer VideoStore Video 4 4 Rents-from Stocks address address * 1 name name ID * 1 phoneNumber phoneNumber * 1..2 HeadOffice branchOffice
Association: Model to Implementation * 4 Class Student { Course enrolls[4]; } Class Course { Student istakenby[]; } Course Student istakenby enrolls
Association: Model to Implementation * 4 Class Student { Course enrolls[4]; } Course Student has enrolls
Modeling Static Class Relationships • Association • Aggregation/composition • Generalization • Dependency
Aggregation • Aggregation is a special form of association • reflect whole-part relationships • The whole delegates responsibilities to its parts • the parts are subordinate to the whole • This is unlike associations in which classes have equal status
UML Forms of Aggregation • Composition (strong aggregation) • parts are existent-dependent on the whole • parts are generated at the same time, before, or after the whole is created (depending on cardinality at whole end) and parts are deleted before or at the same time the whole dies • multiplicity at whole end must be 1 or 0..1 • (weak) Aggregation
Composition – black diamond notation Window 1 1 1 scrollbar 2 title 1 body 1 Header Slider Panel
Automobile Transmission Engine OO Relationships:Composition Composition: expresses a relationship among instances of related classes. It is a specific kind of Whole-Partrelationship. Whole Class It expresses a relationship where an instance of the Whole-class has the responsibility to create and initialize instances of each Part-class. Class W It may also be used to express a relationship where instancesof the Part-classes have privileged access or visibility tocertain attributes and/or behaviors defined by theWhole-class. Composition should also be used to express relationship whereinstances of the Whole-class have exclusive access to and control of instances of the Part-classes. ClassP2 ClassP1 Part Classes Example Composition should be used to express a relationship wherethe behavior of Part instances is undefined without beingrelated to an instance of the Whole. And, conversely, the behavior of the Whole is ill-defined or incomplete if one or more of the Part instances are undefined. [From Dr.David A. Workman]
OO Relationships: Aggregation Container Class Aggregation: expresses a relationship among instances of related classes. It is a specific kind of Container-Containee relationship. It expresses a relationship where an instance of the Container-class has the responsibility to hold and maintain instances of each Containee-class that have been createdoutside the auspices of the Container-class. Aggregation should be used to express a more informalrelationship than composition expresses. That is, it is anappropriate relationship where the Container and its Containeescan be manipulated independently. Aggregation is appropriate when Container and Containees have no special access privileges to each other. ClassC AGGREGATION Class E2 Class E1 Containee Classes Example Bag Milk Apples [From Dr.David A. Workman]
Aggregation vs. Composition • Composition is really a strong form of aggregation • components have only one owner • components cannot exist independent of their owner • components live or die with their owner • e.g. Each car has an engine that can not be shared with other cars. • Aggregationsmay form "part of" the aggregate, but may not be essential to it. They may also exist independent of the aggregate. • e.g. Apples may exist independent of the bag.
Modeling Static Class Relationships • Association • Aggregation/composition • Generalization • Dependency
Generalization/Specialization • A generalization (or specialization) is a relationship between a general concept and its specializations. • Objects of specializations can be used anywhere an object of a generalization is expected (but not vice versa). • Example: Polygon and Ellipse are specializations of Shape
Generalization • Rendering Generalizations • Generalization is rendered as a solid directed line with a large open arrowhead. • Arrowhead points towards generalization • A discriminator can be used to identify the nature of specializations
Generalization Fig. 3-48, UML Notation Guide
An example (generalization) Person 0..2 0..2 0..2 0..2 0..2 0..2 PersonRole name idNumber RegularFlight * EmployeeRole time PassengerRole flightNumber jobFunction supervisor * * * * * * * * * * * * * * * * * * * Booking * * * * * * SpecificFlight seatNumber date
Object Diagram - Example Engineering:Organization Location=“chicago” Organism location * children Apps:Organization Tools:Organization manager employee location=“Wilmington” Location=“chicago” 0..1 parent Person Organization charlene:Person don:Person location=“denver” location=“Raleigh”
Binary and N-ary Associations • A binary association relates two classes. • An n-ary association relates n (n > 2) classes. • N-ary associations can often be modeled as binary associations.
Class Diagram Exercise • Create a use case diagram and a class diagram using the problem description below. The BirdCage Corporation needs software to aid the management of its business. The corporation builds its business around domestic birds. BirdCage arranges its business around different services with each service being managed by a different company making BirdCage a group of companies. Companies are divided into departments. Services include veterinary services, safekeeping of birds, bird calisthenics and a bird competitions service. In order to streamline its business, BirdCage classifies the veterinary and safekeeping services as medical services. The other two services are regarded as entertainment services. In the future the company hopes to add a number of “Nutritional” services to address the production and sales of food for birds. The corporation classifies birds as either exotic or non-exotic. BirdCage does not provide entertainment services for exotic birds. Owls and crows are non-exotic birds, while macaws, cockatoos and green quakers are exotic. BirdCage seeks to maintain a minimum stock of at least 10 of each type of exotic bird.
Modeling Static Class Relationships • Association • Aggregation/composition • Generalization • Dependency
Dependencies in UML • A dependency is a client-server relationship between model elements. • A dependency is shown as a dashed arrow between two model elements. • The server is at the arrowhead. • The arrow may be labeled with an optional stereotype.