Giới thiệu về mô hình hóa tĩnh ppsx

44 354 0
Giới thiệu về mô hình hóa tĩnh ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

More About Static Modeling Chapter 6 Static modeling uses class diagrams and object diagrams to represent the static constituents of a software system. These diagrams use various types of notations for depicting different types of classes and the relationships between them. In addition, these diagrams use special types of constructs called interfaces. This chapter discusses the various types of classes and examines the relationships between them. It also explains the concept of interfaces and the different UML notations used to depict interfaces. In this chapter, you will learn to:  Identify various types of classes and the relationships between them  Identify Interfaces Objectives ¤NIIT More About Static Modeling 6.3 After you identify the classes from the problem statement or use cases, you can classify the identified classes into various types. This enables you to make the software system reusable and manageable. UML enables you to classify classes into the following types:  Abstract class  Parameterized class  Factory class  Self-linked class Abstract Class An abstract class is a class that does not have any direct instances. However, the classes inherited from an abstract class can have direct instances. An abstract class is used to define the common features and common behavior of a set of subclasses. Consider the example of an Automobile class. Some of the attributes of the Automobile class are:  Has wheels  Has an engine  Has a color You can derive two subclasses Bus and Car from the Automobile class. In this case, you may never be required to create an object of the Automobile class in your program. Instead, you may want to use it only because you want it to act as a superclass that defines the common attributes for the Car and Bus classes. In this case, you can declare the Automobile class as an abstract class. The UML notation for an abstract class is same as that of a simple class. However, the name of the class is italicized to indicate that the class is an abstract class. Identifying Types of Classes and Relationships Between Classes Types of Classes 6.4 More About Static Modeling ¤NIIT N ote The following figure depicts the UML notation for an abstract class. Abstract Class Parameterized Class A parameterized class, also called template class, provides a mechanism that enables you to use operations and classes to work with different data types. A parameterized class consists of type parameters that are unbounded, which means that the data types of the parameters are not defined in the parameterized class. You cannot create the objects of a parameterized class. To use the functions defined in the parameterized class, you need to realize the parameterized class by using classes. The type parameters are bounded to the class that realizes the parameterized class. In other words, the datatypes of the type parameters are defined in the classes that realize the parameterized class. Consider an example where you need to create a class for maintaining a list of items. It should be possible to use the same class to maintain a list of integers, a list of strings, or a list of products. In such a case you can create the class as a parameterized class. In the preceding example, each entry corresponding to a product will include the product ID and the product name. To use the functions of the parameterized class, you need to pass different types of objects to instantiate the List class. An example of a parameterized class is C# Generics. The following program segment in C# illustrates the realization of the List parameterized class by using three different types, integer, string, and Product, where Product is a user- defined class containing two attributes, prod_id and prod_name. public class List<T> { private T[] list = new T[5]; private int n = 0; //No. of elements in list ¤NIIT More About Static Modeling 6.5 public void Add(T input) { if (n < 5) list[n++] = input; } } class TestList { private class Product { private int prod_id; private string prod_name; public Product(int id, string name) { prod_id = id; prod_name = name; } } static void Main() { // Declare a list of type int List<int> list1 = new List<int>(); list1.Add(20); list1.Add(30); list1.Add(45); // Declare a list of type string List<string> list2 = new List<string>(); list2.Add("hello"); list2.Add("welcome"); list2.Add("bye"); // Declare a list of type ExampleClass List<Product> list3 = new List<Product>(); Product p1 = new Product (1, "Baking Powder"); Product p2 = new Product (2, "Salt"); Product p3 = new Product (3, "Sugar"); list3.Add(p1); list3.Add(p2); list3.Add(p3); } } 6.6 More About Static Modeling ¤NIIT You represent a parameterized class by a dashed rectangle that you place on the upper right corner of the class. The dashed rectangle contains the list of formal parameters using the syntax, name: type. The following figure shows the class, List, with its formal parameters of the type, P. Parameterized Class Factory Class A class that has multiple objects having the same attribute values is known as a factory class. To represent the multiple objects of a factory class, you can use a symbol of overlapping rectangles, as shown in the following figure. Object Diagram for Factory Class An example of a factory class is an EJB factory class that creates multiple instances of EJB in an instance pool of the J2EE middle tier server. Multiple instances are used whenever requests for the same arrive at the server. Self-Linked Class A class that has objects, which fulfill more than one role, is called a self-linked class. The following figure shows the representation of a self-linked class. Object Diagram for a Self-Linked Class Consider an example for a self-linked class. In an organization, if an employee, Stevens, acts as both, project manager and team leader, then the two positions are linked. List ElementType: T Add (T) ¤NIIT More About Static Modeling 6.7 The instances of Stevens in the Employee class are linked, as shown in the following figure. Example of a Self-Linked Class In addition to generalization, dependency, and association relationships, you can also represent the following relationships among classes and objects:  Recursive aggregation  Qualified association Recursive Aggregation Recursive aggregation is an association relationship between two objects of the same class. Consider an employee information system in which ProjectManager and Developer are two objects of the Employee class. The developer reports to the project manager. Therefore, the ProjectManager and Developer objects of the Employee class share a recursive aggregation relationship, as shown in the following figure. Recursive Aggregation in an Object Diagram In the preceding figure, an object diagram depicts the recursive aggregation among the objects of the Employee class. Recursive Aggregation and Qualified Association Relationships 6.8 More About Static Modeling ¤NIIT You can also depict recursive aggregation in a class diagram as shown in the following figure. Recursive Aggregation in a Class Diagram Qualified Association Qualified association is an association relationship that relates an object of a class to a particular object or a set of objects of another class. You use a value, known as qualifier, to distinguish the objects of one class from another. A qualifier can be an attribute of the class. For example, many students are associated to a university. To distinguish a particular student from other students, you need to map a particular enrolment number to the student. For this reason, the University class uses the Enrolment Number attribute of the Student class as the qualifier to distinguish a particular student from other students. The University and Student classes share an association relationship, as shown in the following figure. Qualified Association ¤NIIT More About Static Modeling 6.9 In UML, a derived element, as the name suggests, can be derived from one or more other elements of the same type. It is logically redundant to depict a derived element in a design model. However, you can use the derived elements to model an explicit detailed design. To represent a derived element in a class diagram, you place a slash (/) before the name of the element. A derived element can be of two types:  Derived attribute  Derived association Derived Attribute You calculate the value of a derived attribute from the value of the other attributes of the object. The formula to calculate the derived attribute is represented as a constraint in the class diagram. For example, the Employee class may have DateOfBirth and Age as attributes. The DateOfBirth attribute is used to derive the value of the Age attribute. You use a dotted straight line with the term, <<derive>>, to represent the relationship between the DateOfBirth and Age attributes. The following figure shows the representation of a derived attribute in a UML notation. Derived Attribute Derived Elements 6.10 More About Static Modeling ¤NIIT Just a minute: Derived Association You can deduce a derived association from the other associations in a class diagram. For example, the Employee class is associated to the Department class and the Department class is a part of the Organization class. Therefore, the Employee class is also associated to the Organization class. You can derive this association from the associations between the Department and Employee classes and the Organization and Department classes. The following figure shows the representation of a derived association in a UML notation. Derived Association Which of the following is an association relationship between two objects of the same class? 1. Recursive aggregation 2. Derived association 3. Qualified association 4. Derived attribute Answer: 1. Recursive aggregation

Ngày đăng: 12/08/2014, 18:22

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan