CSC441 Intro. to SE [5] OO Modelling, UML Static Diagrams PDF
Document Details
Uploaded by LucidArtDeco
Dr. Amr S. Ghoneim
Tags
Summary
This document provides an introduction to object-oriented modeling using UML static diagrams (class, object, and package diagrams). Concepts like aggregation, composition, and inheritance are covered.
Full Transcript
(CS251) Lecture 5 OO Modelling, UML Static SOFTWARE ENGINEERING 1 Diagrams (Class, Object, & Package Diagrams)...
(CS251) Lecture 5 OO Modelling, UML Static SOFTWARE ENGINEERING 1 Diagrams (Class, Object, & Package Diagrams) Dr. Amr S. Ghoneim OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 1 o Generalization, Specialization & Inheritance o Overriding Features TOPICS COVERED o Abstract Class o Continuing Class Diagrams o Object Diagrams o Elements & An Example o Associations o Multiplicity o Class Modelling Tips o Links and Associations o Divide and Conquer: Modularization o Roles o Cohesion o Types of Cohesion o Self-Associations o Coincidental Cohesion o Association classes vs. Ordinary o Functional Cohesion classes o Coupling o Qualified Association o Examples o A sample class model o Consequences of Coupling o Aggregation & Composition o Package Diagrams Relation OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 2 ASSOCIATIONS An association is a relationship between classes that indicate some meaningful and interesting relationship. It’s represented by a line with a name. Properly naming associations is important to enhance understanding: use verb phrase. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 3 ASSOCIATIONS An optional "reading direction arrow" indicates the direction to read the association name; it does not indicate direction of visibility or navigation OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 4 ASSOCIATIONS There may be more than one associations between classes (this is not uncommon). OwnsStock Person * * Company WorksFor * 0..1 OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 5 ASSOCIATIONS Associations are usually implemented by a reference from an object to another. Associations are inherently bidirectional. They can be traversed in either direction. A person WorksFor a company and a company Employs a person. Associations could be unidirectional. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 6 MULTIPLICITY Multiplicity specifies the number of instances of one class that may relate to a single instance of an associated class. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 7 MULTIPLICITY Multiplicity exposes hidden assumptions in the model.. For example, if a person WorksFor a company, can he work for more than one company? In other words, is it one-to-one or one-to-many association? OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 8 MULTIPLICITY VALUES OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 9 LINKS AND ASSOCIATIONS A link is a physical or conceptual connection among objects For example John works for GE company. An association is a relationship between classes and represents group of links. For example, a person WorksFor a company. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 10 LINKS AND ASSOCIATIONS OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 11 LINKS AND ASSOCIATIONS OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 12 ROLES Each association End can be labeled by a role. This makes understanding associations easier. They are especially important for Self-associations between objects of the same class. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 13 SELF ASSOCIATIONS (RECURSIVE-ASSOCIATIONS) Pre-req. 0..2 Course OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 14 ASSOCIATION CLASSES An Association class is an association that is also a class (has attributes and operations) OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 15 ASSOCIATION CLASSES Many-to-may associations provide compelling rationale for association classes. accessPermission is a joint property of File and User and cannot placed in one of them only. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 16 EXERCISES Sketch a class diagram for the following: Each person works for a company receives a salary and has a job title. The person can work for more than one company. The boss evaluates the performance of each worker. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 17 EXERCISES Sketch a class diagram for the following: A user may be authorized on many workstations. Each authorization has a priority and access privileges. A user has a home directory for each authorized workstation, but several workstations and users can share the same home directory. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 18 ASSOCIATION CLASSES VS. ORDINARY CLASSES Association class OwnsStock has only one occurrence for each pair of person and company (i.e. only one link exists between pair of person and company objects ). In contrast there can be any number of purchase objects for each pair of person and company objects.OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 19 QUALIFIED ASSOCIATION A qualified association is an association in which an attribute called the qualifier is used to reduce a many relation to one relation on the other end. Not qualified 1 * File Directory filename qualified 1 0..1 Directory filename File OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 20 QUALIFIED ASSOCIATION A bank has many accounts [0..*] But a bank + account number gives only one account Account Number is attribute of account class. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 21 A SAMPLE CLASS MODEL Model the classes in a system that represents flights. Each city has at least an airport. Airlines operate flights from and to various airports. A flight has a list of passengers, each with a designated seat. Also a flight uses one of the planes owned by the operating airline. Finally a flight is run by a pilot and a co-pilot. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 22 The order of modeling is: Define classes Define associations Define multiplicity OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 23 A SAMPLE CLASS MODEL Model the classes in a system that represents flights. Each city has at least an airport. Airlines operate flights from and to various airports. A flight has a list of passengers, each with a designated seat. Also a flight uses one of the planes owned by the operating airline. Finally a flight is run by a pilot and a co-pilot. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 24 A SAMPLE CLASS MODEL Flights City Airlines List of passengers Seat Planes Pilot and a Co-Pilot. City Airline Pilot Plane Passenger Airport Flight Seat OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 25 A SAMPLE CLASS MODEL Model the classes in a system that represents flights. Each city has at least an airport. Airlines operate flights from and to various airports. A flight has a list of passengers, each with a designated seat. Also a flight uses one of the planes owned by the operating airline. Finally a flight is run by a pilot and a co-pilot. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 26 A SAMPLE CLASS MODEL City has airport(s) Airline operates flights Flight from airport Flight to airport Flight has passengers Passenger has seat Flight uses airplane Airplane owned by airline Flight run by pilot & co-pilot 1 owns * 1 City Airline Pilot Plane 1 2 1 has 1 fly * has operates uses Seat * * * * Airport 1 from * Flight Passenger * has * 27 1 to * A SAMPLE CLASS MODEL Add some suitable attributes and operations to the classes of the flight system. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 28 FURTHER CLASS CONCEPTS AGGREGATION RELATION Aggregation is a special kind of association where an aggregate object contains constituent parts. It’s has-a relationship OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 29 FURTHER CLASS CONCEPTS COMPOSITION RELATION Composition is a special kind of association in which a constituent part belongs at most to one assembly and exists only if the assembly exists. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 30 FURTHER CLASS CONCEPTS AGGREGATION & COMPOSITION RELATION OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 31 FURTHER CLASS CONCEPTS GENERALIZATION, SPECIALIZATION AND INHERITANCE Generalization is the relationship between class (the superclass) and one or more variations of the class (the subclass). The superclass holds common attributes, operations and associations. The subclasses add specific attributes, operations and associations. Each subclass is said to inherit the features of the its superclass. Inheritance is called “is-a” relationship. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 32 GENERALIZATION, SPECIALIZATION AND INHERITANCE The terms generalization, specialization and inheritance refer to aspects of the same idea, they are used in place of one another. Generalization refers to the super-class generalizing its subclasses Specialization refers to the fact that the subclass specializes or refines the super-class, Inheritance refers to the mechanism for implementing generalization / specialization. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 33 GENERALIZATION, SPECIALIZATION AND INHERITANCE Generalization is transitive across an arbitrary number of levels. An ancestor is parent or grandparent class of a class. A descendant is a child or grandchild of a class. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 34 35 35 USE OF GENERALIZATION Generalization serves three purposes. 1. Supporting polymorphism. 2. Structuring the description of objects and their relation to each other based on their similarity and differences. 3. Reusing code. You inherit code from super-classes or from a class library automatically with inheritance. Reuse is more productive than repeatedly writing code form scratch. When reusing code, you can adjust the code if necessary to get the exact desired behavior. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 36 OVERRIDING FEATURES A subclass may override a superclass feature by defining a feature with the same name. The overriding feature (in the sub-class) replaces the overridden feature (in the super-class) Never override a feature so that it is inconsistent with the original inherited feature. Overriding should preserve the attribute type, number and type of arguments of an operation and its return type. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 37 GENERALIZATION, SPECIALIZATION AND INHERITANCE Shape - color: int + Shape (int): void + getColor (): int + setColor (int): void + getArea(): float Rectangle Circle Square - length: float - radius: float - side: float - width: float + Circle(int, int): void + Square(int, int): void + Rectangle ……. + getRadius(): float + getSide(): float + getArea(): float + setRadius(): float + setSide(): float + getArea(): float + getArea(): float side2 38 length x width x radius2 FURTHER CLASS CONCEPTS ABSTRACT CLASS An incomplete class that cannot be instantiated. 39 OBJECT DIAGRAMS An object model: is a collection of objects, where every object in the model is an instance of a class in the class model. o it represents a snapshot of the system at one instant in time. o no object can have attributes or relationships that are not in the class model. An object model may be thought of as an instantiation of a class model. o It shows concrete instances of relevant classes & relationships. o It indicates what connections do exist at that instant in time. o Is visually represented using an object diagram. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 40 OBJECT DIAGRAMS.. ELEMENTS An object diagram: is a visual representation of an object model, showing a snapshot of the system at some point in time. The elements of this object diagram are: ❖ Rectangular boxes representing objects. o Inside each box you add: o ObjectName : ClassName which are underlined. o Names and values of useful attributes in a second compartment (Optional) o Lines drawn between objects are called links. o They represent particular cases of one object ‘knowing about’ another object. o Each link is an instance of an association (a relationship between classes). OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 41 OBJECT DIAGRAMS.. AN EXAMPLE An Object diagram of a hotel system OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 42 CLASS MODELING TIPS The following categories are useful sources of relevant objects: ◦ Tangible objects: the physical things in the domain such as rooms, bills, books and vehicles; ◦ Roles: the roles played by people in the domain, such as employees, guests and members; ◦ Events: the circumstances, episodes, interactions, happenings and significant incidents, such as room reservations, vehicle registrations, orders, deliveries and transactions; ◦ Organizational units: the groups to which people belong, such as accounts departments, production teams and maintenance crews. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 43 CLASS MODELING TIPS Scope: A model represents the relevant aspects of the problem. Exercise judgment in deciding which objects to show and which ones to ignore. Simplicity: Make the model as simple as possible. Simpler models are easier to understand and develop. Diagram Layout: Draw your diagram in a way that is easy to understand. Avoid cross lines. Position important classes where they are visually prominent. Names: Carefully choose class names. Choose descriptive names and use singular nouns to name classes. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 44 DIVIDE & CONQUER: MODULARIZATION The only way till now to reduce the complexity of systems is modularization. Modularization means decompose system or problem to a smaller sub-system with separate boundaries (modules). Examples of modules are: - Whole programs or applications; - Software libraries; - Procedures & functions in classical PLs; - Classes, in an OO PL such as Java. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 45 DIVIDE & CONQUER: MODULARIZATION Modules in themselves are not “good”.. Must design them to have good properties.. Good modularization must have: Maximal relationships within modules (Cohesion) Minimal relationships between modules (Coupling) OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 46 DIVIDE & CONQUER: MODULARIZATION There are two forms of decomposition: (a) Projection: dependent modules, common elements between modules. (b) Partitions: independent modules, which are easy to use. Note: Usually even if the modules sound separate there are relationships between them which must be taken into consideration through the software development and during maintenance. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 47 COHESION Definitions: The degree to which all elements of a component are directed towards a single task. The degree to which all elements directed towards a task are contained in a single component. The degree to which all responsibilities of a single class are related. Internal glue with which component is constructed. All elements of component are directed toward and essential for performing the same task. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 48 High Cohesion Functional TYPES OF COHESION Sequential Communicational Procedural Temporal Logical Coincidental Low OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 49 COINCIDENTAL COHESION Def: Parts of the component are unrelated (unrelated functions, processes, or data).. Module performs multiple, completely unrelated actions.. o Parts of the component are only related by their location in source code. o Elements needed to achieve some functionality are scattered throughout the system. o Accidental o Worst form OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 50 FUNCTIONAL COHESION Def: Every essential element to a single computation is contained in the component.. Module performs exactly one action.. o Every element in the component is essential to the computation. o Ideal situation. o What is a functionally cohesive component? One that not only performs the task for which it was designed, but.. it performs only that function and nothing else. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 51 COUPLING o The degree of dependence such as the amount of interactions among components. No dependencies Loosely coupled Highly coupled some dependencies many dependencies OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 52 COUPLING.. EXAMPLES A component depends on another if a change to one requires a change to another. Examples: Circular Dependency; a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive. Suppose we have a class called A which has class B’s Object (in UML terms: A HAS B). At the same time class B is also composed of an Object of class A (in UML: terms B HAS A). Obviously this represents a circular dependency because while creating an object of A, the compiler must know of B.. On the other hand, while creating an object of B, the compiler must know of A. Chain Dependency; Module A depends on B and B depends on C (chain of interdependent modules).. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 53 COUPLING.. EXAMPLES o One component modifies another. o More than one component share data such as global data structures. o Component passes a data structure to another component that does not have access to the entire structure. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 54 CONSEQUENCES OF COUPLING High coupling Components are difficult to understand in isolation. Changes in component ripple to others. Components are difficult to reuse. Need to include all coupled components. Difficult to understand. Low coupling May incur performance cost. Generally faster to build systems with low coupling. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 55 PACKAGE DIAGRAM Packages let you organize large models so that they are more understandable. OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 56 PACKAGE DIAGRAM Example of packages: Use-Cases are organized by actors OO MODELLING, UML STATIC DIAGRAMS (CLASS, OBJECT, & PACKAGE DIAGRAMS) 57