class diagram. a class diagram describes the classes and the association (static relationship)...
TRANSCRIPT
Class DiagramClass Diagram
Class DiagramClass Diagram
A class diagram describes the classes and A class diagram describes the classes and the association (static relationship) the association (static relationship) between these classes ( between these classes ( 描述系統中物件描述系統中物件的的 type(class)type(class) ,以及這些,以及這些 class class 的的靜態關靜態關連連 .).)association (for example, a customer may association (for example, a customer may
rent a number of videos)rent a number of videos)subtypes ( a nurse is a kind of person)subtypes ( a nurse is a kind of person)
OrderdateReceived
isPrepaidnumber:stringprice:Money
dispatch()close
Order LineQuantity:integer
price:MoneyisSatisfied:bool
Customername
addresscreditRating():String
Corporatecustomer
contractNamecreditRatingcreditLimitremind()
billForMonth(int)
PersonalCustomer
CreditCard#
Employee
Product
* 1
1
*
1*
lineitems
{if Order.customer.creditRatingis “poor”, then Order.isPrepaidmust be true }
*0..1
{creditRating()=“poor”}sale rep
Multiplicity:mandatoryMultiplicity:many-valued
association
constraints
attributes generalizationclass
Multiplicty:optional
Rolename
navigabilitynavigability
See next figSee next fig In specification model, an Order has a In specification model, an Order has a
responsibility to tell you which Customer it responsibility to tell you which Customer it is for, but not converselyis for, but not conversely
In implementation diagram, this would In implementation diagram, this would indicate that Order contains a pointer to indicate that Order contains a pointer to CustomerCustomer
OrderdateReceived
isPrepaidnumber:stringprice:Money
dispatch()close
Order LineQuantity:integer
price:MoneyisSatisfied:bool
Customername
addresscreditRating():String
Corporatecustomer
contractNamecreditRatingcreditLimitremind()
billForMonth(int)
PersonalCustomer
CreditCard#
Employee
Product
* 1
1
*
1*
lineitems
{if Order.customer.creditRatingis “poor”, then Order.isPrepaidmust be true }
*0..1
{creditRating()=“poor”}sale rep
navigability
GeneralizationGeneralization
Generalization (In OOP term, is Generalization (In OOP term, is inheritance)inheritance)
see personal and corporate customerssee personal and corporate customersUsing polymorphism, the corporate Using polymorphism, the corporate
customer may respond to certain customer may respond to certain commands differently from another commands differently from another customercustomer
OperationsOperations
Most obviously correspond to the methods Most obviously correspond to the methods on a classon a class
UML syntax isUML syntax is visibility name (parameter list): return-type-expression {property string}visibility name (parameter list): return-type-expression {property string}
Class Diagrams:Class Diagrams:Advanced ConceptsAdvanced Concepts
Object DiagramObject 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
Partylocation
OrganizationPerson
* children
1 parent
Object Object DiagramDiagram
engineering:Organizationlocation=“boston”
tools:Organizationlocation=“Chicago”
apps:Organizationlocation=“Saba”
John:Personlocation=“Champaign”
Don:Personlocation=“Champaign”
parent
parent
This is associationsnot generalization orinheritance
this is objectnot class
Class Scope Operations and Class Scope Operations and AttributesAttributes
Order
getNumbergetNextNewNumber
instancescope
class scope
Class scope is equivalent to static members in C++
Multiple and Dynamic Multiple and Dynamic ClassificationClassification
Female
Male
PatientPhysio-therapist
Nurse
Doctor
FamilyDoctor
Surgeon
sex {complete}
role
patient
Discriminator
Multiple ClassificationMultiple Classification
DonDon’’t confuse with multiple inheritance.t confuse with multiple inheritance.You should make it clear which You should make it clear which
combinations are legal by using a combinations are legal by using a discriminatordiscriminator
Dynamic ClassificationDynamic Classification
Person
Male
Female
Salesman
Engineer
Manager
Job <<dynamic>>
sex{complete}
Dynamic ClassificationDynamic Classification
allow object to change type within the allow object to change type within the subtyping structuresubtyping structure
useful for conceptual modelinguseful for conceptual modelingHow to implement it? See Fowler 1997How to implement it? See Fowler 1997
Aggregation and CompositionAggregation and Composition
Aggregation is the part-of relationshipAggregation is the part-of relationshipdifficult things difficult things –– what is the difference what is the difference
between aggregation and association?between aggregation and association? It is vague and inconsistentIt is vague and inconsistent Anyway, UML decide to include aggregation Anyway, UML decide to include aggregation
and a stronger variety of aggregation called and a stronger variety of aggregation called compositioncomposition
Point
Style
colorisFilled
Polygon
Circleradius
{ordered}
3..*
*
1 1
*
1
Composition
aggregation
CompositionComposition
With composition, the part object may belong to With composition, the part object may belong to only one whole; further, the parts are usually only one whole; further, the parts are usually expected to live and die with the wholeexpected to live and die with the whole
deletion of the whole is considered to cascade to deletion of the whole is considered to cascade to the partthe part
In previous graph, deleting a polygon would In previous graph, deleting a polygon would caused its associated caused its associated Points Points to be deleted, but to be deleted, but not the associated not the associated StyleStyle..
Alternative Notation for Alternative Notation for CompositionComposition
Circle Polygon
Point
1
Point
{ordered} 3..*
Style
1 1
* *
Derived Associations and Derived Associations and AttributesAttributes
Derived Associations and derived Derived Associations and derived attributes can be calculated from other attributes can be calculated from other associations and attributes.associations and attributes. for example, an age attribute of a Person can for example, an age attribute of a Person can
be derived if you know that personbe derived if you know that person’’s birthday.s birthday.
Account/balance:Money
Entryamount:Money
SummaryAccount
DetailAccount
/entries *
{balance= sum of amounts of entries}
Components{hierarchy} *
0..1
Derivedattribute
Derived Association
1
NoteNote
Entry objects are attached to detail Entry objects are attached to detail AccountsAccounts
The balance of an Account is calculated The balance of an Account is calculated as the sum of Entry accountsas the sum of Entry accounts
A Summary AccountA Summary Account’’s entries are the s entries are the entries of its components determined entries of its components determined recursivelyrecursively
Interfaces and Abstract ClassesInterfaces and Abstract Classes
Programming language that use a single Programming language that use a single construct, the class, which contains both construct, the class, which contains both interfaceinterface and and implementationimplementation..
When you subclass, you inherit both.When you subclass, you inherit both.A pure interface, as in Java, is a class with A pure interface, as in Java, is a class with
no implementation and, therefore, has no implementation and, therefore, has operation declarations but no method operation declarations but no method bodies and no fields.bodies and no fields.
For exampleFor exampleinterface 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);
text Editor
Window{abstract}
toFront()toBack()
Win98 Window
toFront()toBack()
X11 Window
toFront()toBack()
Mac Window
toFront()toBack()
Dependency
InputStream
DataInputStream
<<interface>>DataInput
OrderReader
Realization
generalization
dependency
Abstract Class and InterfaceAbstract Class and Interface
two are similartwo are similarabstract class allows you to add abstract class allows you to add
implementation of some of the methodsimplementation of some of the methodsan interface forces you to defer definition an interface forces you to defer definition
of all methodsof all methods
Classification and Classification and GeneralizationGeneralization
People often talk subtyping as the People often talk subtyping as the ““is ais a”” relationship relationship beware of that way of thinkingbeware of that way of thinking for examplefor example
1. Shep is a border Collie1. Shep is a border Collie2. A border Collie is a Dog2. A border Collie is a Dog3. Dogs are animals3. Dogs are animals4. A border Collie is a Breed (4. A border Collie is a Breed ( 品種品種 ))5. Dog is a Species (5. Dog is a Species ( 物種)物種)
if you try combination 2 and 5 if you try combination 2 and 5 ““A border Collie is a SpeciesA border Collie is a Species””. This . This is not goodis not good
The reasons?The reasons? some are classification and some are generalizationsome are classification and some are generalization
Generalization is transitive and classification is notGeneralization is transitive and classification is not ““Dog are a kind of animalsDog are a kind of animals”” is better is better
Qualified AssociationsQualified Associations
equivalent to associative arrays, maps, and dictionariesequivalent to associative arrays, maps, and dictionaries an example, there maybe one Order Line for each an example, there maybe one Order Line for each
instance of Productinstance of Product Conceptually, you cannot have two Order Lines within an Conceptually, you cannot have two Order Lines within an
Order for the same productOrder for the same product
Order
Order Line
amount:Numberproduct
0..1
line item
Qualified association would imply an interface like
class Order {
public OrderLine getLineItem (Product aProduct);;
public void addLineItem (Number amount, Product forProduct);}
Order
Order Line
amount:Numberproductline item
You can have multiple OrderLines per Product but You can have multiple OrderLines per Product but access to the Line Items is still indexed by Productaccess to the Line Items is still indexed by Product
use an associative array or similar data structure to hold use an associative array or similar data structure to hold the order linesthe order lines
Class Order {Class Order { private Map _lineItems ;private Map _lineItems ;
*
Association ClassAssociation Class
Association class allow you to add attributes, Association class allow you to add attributes, operations, and other features to associationoperations, and other features to association
Person Company
Employment
period:dateRange
employer*
association class
0..1
A person may work for a single companyA person may work for a single company We need to keep information about the period We need to keep information about the period
of time that each employee works for each of time that each employee works for each CompanyCompany
You can redraw: make Employment a full You can redraw: make Employment a full class in its own rightclass in its own right
Employment
period:dateRange
Person Company1 0..1 * 1
/employer0..1*
Parameterized ClassParameterized Class
Several language, noticeably C++, have the Several language, noticeably C++, have the notion of a parameterized class or templatenotion of a parameterized class or template
exexclass set <T> {class set <T> { void insert (T newElement); void insert (T newElement); void remove(T anElement); void remove(T anElement);…………Set <Employee> employSet ;Set <Employee> employSet ;
A define a parameterized class A define a parameterized class in UMLin UML
Set
insert(T)remove(T)
T
templateParameter
templateclass
A use of a parameterized classA use of a parameterized class
Set
insert(T)remove(T)
T
EmployeeSet<Employee>
<<bind>>
Bound Element
VisibilityVisibility C++C++
A public member is visible anywhere in the program and may be A public member is visible anywhere in the program and may be called by any object within the systemcalled by any object within the system
A private member may be used only by the class that defines itA private member may be used only by the class that defines it A protected member may be used only by (a) the class that A protected member may be used only by (a) the class that
defines it or (b) a subclass of that classdefines it or (b) a subclass of that class
In JavaIn Java a protected member may be accessed by subclasses but also a protected member may be accessed by subclasses but also
by any other class in the same package as the owning classby 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 one C++ method or class can be made a friend of a class. A friend has complete access to all members of a classfriend has complete access to all members of a class