Class diagrams show the static structure of classifiers in a system. They are comprised of classes and relationships between classes. A class contains attributes that define an object's state and operations that define its behaviors. Common relationships include inheritance, association, aggregation, and composition. Class diagrams help developers and others understand the design of a system.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
35 views49 pages
Class N Sequence Diagrams
Class diagrams show the static structure of classifiers in a system. They are comprised of classes and relationships between classes. A class contains attributes that define an object's state and operations that define its behaviors. Common relationships include inheritance, association, aggregation, and composition. Class diagrams help developers and others understand the design of a system.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 49
Class Diagrams
Purpose of Class Diagrams
1. Shows static structure of classifiers in a system 2. Diagram provides a basic notation for other structure diagrams prescribed by UML 3. Helpful for developers and other team members too Class Diagram Components • A UML class diagram is made up of: • A set of classes and • A set of relationships between classes What is a Class? • A description of a group of objects all with similar roles in the system, which consists of: • Structural features (attributes) define what objects of the class "know" • Represent the state of an object of the class • Are descriptions of the structural or static features of a class • Behavioral features (operations) define what objects of the class "can do" • Define the way in which objects may interact • Operations are descriptions of behavioral or dynamic features of a class Class Notation • A class notation consists of three parts: • Class Name • The name of the class appears in the first partition. • Class Attributes • Attributes are shown in the second partition. • The attribute type is shown after the colon. • Attributes map onto member variables (data members) in code. • Class Operations (Methods) • Operations are shown in the third partition. They are services the class provides. • The return type of a method is shown after the colon at the end of the method signature. • The return type of method parameters is shown after the colon following the parameter name. • Operations map onto class methods in code Class Notation (contd.) • The graphical representation of the class - MyClass as shown: • MyClass has 3 attributes and 3 operations • Parameter p3 of op2 is of type int • op2 returns a float • op3 returns a pointer (denoted by a *) to Class6 Class Relationships • A class may be involved in one or more relationships with other classes. A relationship can be one of the following types: • Inheritance • Simple Association • Aggregation • Composition Inheritance (Generalizatio n) • Represents an "is-a" relationship. • An abstract class name is shown in italics. • SubClass1 and SubClass2 are specializations of Super Class. • A solid line with a hollow arrowhead that point from the child to the parent class Inheritance Example - Cell Taxonomy Simple Association • A structural link between two peer classes. • There is an association between Class1 and Class2 • A solid line connecting two classes Aggregation: • A special type of association. It represents a "part of" relationship. • Class2 is part of Class1. • Many instances (denoted by the *) of Class2 can be associated with Class1. • Objects of Class1 and Class2 have separate lifetimes. • A solid line with an unfilled diamond at the association end connected to the class of composite Aggregation Example - Computer and parts Composition • A special type of aggregation where parts are destroyed when the whole is destroyed. • Objects of Class2 live and die with Class1. • Class2 cannot stand by itself. • A solid line with a filled diamond at the association connected to the class of composite Composition Example – Person and Body parts Relationship Names • Names of relationships are written in the middle of the association line. • Good relation names make sense when you read them out loud: • "Every spreadsheet contains some number of cells", • "an expression evaluates to a value" • They often have a small arrowhead to show the direction in which direction to read the relationship, e.g., expressions evaluate to values, but values do not evaluate to expressions. Relationship Names Relationship - Roles • A role is a directional purpose of an association. • Roles are written at the ends of an association line and describe the purpose played by that class in the relationship. • E.g., A cell is related to an expression. The nature of the relationship is that the expression is the formula of the cell. Navigability • The arrows indicate whether, given one instance participating in a relationship, it is possible to determine the instances of the other class that are related to it. • The diagram above suggests that, • Given a spreadsheet, we can locate all of the cells that it contains, but that • we cannot determine from a cell in what spreadsheet it is contained. • Given a cell, we can obtain the related expression and value, but • given a value (or expression) we cannot find the cell of which those are attributes. Visibility of Class attributes and Operations • In object-oriented design, there is a notation of visibility for attributes and operations. UML identifies four types of visibility: public, protected, private, and package. • The +, -, # and ~ symbols before an attribute and operation name in a class denote the visibility of the attribute and operation. • + denotes public attributes or operations • - denotes private attributes or operations • # denotes protected attributes or operations • ~ denotes package attributes or operations Class Visibility Example
•attribute1 and op1
of MyClassName are public •attribute3 and op3 are protected. •attribute2 and op2 are private. Class Visibility Summary Multiplicity • How many objects of each class take part in the relationships and multiplicity can be expressed as: • Exactly one - 1 • Zero or one - 0..1 • Many - 0..* or * • One or more - 1..* • Exact Number - e.g. 3..4 or 6 • Or a complex relationship - e.g. 0..1, 3..4, 6.* would mean any number of objects other than 2 or 5 Multiplicity Example • Requirement: A Student can take many Courses and many Students can be enrolled in one Course. 1)Nouns: Look for nouns in the problem statement or domain description. These often represent potential classes. For example, in a banking system, you might have classes like Account, Customer, Transaction, etc. 2)Responsibilities: Consider the responsibilities or behaviors that objects must have or perform within the system. Each responsibility might indicate a potential class. For instance, if an object needs to calculate something or perform a specific action, it could be a separate class. 3)Real-world Objects: Identify real-world objects relevant to the problem domain. These could directly translate into classes. For example, in a library system, you might have classes such as Book, Library, Librarian, etc. 4)Abstraction: Look for opportunities to abstract common properties and behaviors into classes. If you notice multiple objects with similar attributes or behaviors, it might be a candidate for a class. 5)Association Analysis: Consider the relationships between objects. If objects interact or have associations with each other, they might belong to the same class or different classes connected through associations. 6.Attributes and Methods: Identify the attributes (properties) and methods (behaviors) that objects need to have or perform. Each attribute and method might correspond to a class or influence the design of existing classes. 7.Use Cases: Consider the different actions or use cases within the system. Each use case might involve one or more classes. Analyzing the actions required by the system can help identify potential classes. 8.Reuse and Modularity: Look for opportunities to reuse existing classes or break down complex functionalities into smaller, more manageable classes. This promotes modularity and helps in creating a more maintainable design. 9.Domain Experts: Collaborate with domain experts or stakeholders who have a deep understanding of the problem domain. Their insights can help identify relevant classes and ensure the class diagram accurately represents the domain. 10.Iterative Refinement: Remember that class identification is an iterative process. Start with a rough draft of classes based on initial analysis, then refine and adjust as you gain a deeper understanding of the system requirements. Online Shopping UML Class Diagram Example Online Shopping Scenario • Here we provide an example of UML class diagram which shows a domain model for online shopping. The purpose of the diagram is to introduce some common terms, "dictionary" for online shopping - Customer, Web User, Account, Shopping Cart, Product, Order, Payment, etc. and relationships between. It could be used as a common ground between business analysts and software developers. • Each customer has unique id and is linked to exactly one account. Account owns shopping cart and orders. Customer could register as a web user to be able to buy items online. Customer is not required to be a web user because purchases could also be made by phone or by ordering from catalogues. Web user has login name which also serves as unique id. Web user could be in several states - new, active, temporary blocked, or banned, and be linked to a shopping cart. Shopping cart belongs to account. Online Shopping Scenario (contd.) • Account owns customer orders. Customer may have no orders. Customer orders are sorted and unique. Each order could refer to several payments, possibly none. Every payment has unique id and is related to exactly one account. • Each order has current order status. Both order and shopping cart have line items linked to a specific product. Each line item is related to exactly one product. A product could be associated to many line items or no item at all. Sequence Diagram What is Sequence Diagram? • Sequence Diagrams are interaction diagrams that detail how operations are carried out. • They capture the interaction between objects in the context of a collaboration. • Sequence Diagrams are time focus and they show the order of the interaction visually by using the vertical axis of the diagram to represent time what messages are sent and when. What is Sequence Diagram? (contd.) • Sequence Diagrams captures: • the interaction that takes place in a collaboration that either realizes a use case or an operation (instance diagrams or generic diagrams) • high-level interactions between user of the system and the system, between the system and other systems, or between subsystems (sometimes known as system sequence diagrams) Purpose of Sequence Diagram • Model high-level interaction between active objects in a system • Model the interaction between object instances within a collaboration that realizes a use case • Model the interaction between objects within a collaboration that realizes an operation • Either model generic interactions (showing all possible paths through the interaction) or specific instances of a interaction (showing just one path through the interaction) Sequence Diagrams at a Glance • Sequence Diagrams show elements as they interact over time and they are organized according to object (horizontally) and time (vertically): • Object Dimension • The horizontal axis shows the elements that are involved in the interaction • Conventionally, the objects involved in the operation are listed from left to right according to when they take part in the message sequence. However, the elements on the horizontal axis may appear in any order • Time Dimension • The vertical axis represents time proceedings (or progressing) down the page. Sequence Diagram Notation Actor •a type of role played by an entity that interacts with the subject (e.g., by exchanging signals and data) •external to the subject (i.e., in the sense that an instance of an actor is not a part of the instance of its corresponding subject). •represent roles played by human users, external hardware, or other subjects. Sequence Diagram Notation (contd.) Lifeline •A lifeline represents an individual participant in the Interaction Sequence Diagram Notation (contd.) Activations •A thin rectangle on a lifeline) represents the period during which an element is performing an operation. •The top and the bottom of the of the rectangle are aligned with the initiation and the completion time respectively Sequence Diagram Notation (contd.) Call Message •A message defines a particular communication between Lifelines of an Interaction. •Call message is a kind of message that represents an invocation of operation of target lifeline. Sequence Diagram Notation (contd.) Return Message •A message defines a particular communication between Lifelines of an Interaction. •Return message is a kind of message that represents the pass of information back to the caller of a corresponded former message. Sequence Diagram Notation (contd.) Self Message •A message defines a particular communication between Lifelines of an Interaction. •Self message is a kind of message that represents the invocation of message of the same lifeline. Sequence Diagram Notation (contd.) Recursive Message •A message defines a particular communication between Lifelines of an Interaction. •Recursive message is a kind of message that represents the invocation of message of the same lifeline. It's target points to an activation on top of the activation where the message was invoked from. Sequence Diagram Notation (contd.) • Destroy Message • A message defines a particular communication between Lifelines of an Interaction. • Destroy message is a kind of message that represents the request of destroying the lifecycle of target lifeline Sequence Diagram Notation (contd.) Sequence Fragments •A sequence fragment is represented as a box, called a combined fragment, which encloses a portion of the interactions within a sequence diagram •The fragment operator (in the top left cornet) indicates the type of fragment • Alt: (Alternative multiple fragments) only the one whose condition is true will execute • Loop: the fragment may execute multiple times, and the guard indicates the basis of iteration • Opt: (Optional) the fragment executes only if the supplied condition is true. Equivalent to an alt only with one trace. Combined Fragment Example Facebook Web User Authentication UML Sequence Diagram Example Facebook Web User Authentication Scenario • An example of UML sequence diagram which shows how Facebook (FB) user could be authenticated in a web application to allow access to his/her FB resources. Facebook uses OAuth 2.0 protocol framework which enables web application (called "client"), which is usually not the FB resource owner but is acting on the FB user's behalf, to request access to resources controlled by the FB user and hosted by the FB server. Instead of using the FB user credentials to access protected resources, the web application obtains an access token. • Web application should be registered by Facebook to have an application ID (client_id) and secret (client_secret). When request to some protected Facebook resources is received, web browser ("user agent") is redirected to Facebook's authorization server with application ID and the URL the user should be redirected back to after the authorization process. Facebook Web User Authentication Scenario (contd.) • User receives back Request for Permission form. If the user authorizes the application to get his/her data, Facebook authorization server redirects back to the URI that was specified before together with authorization code ("verification string"). The authorization code can be exchanged by web application for an OAuth access token. • If web application obtains the access token for a FB user, it can perform authorized requests on behalf of that FB user by including the access token in the Facebook Graph API requests. If the user did not authorize web application, Facebook issues redirect request to the URI specified before, and adds the error_reason parameter to notify the web application that authorization request was denied. Thanks