Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Diagram. A class diagram describes the classes and the association (static relationship) between these classes ( 描述系統中物件 的 type(class) ,以及這些 class.

Similar presentations


Presentation on theme: "Class Diagram. A class diagram describes the classes and the association (static relationship) between these classes ( 描述系統中物件 的 type(class) ,以及這些 class."— Presentation transcript:

1 Class Diagram

2 A class diagram describes the classes and the association (static relationship) between these classes ( 描述系統中物件 的 type(class) ,以及這些 class 的靜態關連.) A class diagram describes the classes and the association (static relationship) between these classes ( 描述系統中物件 的 type(class) ,以及這些 class 的靜態關連.) association (for example, a customer may rent a number of videos) association (for example, a customer may rent a number of videos) subtypes ( a nurse is a kind of person) subtypes ( a nurse is a kind of person)

3 Order dateReceived isPrepaid number:string price:Money dispatch() close Order Line Quantity:integer price:Money isSatisfied:bool Customer name address creditRating():String Corporate customer contractName creditRating creditLimit remind() billForMonth(int) Personal Customer CreditCard# Employee Product *1 1 * 1* line items {if Order.customer.creditRati ng is “poor”, then Order.isPrepaid must be true } * 0..1 {creditRating()= “poor”} sale rep Multiplicity:mandatory Multiplicity:many-valued association constraints attributesgeneralization class Multiplicty:optional Role name

4 navigability See next fig See next fig In specification model, an Order has a responsibility to tell you which Customer it is for, but not conversely In specification model, an Order has a responsibility to tell you which Customer it is for, but not conversely In implementation diagram, this would indicate that Order contains a pointer to Customer In implementation diagram, this would indicate that Order contains a pointer to Customer

5 Order dateReceived isPrepaid number:string price:Money dispatch() close Order Line Quantity:integer price:Money isSatisfied:bool Customer name address creditRating():String Corporate customer contractName creditRating creditLimit remind() billForMonth(int) Personal Customer CreditCard# Employee Product *1 1 * 1* line items {if Order.customer.creditRati ng is “poor”, then Order.isPrepaid must be true } * 0..1 {creditRating()= “poor”} sale rep navigability

6 Generalization Generalization (In OOP term, is inheritance) Generalization (In OOP term, is inheritance) see personal and corporate customers see personal and corporate customers Using polymorphism, the corporate customer may respond to certain commands differently from another customer Using polymorphism, the corporate customer may respond to certain commands differently from another customer

7 Operations Most obviously correspond to the methods on a class Most obviously correspond to the methods on a class UML syntax is UML syntax is visibility name (parameter list): return-type-expression {property string} visibility name (parameter list): return-type-expression {property string}

8 Class Diagrams: Advanced Concepts

9 Object Diagram A snapshot of the objects in a system at a point in time Often called instance diagram Let’s see the class diagram first Party location Organization Person * children 1 parent

10 Object Diagram engineering:Organization location=“boston” tools:Organization location=“Chicago” apps:Organization location=“Saba” John:Person location=“Champaign” Don:Person location=“Champaign” parent This is associations not generalization or inheritance this is object not class

11 Class Scope Operations and Attributes Order getNumber getNextNewNumber instance scope class scope Class scope is equivalent to static members in C++

12 Multiple and Dynamic Classification Female Male Patient Physio- therapist Nurse Doctor Family Doctor Surgeon sex {complete} role patient Discriminator

13 Multiple Classification Don’t confuse with multiple inheritance. Don’t confuse with multiple inheritance. You should make it clear which combinations are legal by using a discriminator You should make it clear which combinations are legal by using a discriminator

14 Dynamic Classification Person Male Female Salesman Engineer Manager Job > sex {complete}

15 Dynamic Classification allow object to change type within the subtyping structure allow object to change type within the subtyping structure useful for conceptual modeling useful for conceptual modeling How to implement it? See Fowler 1997 How to implement it? See Fowler 1997

16 Aggregation and Composition Aggregation is the part-of relationship Aggregation is the part-of relationship difficult things – what is the difference between aggregation and association? difficult things – what is the difference between aggregation and association? It is vague and inconsistent It is vague and inconsistent Anyway, UML decide to include aggregation and a stronger variety of aggregation called composition Anyway, UML decide to include aggregation and a stronger variety of aggregation called composition

17 Point Style color isFilled Polygon Circle radius {ordered} 3..* * 1 1 * 1 Composition aggregation

18 Composition With composition, the part object may belong to only one whole; further, the parts are usually expected to live and die with the whole With composition, the part object may belong to only one whole; further, the parts are usually expected to live and die with the whole deletion of the whole is considered to cascade to the part deletion of the whole is considered to cascade to the part In previous graph, deleting a polygon would caused its associated Points to be deleted, but not the associated Style. In previous graph, deleting a polygon would caused its associated Points to be deleted, but not the associated Style.

19 Alternative Notation for Composition CirclePolygon Point 1 {ordered} 3..* Style 1 1 **

20 Derived Associations and Attributes Derived Associations and derived attributes can be calculated from other associations and attributes. Derived Associations and derived attributes can be calculated from other associations and attributes. for example, an age attribute of a Person can be derived if you know that person’s birthday. for example, an age attribute of a Person can be derived if you know that person’s birthday.

21 Account /balance:Money Entry amount:Money Summary Account Detail Account /entries* {balance= sum of amounts of entries} Components {hierarchy} * 0..1 Derived attribute Derived Association 1

22 Note Entry objects are attached to detail Accounts Entry objects are attached to detail Accounts The balance of an Account is calculated as the sum of Entry accounts The balance of an Account is calculated as the sum of Entry accounts A Summary Account’s entries are the entries of its components determined recursively A Summary Account’s entries are the entries of its components determined recursively

23 Interfaces and Abstract Classes Programming language that use a single construct, the class, which contains both interface and implementation. Programming language that use a single construct, the class, which contains both interface and implementation. When you subclass, you inherit both. When you subclass, you inherit both. A pure interface, as in Java, is a class with no implementation and, therefore, has operation declarations but no method bodies and no fields. A pure interface, as in Java, is a class with no implementation and, therefore, has operation declarations but no method bodies and no fields.

24 For example interface Stack { boolean Push(Object); Object Pop(); } Class MyStack implements Stack boolean Push(Objects) { …….. } Object Pop() { …….. return xxxx ; } Somewhere in initialization Stack S = new MyStack() ; Somewhere in the code S.push(a); S.push(b); S.pop(a); S.pop(b);

25 text Editor Window {abstract} toFront() toBack() Win98 Window toFront() toBack() X11 Window toFront() toBack() Mac Window toFront() toBack() Dependency

26 InputStream DataInputStream > DataInput OrderReader Realization generalization dependency

27 Abstract Class and Interface two are similar two are similar abstract class allows you to add implementation of some of the methods abstract class allows you to add implementation of some of the methods an interface forces you to defer definition of all methods an interface forces you to defer definition of all methods

28 Classification and Generalization People often talk subtyping as the “is a” relationship People often talk subtyping as the “is a” relationship beware of that way of thinking beware of that way of thinking for example 1. Shep is a border Collie 2. A border Collie is a Dog 3. Dogs are animals 4. A border Collie is a Breed ( 品種 ) 5. Dog is a Species ( 物種) for example 1. Shep is a border Collie 2. A border Collie is a Dog 3. Dogs are animals 4. A border Collie is a Breed ( 品種 ) 5. Dog is a Species ( 物種) if you try combination 2 and 5 “A border Collie is a Species”. This is not good if you try combination 2 and 5 “A border Collie is a Species”. This is not good The reasons? The reasons? some are classification and some are generalization some are classification and some are generalization Generalization is transitive and classification is not Generalization is transitive and classification is not “Dog are a kind of animals” is better “Dog are a kind of animals” is better

29 Qualified Associations equivalent to associative arrays, maps, and dictionaries equivalent to associative arrays, maps, and dictionaries an example, there maybe one Order Line for each instance of Product an example, there maybe one Order Line for each instance of Product Conceptually, you cannot have two Order Lines within an Order for the same product Conceptually, you cannot have two Order Lines within an Order for the same product Order Order Line amount:Number product 0..1 line item

30 Qualified association would imply an interface like class Order { public OrderLine getLineItem (Product aProduct);; public void addLineItem (Number amount, Product forProduct); }

31 Order Order Line amount:Number product line item You can have multiple OrderLines per Product but access to the Line Items is still indexed by Product You can have multiple OrderLines per Product but access to the Line Items is still indexed by Product use an associative array or similar data structure to hold the order lines use an associative array or similar data structure to hold the order lines Class Order { Class Order { private Map _lineItems ; private Map _lineItems ; *

32 Association Class Association class allow you to add attributes, operations, and other features to association Association class allow you to add attributes, operations, and other features to association Person Company Employment period:dateRange employer* association class 0..1

33 A person may work for a single company A person may work for a single company We need to keep information about the period of time that each employee works for each Company We need to keep information about the period of time that each employee works for each Company You can redraw: make Employment a full class in its own right You can redraw: make Employment a full class in its own right Employment period:dateRange PersonCompany 10..1* 1 /employer 0..1*

34 Parameterized Class Several language, noticeably C++, have the notion of a parameterized class or template Several language, noticeably C++, have the notion of a parameterized class or template ex class set { void insert (T newElement); void remove(T anElement); … … Set employSet ; ex class set { void insert (T newElement); void remove(T anElement); … … Set employSet ;

35 A define a parameterized class in UML Set insert(T) remove(T) T template Parameter template class

36 A use of a parameterized class Set insert(T) remove(T) T EmployeeSet > Bound Element

37 Visibility C++ C++ A public member is visible anywhere in the program and may be called by any object within the system A public member is visible anywhere in the program and may be called by any object within the system A private member may be used only by the class that defines it A private member may be used only by the class that defines it A protected member may be used only by (a) the class that defines it or (b) a subclass of that class A protected member may be used only by (a) the class that defines it or (b) a subclass of that class In Java In Java a protected member may be accessed by subclasses but also by any other class in the same package as the owning class a protected member may be accessed by subclasses but also by any other class in the same package as the owning class C++ C++ one C++ method or class can be made a friend of a class. A friend has complete access to all members of a class one C++ method or class can be made a friend of a class. A friend has complete access to all members of a class


Download ppt "Class Diagram. A class diagram describes the classes and the association (static relationship) between these classes ( 描述系統中物件 的 type(class) ,以及這些 class."

Similar presentations


Ads by Google