Analysis and Design with UML: Classes and Relationships Bina Ramamurthy 5/28/2016 B.Ramamurthy 1 Introduction Purpose of this part is to transform the requirement analysis represented by the usecase diagram(s) to design diagrams using static analysis. The result of this phase is UML class diagrams consisting of classes and relationship among them. We will study techniques to discover classes and the UML notations for representing class diagrams. 5/28/2016 B.Ramamurthy 2 Topics for Discussion Classes, Responsibility, Collaboration (CRC) method for discovering classes and assigning responsibility. Class-based Analysis and Design: UML notations, class diagrams. Case Studies. 5/28/2016 B.Ramamurthy 3 CRC Card Method Although proponents of the object paradigm often say that identifying objects is a simple and intuitive process, a number of noted experts admit that this is not always true! …The solution is to use the CRC process to determine the classes necessary to the system as part of the design process for the application. CRC (classes, responsibility, and collaboration) cards can be used to visualize and test different class-based models during the design phase. It is a proven technique used and advocated by leading methodologists. 5/28/2016 B.Ramamurthy 4 CRC Card Class Name Collaborations Responsibilities 5/28/2016 B.Ramamurthy 5 CRC Card Example Weather Station Responsibilities 1. Select 24hr/Current 2. Set Date Time 3. Display Current 1. Temp(T) 2. Wind (W) 3. Pressure (P) 4. Humidity (H) 4. Display 24hours 1. Hi/Lo for (TWPH) 5. Display Trends in TWPH 6. Calibrate 5/28/2016 B.Ramamurthy Collaborations User Interface(UI) Date Time Temp Wind Pressure Humidity Calibrator 6 CRC Card: UserInterface UserInterface Collaborators Responsibilities Keypad 1. 2. 3. 4. 5/28/2016 Display Input date Input time Input selection Display data Temp Wind Pressure Humidity B.Ramamurthy 7 CRC Card: Keypad Collaborators Keypad Date Responsibilities Time 1. Store date 2. Store time 3. Store selection 5/28/2016 Selection B.Ramamurthy 8 CRC Card: Temperature Temperature Collaborations Responsibilities T.Device 1. Measure and Record temperature 2. Determine and record Hi/Lo 3. Determine trend StatDataBase Date Time 5/28/2016 B.Ramamurthy 9 Class Discovery The entries in the collaborations column are possible classes or non-software entities. In this case these are: UserInterface, Display, Tempertaure, Wind, Pressure, Humidity, StatDataBase, Selection, Date, Time, Keypad, Callibrator. The responsibility of designing one or more of these classes can be assigned to the members of the group who participated in this discovery process. On to relations among classes and class diagrams. 5/28/2016 B.Ramamurthy 10 Classes OO paradigm supports the view that a system is made up of objects interacting by message passing. Classes represent collection of objects of the same type. An object is an instance of a class. A class is defined by its properties and its behaviors. A class diagram describes the static view of a system in terms of classes and relationships among the classes. 5/28/2016 B.Ramamurthy 11 Discovering Classes (Alternative) Underline the nouns in a problem statement. Using the problem context and general knowledge about the problem domain decide on the important nouns. Design and implement classes to represent the nouns. Underline the verbs. Verbs related to a class may represent the behavior of the class. 5/28/2016 B.Ramamurthy 12 Examples Drawing package: Design a user interface for drawing various shapes: circle, square, rectangle. Football scores: Keep track of football score. General purpose counter: To keep of track of count for various applications. Library: Books, different categories of books, details of student borrower, library personnel. 5/28/2016 B.Ramamurthy 13 Designing Classes A class represents a class of objects. A class contains the data declarations (“parts”) and methods (“behaviors” or “capabilities” ). OO Design: Class properties or characteristics are answers to “What is it made of?” (It has a ____, ____, etc.) Behaviors, capabilities or operations are answers to “What can it do?” (verbs in the problem) 5/28/2016 B.Ramamurthy 14 Classes are Blueprints A class defines the general nature of a collection of objects of the same type. The process creating an object from a class is called instantiation. Every object is an instance of a particular class. There can be many instances of objects from the same class possible with different values for data. A class structure implements encapsulation as well as access control: private, public, protected. 5/28/2016 B.Ramamurthy 15 Example objects Object References redRose class Rose blueRose 5/28/2016 class B.Ramamurthy 16 Class Diagram : Automobile Automobile 5/28/2016 public: seat seatBelt accelerator private: sparkPlugs gear protected: gloveCompartment public: startEngine brake protected: transmission private: fuelInjection B.Ramamurthy 17 Automobile Class Using Rational Rose Tool Automobile seat seatBelt acceleratorPedal sparkPlugs gear gloveCompartment startEngine( ) brake( ) transmission( ) fuelInjection( ) 5/28/2016 B.Ramamurthy 18 Access Control Public, protected, private Public properties and behaviors are available to any other object to use/invoke Private: available only within the objects. Protected: available within the objects and to the class hierarchy inherited from the class. (We will discuss more about this when dealing with OO concept Inheritance.) 5/28/2016 B.Ramamurthy 19 Relationships Typically an application consists of many related classes. Commonly used relationships include: associations, aggregations, and generalizations. We will look into other relationships later. (Focus in this lecture will be on general class diagrams.) 5/28/2016 B.Ramamurthy 20 Association An association is a connection between classes, a semantic connection between objects of classes involved in the association. Association typically represents “has a” or “uses” relationships. Indicated by a line, 5/28/2016 sometimes with arrow indicating unidirectional relationship, adorned by the name of the relation, and the ends of the line adorned by cardinality of relationship and optionally by the roles connected to each class. B.Ramamurthy 21 Association : Examples Person Uses Computer A person uses a computer. Person Owns 0..* Car A person may own many (zero..many) cars. 5/28/2016 B.Ramamurthy 22 Roles in Association drives Person driver company car Car A person (driver) drives a (company) car. wife Person husband married to 5/28/2016 B.Ramamurthy 23 Aggregation Aggregation represents a relation “contains”, “is a part of”, “whole-part” relation. Indicated by a line adorned on the “whole” by a hollow diamond 5/28/2016 Along with name of relationship and Cardinality. B.Ramamurthy 24 Aggregation: Example League contains Team * Membership aggregation: A league is made up of Many teams. 4 Auto made of 1 * 5/28/2016 wheel engine Strong aggregation. part B.Ramamurthy 25 Generalization Generalization is a relationship between a general and a specific class. The specific class called the subclass inherits from the general class, called the superclass. Public and protected properties (attributes) and behaviors (operations) are inherited. Design representation “inheritance” OO concept. 5/28/2016 B.Ramamurthy 26 Generalization: Symbol It represents “is a” relationship among classes and objects. Represented by a line with an hollow arrow head pointing to the superclass at the superclass end. 5/28/2016 B.Ramamurthy 27 Generalization: Example Vehicle Car 5/28/2016 Boat B.Ramamurthy Truck 28 Combined Example drives Person 0..* Car 5/28/2016 Vehicle Boat B.Ramamurthy Truck 29 Summary We looked at Class Diagrams which is very important part of UML Model, perhaps the only model used in many design representation. We also looked at two methods for class discovery(/analysis): CRC Card Method and Nouns-Verbs. Class diagrams directly represent the implementation (code) classes and the relationship among them. 5/28/2016 B.Ramamurthy 30