www.ddss.arch.tue.nl 7m822 uml class diagrams advanced concepts 15 september 2008
Post on 21-Dec-2015
216 views
TRANSCRIPT
www.ddss.arch.tue.nl
7M822
UML Class Diagrams advanced concepts
UML Class Diagrams advanced concepts
15 September 2008
www.ddss.arch.tue.nl
7M822
UML Class DiagramsUML Class Diagrams
The class diagram provides a static structure of all the classes that exist within the system. Classes are arranged in hierarchies sharing common structure and behaviour and are associated with other classes.
Class diagrams are used in:• Analysis To build a conceptual domain model with semantic
associations between concepts• Design Structural model of a design in terms of class interfaces• Implementation Source code documentation, exposing the
implementation
www.ddss.arch.tue.nl
7M822
An object is a thing that can be tangible or intangible.Examples• tangible : bike, car, house• intangible : order, account
ObjectsObjects
An object has properties.Example• a Customer has a
name and address
Customer
NameAddress
www.ddss.arch.tue.nl
7M822
Property of object = attribute + value
(Multiple) Objects(Multiple) Objects
Adacs: Customer
Name = AdacsAddress = Enschede
Primo: Customer
Name = PrimoAddress = Grave
Multiple objects unique identity
1247: Customer
Custno = 1247Name = AdacsAddress = Enschede
9378: Customer
Custno = 9378Name = PrimoAddress = Grave
www.ddss.arch.tue.nl
7M822
Link = connection between objects
Connection between ObjectsConnection between Objects
1247: Customer
Custno = 1247Name = AdacsAddress = Enschede
345T7: Order
Orderno = 345T7dateReceived = 10/10/05Price = 1987
902U2: Order
Orderno = 902U2dateReceived = 07/04/05Price = 3789
www.ddss.arch.tue.nl
7M822
Class• class is abstract concept• each object is instance of a class• classes classify objects• attribute has no values
ClassClass
Customer
CustnoNameAddress
Order
OrdernodateReceivedPrice
Object analysis than values to attributes
www.ddss.arch.tue.nl
7M822
• Attribute has no values• Association is relation between classes
Class DiagramClass Diagram
Customer
CustnoNameAddress
Order
OrdernodateReceivedPrice
Link is instance of association
1 *
www.ddss.arch.tue.nl
7M822
Class diagramClass diagram
A class diagram describes the type of objects in a system and the static relationships between them. • Static relations:
– Associations
– Subtypes
• Static structure:– Attributes
– Operations
www.ddss.arch.tue.nl
7M822
Class – operationsClass – operations
• An operation is the implementation of a service that can be requested from any object of the class in order to affect behaviour.
• Operations are used to manipulate the attributes or to perform other actions. Operations are normally called functions, but they are inside a class and can be applied only to objects of that class.
• An operation is described with a return-type, a name and zero or more parameters. Together, the return-type, name, and parameters are called the signature of the operation.
• The signature describes everything needed to use the operation.
www.ddss.arch.tue.nl
7M822
Class - associationsClass - associations
• Associations are structural relationships where instances (objects) of one class are connected to instances (objects) of another class.– an employee works for a company
– a company has a number of offices
• An association is normally bidirectional, which means that if an object is associated with another object, both objects are aware of each other (navigation is bidirectional by default).
• Binary association connects exactly two classes. N-ary connects many classes.
• The most common association is just a connection between classes.
www.ddss.arch.tue.nl
7M822
Association adornments: name, roleAssociation adornments: name, role
• The association has a name - the descriptive term, often a verb, for the association.
• Each association has two association ends; each end is attached to one of the classes in the association. An end can be explicitly named with a label. This label is called a role name (association ends are often called roles).
Person Company
employee employer
works for
Person Company
employee employer
has employment for
www.ddss.arch.tue.nl
7M822
Associations: multiplicityAssociations: multiplicity
• Multiplicity defines the number of objects associated with an instance of the association.– Default of 1 (1: 1)
– 0 or 1: 0..1
– Zero or more (0..infinite): *
– 1 or more (1..infinite): 1..*
– n..m; range from n to m inclusive
Car Persontransports
passenger
Car Persontransports
passenger
5
Car Persontransports
passenger
*
Car Persontransports
passenger
1..*
Car Persontransports
passenger
2..5
www.ddss.arch.tue.nl
7M822
GeneralizationGeneralization
• A specialization / generalization relationship, in which objects of the specialized element (child) are substitutable for objects of the generalized element (parent).
– Superclass – the generalization of another class, the child.
– Subclass – the specialization of another class, the parent.
Customer
Corporate
Customer
Personal
Customer
www.ddss.arch.tue.nl
7M822
Generalization - characteristicsGeneralization - characteristics
• Identify common features concerning behaviour and knowledge. Define these common features on a higher level in the inheritance hierarchy.
• The aim is at behaviour more than knowledge when combining classes.
• Generalization is a bottom-up process.• A superclass includes all common properties of its
subclasses.
www.ddss.arch.tue.nl
7M822
Specialization - characteristicsSpecialization - characteristics
• Define a new class which is a special appearance of an existing class.
• Specialization is a top-down process. • A subclass can have attributes and operations that are
specific for that sub-class.• A subclass may redefine operations of its super-class
www.ddss.arch.tue.nl
7M822
ConstraintsConstraints
• The basic constructs of association, attribute, and generalization do much to specify important constraints.
• They cannot indicate every constraint. These constraints still need to be captured; the class diagram is a good place to do that.
• The UML allows you to use anything to describe constraints. The only rule is that you put them inside braces { }.
www.ddss.arch.tue.nl
7M822
Attribute analysisAttribute analysis
• It is not always clear which attributes belongs to which classes by finding out the class attributes. An attribute is assigned to that class where it is certainly a feature.
• For example: a project leader has the attributes name, department and age.
• But what to do with the attributes project number, project duration, starting time, and budget? These attributes clarifies something about the relation between project leader and type of project.
• This often happens if a n:m relation refers to an association between two classes. In that case, we can define a new class, for instance project management.
www.ddss.arch.tue.nl
7M822
Aggregation and CompositionAggregation and Composition
• Aggregation is a special form of association that specifies a whole-part relationship between the aggregate (the whole) and a component (the part).
• Composition is a form of aggregation with strong ownership and coincident lifetime of the parts by the whole; parts with nonfixed multiplicity may be created after the composite itself, but once created they live and die with it; such parts can also be explicitly removed before the death of the composite.
www.ddss.arch.tue.nl
7M822
Aggregation and Composition: exampleAggregation and Composition: example
Chess set
Board
+kleur() : {w,b}
-column : A..H-row : 1..8
Field
-color : {w,b}
Piece
1
1
1
*
1 *64 1 0..1
Pawn Bishop Knight Rook Queen King
www.ddss.arch.tue.nl
7M822
Object diagramObject diagram
• An object diagram is a diagram that shows a set of objects and their relationships at a point in time; object diagrams address the static design view or static process view of a system.
• An object diagram is shown as a class, and the name is underscored, although an object’s name can be shown optionally preceding the class name as: objectname: classname. The object does not have to be named, in which case only the classname is shown underscored.
www.ddss.arch.tue.nl
7M822
Advanced Class Concepts : VisibilityAdvanced Class Concepts : Visibility
• Visibility – class members (attributes, operations) may be specified as:{we have feature f, an attribute or operation that is defined on an object O of class C }
+ : public{f is public f is visible to any object and f is inherited by the subclasses of C.}
# : protected{f is private f is visible only to O and f is not inherited by the subclasses of C.}
- : private(f is protected f is visible only to objects of class C and to objects of C’s
subclasses and f is inherited by the subclasses of C.}
• Restricting visibility is the same as restricting accessibility.
www.ddss.arch.tue.nl
7M822
Advanced Class Concepts : ScopeAdvanced Class Concepts : Scope
• Individual member data (= attributes) may have:– Class scope : a single copy of an attribute is shared by all
instances of a class.– Instance scope : each instance of a class would have its own copy
of the attribute.
www.ddss.arch.tue.nl
7M822
Class objectClass object
• Objects are abstractions of real-world entities.
• Objects encapsulate state and represent information.
• Object’s state is protected.
• Objects have responsibility.
• Objects interact by interfaces.
www.ddss.arch.tue.nl
7M822
Advanced Class Concepts : AbstractAdvanced Class Concepts : Abstract
• An operation is abstract if it has no implementation.
• A abstract class cannot have any direct instances.
• Abstract classes only occur in the context of an inheritance hierarchy.
• Abstract operations and classes are specified by writing its name in italics.
www.ddss.arch.tue.nl
7M822
Advanced Class Concepts : Abstract cont.Advanced Class Concepts : Abstract cont.
www.ddss.arch.tue.nl
7M822
Interfaces and Abstract ClassesInterfaces and Abstract Classes
• One can vary the interfaces of classes independent of the implementation.
• A pure interface is a class with no implementation and, therefore has operation declarations but no method bodies an fields.
• Interfaces are often declared through abstract classes.
www.ddss.arch.tue.nl
7M822
Interfaces and Abstract ClassInterfaces and Abstract Class
• Sub classing is not only the way to do this.• For instance Java provides an interface construct, and the compiler
checks that the implementing class provides implementations of all the interface’s operations.
www.ddss.arch.tue.nl
7M822
Aggregation and Composition once moreAggregation and Composition once more
• Aggregation is a special form of association that specifies a whole-part relationship between the aggregate (the whole) and a component (the part); aggregation is the part-of relationship.{ it is a special form of association in which a collection of objects, each having an independent existence, is associated with an single object} {unfilled diamond}
• Composition is a form of aggregation with strong ownership and coincident lifetime of the parts by the whole; the part object may belong to only one whole – the parts are usually expected to live and die with the whole. {usually, any deletion of the whole is considered to cascade to the parts}{filled diamond}
www.ddss.arch.tue.nl
7M822
Aggregation and Composition once more, cont.Aggregation and Composition once more, cont.
Computer system
Monitor
Building
Floor
Computer system
Monitor
Building
Floor
1
1
1
1..*
1
1..2
1
*
www.ddss.arch.tue.nl
7M822
Derived Associations and AttributesDerived Associations and Attributes
• Derived associations and attributes can be calculated fro other associations and attributes, respectively, on a class diagram.– For example, an age attribute
of a Person can be derived if you know that Person’s date of birth.
www.ddss.arch.tue.nl
7M822
Derived Associations and Attributes exampleDerived Associations and Attributes example
www.ddss.arch.tue.nl
7M822
Qualified AssociationsQualified Associations
• The qualifier specifies how a specific object at the many end of the association is identified, and may be seen as a kind of key separating all the objects in the association.
www.ddss.arch.tue.nl
7M822
Reflexive AssociationsReflexive Associations
• A class has an association to itself.
• Example: A directory may contain other directories.
Directory File
0..1parent
subdirectory 0..*
1 0..*
www.ddss.arch.tue.nl
7M822
ReferencesReferences
• Sommerville, Ian (2001)
Software Engineering, 6th edition
http://www.software-engin.com
• Grady Booch, James Rumbaugh, Ivar Jacobson (2005)
The Unified Modeling Language User Guide, 2nd edition
• Martin Fowler (2000, 2004)
Object-Oriented Software Engineering, 2nd edition; 3rd edition