<<

2019/6/10

Object-oriented Analysis and Design Object-oriented Analysis and Design

Applying UML and Patterns Chap 16 An Introduction to Object-oriented Analysis UML Class Diagrams and Design and Iterative Development

Part III Elaboration Iteration I – Basic2

Software Engineering Software Engineering 1 2

Object-oriented Analysis and Design Object-oriented Analysis and Design Common UML class diagram notation 1 Introduction

 Objective of this chapter  Provide a reference for frequently used UML class diagram notation

 The UML includes class diagrams to illustrate classes, interfaces, and their associations. They are used for static object modeling

Software Engineering Software Engineering 3 4

Object-oriented Analysis and Design ★ Object-oriented Analysis and Design

Common UML class diagram notation 2 Design Class Diagram (DCD)

 The same UML diagram can be used in multiple perspectives  In a conceptual perspective the class diagram can be used to visualize a domain model.  Class diagram is used in a software or design perspective, called design class diagram (DCD)

Domain Model Sale Register 1 Captures 1 time conceptual ... isComplete : Boolean perspective /total

Register Sale

Design Model ... 1 time isComplete : Boolean DCD ; software endSale () currentSale /total perspective enterItem (...) makePayment (...) makeLineItem (...) Software Engineering Software Engineering 5 6

1 2019/6/10

★ Object-oriented Analysis and Design ★★ ★ Object-oriented Analysis and Design Classifier Ways to Show UML Attributes

 A UML classifier is "a model element that describes  Ways to Show UML Attributes: behavioral and structure features".  Attribute Text and Association Lines  Classifiers can also be specialized.  Attributes of a classifier are shown several ways:  They are a generalization of many of the elements of the  attribute text notation, such as currentSale : Sale. UML, including classes, interfaces, use cases, and actors.  association line notation  In class diagrams, the two most common classifiers are  both together regular classes and interfaces.  The full format of the attribute text notation is:  visibility name : type multiplicity = default {property- string}  Guideline: Attributes are usually assumed private if no visibility is given

Software Engineering Software Engineering 7 8

Object-oriented Analysis and Design Object-oriented Analysis and Design

Ways to Show UML Attributes 1 Ways to Show UML Attributes 2

Register Sale using the attribute text notation to currentSale : Sale ... the association name, common when drawing a indicate Register has domain model, is often excluded (though still legal) a reference to one ...... when using class diagrams for a software Sale instance perspective in a DCD

Register Sale 1 Captures-current-sale 1 Register Sale UP Domain Model OBSERVE : this style conceptual perspective id : Int time : DateTime visually emphasizes 1 ...... the connection currentSale between these classes ......

using the association notation to indicate UP Design Model Register Sale Register has a reference to one Sale instance DCD 1 id: Int time: DateTime software perspective currentSale ...... Register Sale thorough and unambiguous , but some 1 currentSale : Sale ... people dislike the currentSale possible redundancy ...... Attribute text versus association line notation for a UML attribute

Software Engineering Software Engineering 9 10

Object-oriented Analysis and Design Object-oriented Analysis and Design

Ways to Show UML Attributes 3 Ways to Show UML Attributes 4

 Guideline: When showing attributes-as-associations,  Guideline: When to Use Attribute Text versus follow the style in DCDs, which is suggested by the Association Lines for Attributes UML specification. (Fig 16.4 upper)  Use the attribute text notation for objects and  Guideline: when using class diagrams for a domain the association line notation for others. model do show association names but avoid navigation  Both are semantically equal, but showing an association arrows, as a domain model is not a software perspective. line to another class box in the diagram (as in Figure 16.3) gives visual emphasis - it catches the eye, emphasizing the connection between the class of objects on the diagram.

Software Engineering Software Engineering 11 12

2 2019/6/10

Object-oriented Analysis and Design Object-oriented Analysis and Design

Ways to Show UML Attributes 5 Ways to Show UML Attributes 6

Register Sale applying the guideline  How to Show Collection Attributes with Attribute Text 1 to show attributes as id: Int time: DateTime attribute text versus as currentSale and Association Lines ? association lines ......

Register has THREE attributes: Store 1. id 1 2. currentSale address: Address public class Sale { phone: PhoneNumber 3. location location private List lineItems = ... new ArrayList(); // … public class Register { } private int id; } private Sale currentSale; private Store location; // … }

Software Engineering Software Engineering 13 14

★ ★ ★ Object-oriented Analysis and Design Object-oriented Analysis and Design

Ways to Show UML Attributes 7 Operations and Methods 1

 Operations Sale SalesLineItem visibility name (parameter-list) : return-type {property-string} time: DateTime ...  Guideline:Assume the version that includes a return type. lineItems : SalesLineItem [1..*] or ...  Guideline: Operations are usually assumed public if no visibility is lineItems : SalesLineItem [1..*] {ordered} shown. ...  Example Two ways to show a collection attribute  + getPlayer( name : String ) : Player {exception IOException}  public Player getPlayer( String name ) throws IOException

Sale SalesLineItem  An operation is not a method. 1..* time: DateTime ...  A UML operation is a declaration, with a name, parameters, lineItems return type, exceptions list, and possibly a of constraints of ... {ordered, List} ... pre-and post-conditions.  But, it isn't an implementation - rather, methods are notice that an association end can optionally also have a property string such as {ordered, List} implementation

Software Engineering Software Engineering 15 16

Object-oriented Analysis and Design Object-oriented Analysis and Design

Operations and Methods 2 Keywords 1

 How to Show Methods in Class Diagrams?  A UML keyword is a textual adornment to categorize a  in interaction diagrams, by the details and sequence of model element. messages  For example, the keyword to categorize that a classifier  in class diagrams, with a UML note symbol stereotyped box is an is «interface». with «method»  The «» keyword was used on p. 91 to replace the human stick-figure actor icon with a class box to model computer-system or robotic actors. Register «method»  Guideline: When sketching UML - when we want speed, // pseudo-code or a specific language is OK ... public void enterItem( id, qty ) ease, and creative flow - modelers often simplify { endSale() ProductDescription desc = catalog.getProductDescription(id); enterItem(id, qty) keywords to something like '' or ''. sale.makeLineItem(desc, qty); makeNewSale() } makePayment(cashTendered)

Software Engineering Software Engineering 17 18

3 2019/6/10

★ Object-oriented Analysis and Design Object-oriented Analysis and Design

Keywords 2 Stereotypes, Profiles, and Tags

 Most keywords are shown in guillemet (« ») but some  Stereotypes are shown in curly braces, such as {abstract}, which is a  are shown with guillemets symbols constraint containing the abstract keyword.  represents a refinement of an existing modeling concept and is defined within a UML  In general, when a UML element says it can have a  The UML predefines many stereotypes, such as «destroy» (used "property string“ - such as a UML operation and UML on sequence diagrams), and also allows user-defined ones. association end have - some of the property string terms  Thus, stereotypes provide an extension mechanism in the UML will be keywords used in the curly brace format.  Profiles  a collection of related stereotypes, tags, and constraints to specializethe use of the UML for a specific domain or platform  For example, UML profile for project management or for data modeling.

Software Engineering Software Engineering 19 20

× Object-oriented Analysis and Design Object-oriented Analysis and Design Stereotypes Property and Property String

 In the UML, a property is "a named value denoting a characteristic of an element. A property has semantic

declaring the using the stereotype impact."

a tool will probably allow a popup to fill in the tag values,  Some properties are predefined in the UML, such as UML extension once an element has been stereotyped with «authorship» relationship to a basic visibility - a property of an operation. UML metamodel term – Element  Others can be user-defined.

«authorship»  Textual presentation approach «» «stereotype» «authorship» author = “craig” Element Authorship Square status = “tested”  UML property string {name1=value1, name2=value2} ... author: String ... status : String  such as {abstract, visibility=public}.  Some properties are shown without a value, such as {abstract}; Stereotype declaration and use

Software Engineering Software Engineering 21 22

Object-oriented Analysis and Design Object-oriented Analysis and Design Abstract Classes and Abstract Operations Constraint

 Abstract classes and operations can be shown either with  Constraints an {abstract} tag (useful when sketching UML) or by  Constraints may be used on most UML diagrams, but are especially common on class diagrams. italicizing the name (easy to support in a UML tool).  A UML constraint is a restriction or condition on a UML element.  The opposite case, final classes and operations that can't  It is visualized in text between braces; be overridden in subclasses, are shown with the {leaf}  for example: { size >= 0 }.  The text may be natural language or anything else, such as Object tag. Constraint Language (OCL)

Software Engineering Software Engineering 23 24

4 2019/6/10

Object-oriented Analysis and Design ★ Object-oriented Analysis and Design Singleton Classes Generalization

 Generalization in the UML is shown with a solid line and fat triangular arrow from the subclass to superclass 1  A taxonomic relationship between a more ServicesFactory UML notation : this '1' can optionally be used general classifier and a more specific classifier. UML notation : in a instance : ServicesFactory to indicate that only one class box , an instance will be created  Each instance of the specific classifier is also an underlined attribute or accountingAdapter : IAccountingAdapter (a singleton ) method indicates a inventoryAdapter : IInventoryAdapter indirect instance of the general classifier. static (class level ) taxCalculatorAdapter : ITaxCalculatorAdapter  Thus, the specific classifier indirectly has member , rather than an instance member getInstance () : ServicesFactory features of the more general classifier. getAccountingAdapter () : IAccountingAdapter  Generalization  inheritance? getInventoryAdapter () : IInventoryAdapter getTaxCalculatorAdapter () : ITaxCalculatorAdapter  It depends. In a domain model conceptual- ... perspective class diagram, the answer is no.  In a DCD software-perspective class diagram, it implies OOPL inheritance from the superclass to subclass.

Software Engineering Software Engineering 25 26

Object-oriented Analysis and Design Object-oriented Analysis and Design

Dependency 1 Dependency 2

 Dependency lines may be used on any diagram, but are especially common on class and diagrams.  All of these could be shown with a dependency line in  The UML includes a general dependency relationship that indicates the UML, but some of these types already have special that a client element (of any kind, including classes, packages, use lines that suggest the dependency cases, and so on) has knowledge of another supplier element and that a change in the suppliercould affect the client.  When to show a dependency?  Dependency is illustrated with a dashed arrow line from the client to  Guideline: In class diagrams use the dependency line to supplier.  Dependency can be viewed as another version of coupling depict global, parameter variable, local variable, and  There are many kinds of dependency static-method (when a call is made to a static method of  having an attribute of the suppliertype another class) dependency between objects.  sending a message to a supplier; the visibility to the supplier could be:  an attribute, a parameter variable, a local variable, a global variable, or class visibility (invoking static or class methods)  receiving a parameter of the supplier type  the supplieris a superclass or interface

Software Engineering Software Engineering 27 28

Object-oriented Analysis and Design Object-oriented Analysis and Design

Dependency 2 Dependency 3

the doX method invokes the runFinalization static method, and thus has a dependency on the Sale has parameter visibility to a the System class ProductDescription, and thus some kind of System dependency ProductDescription ... Foo ... runFinalization() Sale ...... doX() ... Showing dependency updatePriceFor( ProductDescription ) ... SalesLineItem Clock B Window «call» A «create» 1..* ... getTime () ... lineItems ...... a dependency on calling on operations of a dependency that A objects create B objects Showing dependency the operations of a Clock Optional dependency labels in the UML Software Engineering Software Engineering 29 30

5 2019/6/10

Object-oriented Analysis and Design ★★ Object-oriented Analysis and Design

Interface Composition Over Aggregation 1

 Aggregation is a vague kind of association in the UML that loosely suggests whole-part relationships  It has no meaningful distinct semantics in the UML versus a plain association,  but the term is defined in the UML. Why?

 In spite of the few semantics attached to aggregation, everybody thinks it is necessary (for different reasons). Think of it as a modeling placebo. [RJB04]

 Guideline: Therefore, following the advice of UML creators, don't bother to use aggregation in the UML; rather, use composition when appropriate

Software Engineering Software Engineering 31 32

Object-oriented Analysis and Design Object-oriented Analysis and Design

Composition Over Aggregation 2 Composition Over Aggregation 3

 Composition  also known as composite aggregation, is a strong kind of whole-part aggregation and is useful to show in some 1 0..7 composition means Hand Finger models. -a part instance (Square) can only be part of one  A composition relationship implies that composite (Board) at a time 1) an instance of the part (such as a Square) belongs to only composition -the composite has sole responsibility for management of one compositeinstance (such as one Board) at a time, its parts, especially creation and deletion

2) the part must always belong to a composite (no free- 1 40 1 1..* floating Fingers), and Board Square Sale SalesLineItem 3) the composite is responsible for the creation and deletion of its parts - either by itself creating/deleting the parts, or by collaborating with other objects.  Guideline: The association name in composition is always implicitly some variation of "Has-part," therefore don't bother to explicitly name the association Software Engineering Software Engineering 33 34

Object-oriented Analysis and Design Object-oriented Analysis and Design

Association Class Qualified Association 1

 An association class allows you treat an association itself as a class, and model it with attributes, operations,

and other features. (a) Product Contains Product Catalog Description  For example, if a Company employs many Persons, 1 1..* modeled with an Employs association, you can model the

association itself as the Employment class, with 1 1 Product Contains Product (b) itemID attributes such as startDate Catalog Description

Company * Employs * Person qualifier multiplicity reduced to 1

a person may have Employment employment with several companies salary startDate Software Engineering Software Engineering 35 36

6 2019/6/10

Object-oriented Analysis and Design Object-oriented Analysis and Design Relationship Between Interaction and Qualified Association 2 Class Diagrams

 A qualified association has a qualifier that is used to : Register : Sale select an object (or objects) from a larger set of related objects, based upon the qualifier key. makePayment(cashTendered)  Informally, in a software perspective, it suggests looking makePayment(cashTendered) things up by a key, such as objects in a HashMap. For messages in interaction example, if a ProductCatalog contains many diagrams indicate operations in the class diagrams classes ProductDescriptions, and each one can be selected by an identified in the interaction itemID diagrams are Register Sale declared in the 1 class diagrams ...... currentSale makePayment(…) makePayment(…) ......

Software Engineering Software Engineering 37 38

× Object-oriented Analysis and Design × Object-oriented Analysis and Design

Template Classes and Interfaces 1 Template Classes and Interfaces 2

the attribute type may be expressed in  Many languages (Java, C++, …) support templatized parameterized or template K official UML , with the template binding interfaces and classes «interface» syntax requiring an arrow types, also known (with shades of variant meanings) as List or K is a template parameter in another language , such as Java templates, parameterized types, and generics. clear () ...  They are most commonly used for the element type of Board collection classes, such as the elements of lists and maps. squares : List For example, in Java, suppose that a Board software anonymous class with or template binding complete T squares : List object holds a List (an interface for a kind of collection) ArrayList ... of many Squares. And, the concrete class that elements : T[*] ArrayList ... implements the List interface is an ArrayList: clear () clear () for example , the elements attribute is an ...... public class Board { array of type T , parameterized and bound private List squares = new ArrayList(); before actual use . there is a chance the UML 2 “arrow” symbol will // … eventually be replaced with something else e .g., ‘=’ }

Software Engineering Software Engineering 39 40

× Object-oriented Analysis and Design × Object-oriented Analysis and Design User-Defined Compartments Active Class

DataAccessObject  An active object runs on and controls its own thread of id : Int execution. ...  The class of an active object is an active class

doX() ... active class exceptions thrown Clock DatabaseException «interface» IOException Runnable ... responsibilities run() run() serialize and write objects ... read and deserialize objects ... Software Engineering Software Engineering 41 42

7