Vous êtes sur la page 1sur 14

Object Modeling

Ordering:
 Usually objects on many sides of an association have no explicit order, and can be
considered as a set.
 Example 1: Workstation screen containing a number of overlapping windows. The windows
are explicitly ordered, so only the topmost window is visible at any point on the screen.
{Ordered}
Window Screen
Visible-on

 The ordering is an inherent property of association.


 An ordered set of objects on the many end of association is indicated by writing “{ordered}”
next to multiplicity dot for the node.
 This ordering is also a kind of imposing constraint (rule) to an association.
 Example 2: Bank teller serves n number of customers but in ordered manner.

{Ordered}
Bank teller Customer
Servers

Modeling association as a class:


 An association can have attributes and operations, just like a class. When this is the case we
have an association class.
 We can view an association class the same way as regular class and we use dotted line to
connect it to the association line in UML (unified modelling language).
 An association class can have association to other classes.
 It is useful to model an association as a class when links can participate in associations with
other class or when links are subjected to operations.
 Example 1: Authorization information for users on workstations
Users may be authorized on many workstations. Each authorization carries a priority and
access privileges, shown as link attributes. A user has a home directory for each authorized
workstation, but the same home directory can be shared among several workstations or
among several users.
Authorize on
User Workstation

Home directory
Authorization
Directory
Priority
Privileges

Start session ()

 Example 2: Plays-on
Player Team

Negotiated-by
Contract General
Manager

Qualified association:
 When an association’s multiplicity is one-to-many, a particular challenge often arises:
lookup.
 When an object from one class has to choose a particular object of another class in order to
fulfil a role in an association, the first class has to rely on a specific attribute to find the right
object. That attribute is typically an identifier, such as an ID_NUMBER.
 Example: We make reservation at a hotel; the hotel assigns us a confirmation number. If we
call with questions regarding reservation we have to specify confirmation number.
1 Finds *
Person or Reservation
clerk Confirmation number

 Qualifier symbol is a small rectangle adjoining the class that does the looking up.
 The idea is to effectively reduce a one-to-many multiplicity to a one-to-one multiplicity.
 Example 2: A directory contains many files. Within the context of the directory, the filename
specifies a unique file.

Directory File
Filename

Directory and File are object classes and filename is a qualifier.


It is more informative to be told that a directory and filename combine to identify a file,
rather than to be told that a directory has many files.
Reflexive Associations:
 Sometimes, a class is in association with itself. This can happen when a class has objects
that can play a variety of roles.
 Example 1: A car occupant can be either a driver or a passenger. In role of a driver, the
occupant drives zero or more car occupants who play the role of passenger.

Driver 1 Car occupant

0...4Passenger
Drives
 R.A’s are represented by drawing an association line from the class rectangle back to the
same class rectangle.
 Example 2: A directory may contain many other directories and may optionally be contained
in another directory.
Container Directory

Contents

Role names:
 A role is one end of an association.
 A binary association has 2 roles, each of which may have a role name.
 A role name is a name that uniquely identifies one end of an association.
 Roles provide a way of viewing a binary association as a traversal from one object to a set of
associated objects.
 Example 1: Person and Company
Specifies how Person and Company participate in association “Works-for”. A person
assumes the role of the employee with respect to company; a company assumes the role of
employer w.r.t a person.
Works-for
Person Company
Employee Employer

John TCS
Kiran BOSCH
Smith TCS
 A role names are written next to the association line near the class that plays the role.
 Role names are necessary for associations between 2 objects of the same class.
 Example 2: “Boss” and “Worker” distinguish the 2 employees participating in the
“Manages” association.
Boss Person

Manages
Worker

 Example 3: A directory may contain many other directories and may optionally be contained
in another directory. Each directory has exactly one user who is an owner, and many users
who are authorized to use the directory.
Owner Container
User Directory

Authorized user
Contents

Aggregation:
 The human is an entity that is obtained by putting together all the organs. This is a classic
example of aggregation.
 Aggregation is based on the principle of “A whole is a sum of parts”.
 It is used when we want to put create an entity by putting together all the existing entities.
For example hands, legs, face put together form a human.
 The process of putting together components to get the required entity is called
“Aggregation”.
 Aggregation is a “part-whole” or “a-part-of” relationship in which objects representing the
components of something are associated with an object representing the entire assembly.
 Aggregation is a special type of association with some extra semantics.
 Example 1: A document consists of many paragraphs, each of which consists of many
sentences.

Document Paragraph Character

 Aggregation is drawn like association, except a small diamond indicates the assembly end of
the relationships.
 Properties of aggregation:
1. Transitivity: if A is part of B and B is part of C then A is part of C.
Example: Carburettor is part of engine, and engine is part of car then carburettor is part
of car.
Car Engine Carburettor

2. Anti-symmetric: if A is part of B, then B is not part of A


Example: Wheel is part of car but car is not part of wheel

Car Wheel

 Example of multi-level aggregation: A microcomputer is composed of one or more monitors,


a system box, an optional mouse and a keyboard. In turn, a system box has a chassis, a CPU,
many RAM chips, and optional fan.

Microcomputer

Monitor System-box Mouse Keyboard

Chassis CPU RAM Fan

Aggregation Vs Association:
 Aggregation comes about because an entity consists of several components.
 Association on the other hand is the relationship between the entities.
 Example 1: An account is an aggregation of customer details, operational instructions,
business rules etc. the customer is not part of the account but has relationship to an account.

Account Customer
1..* Owned by

Customer Operational Business


details details rules

 Example 2: A company is an aggregation of its division, which are in turn aggregation of


their departments; a company is indirectly an aggregation of departments. A company is not
an aggregation of its employees, since company and person are independent objects of equal
importance.
Company Division Department

Works-for
Person

 Generally if classes are tightly bound by a whole-part relationship then the relationship can
be termed as aggregation.
 The decision to use aggregation or association is not well defined and is a matter of
judgement and is often arbitrary.

Generalization Inheritance and Specialization:


 Generalization and inheritance are powerful abstractions for showing similarities among
classes while preserving their differences.
 Generalization is the relationship between a class and one or more refined versions of it.
 The class being refined is called the superclass and each refined versions is called sub class.
 Example 1: Account is more general class which can be refined into savings account and
current account.
Account

Savings Current
Account Account

 Example 2: Vehicle

Car Boat Truck

 Generalization is sometimes called the “is-a “relationship because each instance of a


subclass is an instance of superclass as well.
 Each subclass not only inherits all the features of its ancestors but adds its own specific
attributes and operations as well.
 Generalization notation in OMT is a triangle connecting a superclass to its subclasses.
 A discriminator is an attribute of enumeration type that indicates which property of an object
is being abstracted by a particular generalization relationship.
 A discriminator must be an attribute of the superclass.
 Discriminators are written next to the generalization symbol.
 Example 3:
Account

Nature of account

Term Based Transaction


Based
Duration based Activity based

Short Term Long Term Current Account Savings Account

[Nature of account, duration based, activity based are called discriminators]


 If we start with a general class and refine it to produce classes that are more and more
specific the process is referred to as specialization.
 Specialization is a process of refining a class into more refined class.
 In terms of specialization, a super class and subclass are defined as follows:
1. The class being refined is called the super class.
2. The process of specialization results in creation of a subclass.
 The term inheritance, generalization and specialization all refer to aspects of the same idea
and are often used interchangeably.
 Generalization refers to the relationship among classes while inheritance refers to the
mechanism of sharing attributes and operations using the generalization relationship.
 Generalization and specialization are two different viewpoints of the same relationship,
viewed from the super class or from the subclass.

Aggregation Vs Generalization:
 Aggregation is not the same thing as generalization.
 Aggregation relates instances. Two distinct objects are involved; one of them is a part of the
other.
 Generalization relates classes and is a way of structuring the description of a single object.
Both superclass and subclass refer to properties of a single object.
 With generalization, an object is simultaneously an instance of the superclass and an
instance of the subclass.
 Both generalization and aggregation gives rise to a tree like structure.
 An aggregation tree is composed of object instances that are all parts of a composite object;
a generalization tree is composed of classes that describe an object.
 Aggregation is often called “a-part-of” relationship; generalization is often called “a-kind-of
“or “is-a” relationship.
 Example 1: Lamp

Base Cover Switch Wiring

Fluorescent Incandescent
lamp lamp

Twist mount Starter Socket

A lamp is made of a base and a cover and a switch; a lamp is a fluorescent lamp or an
incandescent lamp.
 Aggregation is sometimes called an “and-relationship” and generalization an “or-
relationship”.

Recursive Aggregates:
 Aggregation can be fixed, variable or recursive.
 A fixed aggregate has a fixed structure; the number and types of subparts are predefined.
 Example 1: Lamp

Base Cover Switch Wiring

 A variable aggregate has a finite number of levels, but the number of parts may vary.
 Example 2:

Company Division Department

Document Paragraph Character

 A recursive aggregate contains, directly or indirectly an instance of the same kind of the
aggregate; the number of potential levels is unlimited.
 Example 3:
Program Blocks

Simple Compound
statements statements

A computer program is an aggregation of blocks, with optionally recursive compound


statements; the recursion ends with simple statements.

Metadata:
 Metadata is a data that describes other data. For example, the definition of a class is
metadata.
 Relational database management systems use metadata. A person can define database tables
for storing information. Similarly, a relational DBMS has several metatables that store table
definitions.
 Thus a table may store the fact that the capital of India is Delhi, capital of Japan is Tokyo. A
metatable would store the fact that a country has a capital.

Candidate keys:
 It is a minimal set of attributes that uniquely identifies an object or link.
 By minimal, we mean that you cannot discard an attribute from the candidate key and still
distinguish all objects and links.
 A class or association may have one or more candidate keys, each of which may have
different combinations and number of attributes.
 An object identifier is always a candidate key for a class. One or more combinations of
related objects are candidate keys for associations.
 Multiplicity and candidate keys have nearly same expressive power for binary associations.
 Multiplicity also includes the notion of existence dependency – whether an object must
participate in an association or not.
 Candidate key is delimited in an object model with braces.
 Examples:
1. Many-to-many association: it requires both related objects to uniquely identify each link.
Person

Owns-stock
{Candidate keys: (person, company)}
Company
2. One-to-many association: it has a single candidate keys – the object on the
many end.
Person

Works-for

Company
{Candidate key: (person)}

3. One-to-one association: it has two candidate keys – either of the objects.


Has-capital
Country Country {Candidate keys: (country) (city)}

4. Candidate keys can also be specified even when one or both classes are
optional.
Has-capital
Country Country

[A city may not be a capital at all, but a city is capital of at most one
country]

5. Ternary association: it has one candidate key consisting of all three objects.
No combination of just one or two objects will uniquely identify each link.

Project Language

Person

{Candidate keys: (project, person, language)}

Abstract classes:
 An abstract class is a class that has no direct instances or an abstract class is one that does
not have any objects or more precisely, it is not allowed to have any objects.
 An abstract class is only used to inherit from, in that it describes common attributes and
behaviour or operations for other classes.
 Example 1: A class hierarchy for vehicles. The Car class is subclass to vehicle, but
superclass to Sports car, Passenger car, and Truck.

Vehicle

Car Boat

Sports car Passenger Truck Sailing boat Motor boat Cargo ship
car

A vehicle class is excellent to capture the commonalities between the car and boat. Vehicle
is an example of abstract class, which does not have any objects, but is used only to inherit
from.
 A class can be specified explicitly as abstract by putting the tagged value {abstract} within
the compartment of the class, under the class name shown in example 2.
 Example 2:
Vehicle {abstract}

drive ( ) {abstract}

Car Boat

drive ( ) drive ( )

drive( ) starts the drive( ) starts the


wheels propeller

 An abstract class normally has abstract operations. An abstract operation is one that has no
implementation method in the class where it is specified; only the signature is shown.
 A class that has at least one abstract operation must be an abstract class.
 A class that inherits from a class that has one or more abstract operations must implement
those abstract operations (provide methods for them) or itself become an abstract class.
 Abstract operations are shown with the property string {abstract} following the operation
signature.
 Abstract operations are defined in abstract classes to specify behaviour that all the
subclasses must have.
 In example 2, a Vehicle class could have abstract operations to specify that all vehicles must
be able to drive, start, and stop. Thus all classes inheriting from the vehicle must provide
methods for those operations (or themselves become abstract).
 Example 3: Class Employee is an abstract class. All employees must be hourly, salaried, or
exempt. Compute pay is an abstract operation of class Employee; its form but not its
implementation is defined. Each subclass must supply a method for its operation.

Employee

Year-to-date earnings

Compute-pay ( )
{abstract}

Hourly Employee Salaried Employee Exempt Employee

Hourly-rate Weekly-rate Monthly-rate


Overtime-rate

Compute-pay ( ) Compute-pay ( ) Compute-pay ( )

 The origin class of a feature is the topmost defining class. The origin class defines the
protocol of the feature that is the type of an attribute or the number and the type of
arguments and the result type of the operations as well as the semantic intent. Descendent
classes can refine the protocol by further restricting the type or by overriding the
initialization or method code. Descendent classes may not expand or change the protocol.

Concrete classes:
 A concrete class is a class that is instantiable; that is it can have direct instances or from
which we can create objects.
 A concrete class have implementations for all the operations.
 If the Vehicle class (example 2) has specified an abstract operation drive, then both cars and
boats must implement that method. The implementations are different, though.
 When someone tells the boat to drive, the propeller moves or if its car then wheel moves.
 As shown, the subclass can redefine operations. A redefined operation must have the same
signature (return type, name and parameters) as the superclass.
 The operation being redefined can be either abstract (not have an implementation in the
superclass) or concrete (have implementation in the superclass). In both cases, the
redefinition in the subclass will be used for all instances of that class.
 New operations, attributes, and association can added to the subclasses. An object of a
subclass may be used in any situation where it is possible to use the superclass objects.
 Example 1: A Person class has drives association to the Vehicle class. The Vehicle class is
abstract; that means that the actual objects that the person drives are from the concrete
subclasses Car and Boat. When the person calls (performs) the drive operation, the result
depends on whether the object used in that situation is a car or a boat.

Drives * Vehicle {abstract}


Person
Color

drive ( ) {abstract}

Car Boat

drive ( ) drive ( )

Example showing inheritance, polymorphism and recursive associations or


aggregates:
 A canvas consists of many figures. Figures could be circles, lines, polygons, or groups. A
group consists of many figures. When a client asks the canvas to draw itself, the canvas asks
its associated figures to draw themselves. Each figure (circle, line, polygon or group) is
responsible for drawing itself in a appropriate way. The group draws itself through calling
the draw operations in the figures that make up the group. Note that the canvas does not
have to ask each figure which kind of figure it is; it just has to call the draw operation and
everything else works automatically.
Consists of * Figure {abstract}
Canvas

Consists of * position:pos

draw ( ) {abstract}

Group Polygon Line Circle


Consists of *

draw ( ) draw ( ) draw ( ) draw ( )

Generalization as Extension and Restriction:


 An instance of a class is an instance of all ancestors of the class. This is part of the definition
of generalization. Therefore all ancestor class features must apply to the subclass instances.
 A subclass may reimplement an operation for the reasons of efficiency but cannot change the
external protocol.
 A subclass may add new features. This is called extension.
 A subclass may also constrain ancestor attributes. This is called restriction because it
restricts the values that instances can assume.

Vous aimerez peut-être aussi