0% found this document useful (0 votes)
4 views

COM423_6-Class_Diagrams

The document discusses Object-Oriented Analysis and Design, focusing on class diagrams and their elements such as classes, attributes, operations, and associations. It explains the importance of Problem Domain Classes (PDCs) and how they differ from software classes, as well as the various types of associations and their multiplicities. Additionally, it outlines steps to build a class diagram and provides a problem statement with examples of class diagrams.

Uploaded by

bed-com-43-21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

COM423_6-Class_Diagrams

The document discusses Object-Oriented Analysis and Design, focusing on class diagrams and their elements such as classes, attributes, operations, and associations. It explains the importance of Problem Domain Classes (PDCs) and how they differ from software classes, as well as the various types of associations and their multiplicities. Additionally, it outlines steps to build a class diagram and provides a problem statement with examples of class diagrams.

Uploaded by

bed-com-43-21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

COM 423 – Object-Oriented Analysis

and Design
Class Diagrams

25 January 2018
Classes and Objects
 In OO system development these things are referred to as
Problem Domain Classes (PDCs)
 The classes, associations and attributes are then modeled using
a domain model class diagram
 Domain model class diagram is UML diagram that shows the
things that are important in the users’ work:
 Ie the problem domain classes (things); their associations and their
attributes
 PDCs are not software classes though often similar
 As they are used to design software classes during design and
implementation
 PDCs just store information, Software objects do the work ie have
bahaviour
2 Kondwani G. Munthali (PhD), 1/25/2018
Department of Computer Science
Classes and Objects…
 The Class diagram is by far the most used and best
known of the object-oriented diagrams.
 It is the source for generating code, and
 It is the target for reverse engineering code
 The other diagrams tend to serve as tools of discovery
that add to your knowledge about how to build the Class
diagram

Department of Computer Science


Classes and Objects: Object Model
 The object model is used to mean the set of diagrams used to
model objects:
 The Class diagram; The Object diagram
 The class diagram represents classes, their attributes and
methods, and the way in which classes of objects are related to
one another
 A class is a definition for a type of object
 The object diagram represents an instance of a class:
 The class defines the rules; the objects express the facts.
 The class defines what can be; the object describes what is.
 The contents of an object diagram must correspond to the
definition of the class
Department of Computer Science
Elements of a Class Diagram
 The class symbol comprises of three rectangular
compartments that contain distinct information needed
to describe the properties of a certain type of object
 The name compartment uniquely defines a class within a
package
 classes may have the same name if they reside in different packages
 The attribute compartment contains all the data definitions
(fields)
 The operations compartment contains a definition for each
behavior (method) supported by the object

Department of Computer Science


Modeling an Attribute
 An attribute describes a piece of information that an
object owns or knows about itself
 An attribute must have a name and a data type
 Each attribute definition must also specify what other
objects are allowed to see it - that is its visibility
 Public (+) visibility allows access to objects of all other classes
 Private (-) visibility limits access to within the class itself
 Protected (#) visibility allows access by subclasses
 Package (~) visibility allows access to other objects in the same
package

Department of Computer Science


Defining an Attribute
 The general structure for an attribute definition is:
 visibility / attribute name : data type = default value {constraints}
 Where:
 Slash (/): optional; used to indicate a derived attribute (e.g. age can
be derived from another attribute, date of birth)
 Assignment operator and default value: optional; used to specify
default values, if any.
 Constraints: constraints express all the rules required to guarantee
the integrity of this piece of information
 Class level attribute: indicated by underlining the attribute definition.
 Denotes that all objects of the class share a single value for the attribute.
 This is called a static attribute in Java

Department of Computer Science


Example: Company Name
 Company Name as a data field is a string of characters
 Limited to 30 characters; Some company names have punctuation marks (e.g.Yahoo!)
 There is a no default value, but you want valid display data, so you must initialize the field
to spaces

- company: character = spaces {1 to 30 characters including alphabetic, spaces and


Department of Computer Science
punctuation; no special characters allowed }
Modeling Operation (Methods)
 Objects have behaviors, things they can do and things that
can be done to them
 These behaviors are modeled as operations
 Operation and method can be interchangeable but UML
distinguishes the two:
 In the UML, an operation is the declaration of the signature or
interface, the minimum information required to invoke the
behavior on an object
 A method is the implementation of an operation and must
conform to the signature of the operation that it implements

Department of Computer Science


Elements of an Operation Specification
 Operations require a name, arguments, and sometimes a return data type
 Arguments, are simply attributes, so they can be specified using the attribute
notation
 BUT it is very common to use the abbreviated form of name and data type only
 The return data type is the data type of the output returned by the operation (if
any)
 You can specify the visibility of the operation: -, +, # and ~
 The general notation for defining an operation is:
 visibility operationName ( argname : data type {constraints}, ...) : return data type
{constraints}
 The operation name does not have to be unique (operator overloading), but the
combination of name and parameters does need to be unique within a class
 You can create class level operations (denoted by underlining the operation
declaration): same as static methods in Java

Department of Computer Science


Example: totalOrderAmount

Department of Computer Science


Class Compartments
 The Class notation
consists of three
compartments:
 Name Compartment
 Attribute Compartment
 Operation Compartment
 Can also be shown with
just the name
compartment especially
when the focus is on
object definitions and
relationships
Department of Computer Science
Class diagram: Associations
 An object oriented system is a collection of objects
working together to deliver expected services
 Associations are relationships between objects
 They exist because objects work together
 To work together objects communicate with each other
 To work together objects must fulfil certain limitations:
 Number of participants in a relationship
 Roles of participants in a relationship

Department of Computer Science


Association Name
 The name of an association expresses its purpose
 An association name is a verb or verb phrase that
describes how objects of one type (class) relate to
objects of another type (class)
 For example:
 a person owns a car; a person drives a car; a person rents a
car

Department of Computer Science


Elements of an Association Diagram
 UML associations have four basic elements:
 The participating classes, Person and Car.
 Represented using the name compartment only because the focus is
on the classes and their associations
 The association, represented by a line between the two classes
 The name of the association, represented by a verb or verb
phrase on the association line
 The direction to read the name (indicating the direction is
optional)
 Depends on the position of the participating classes

Department of Computer Science


Example

Department of Computer Science


Association Multiplicity
 Association Multiplicity is the UML term for the rule that
defines the number of participating objects:
 e.g. “How many Cars may a Person own?”, “How many can
they rent?” and “How many people can drive a given Car?”
 A multiplicity value must be assigned to each of the
participating classes in an association

Department of Computer Science


Association Multiplicity
 The answer to each question goes next to the class concerned
 Multiplicity can be specified as follows:
 Values separated by two periods (..) mean a range.
 For example, 1..3 means between 1 and 3 inclusively; 5..10 means
between 5 and 10 inclusively
 Values separated by commas mean an enumerated list of possibilities
 For example, 4,6,8 means you may have 4 objects or 6 objects or 8
objects of this type
 Asterisk (*) when used alone means zero or more, no lower or
upper limit
 Asterisk (*) when used in a range (1..*) means no upper limit
 In this case, you must have at least one but you can have as many more as
you want
Department of Computer Science
Association Roles
 Sometimes the association name is a bit hard to
determine
 An alternative is to use association roles:
 a role describes how an object participates in the association

 In modelling tools roles generate code BUT associations do


not
Department of Computer Science
Association Constraints
 Sometimes there are constraints that apply to an
association
 For example, only people with driving licenses are allowed to
drive

Department of Computer Science


Association Class
 An association class encapsulates information about an
association
 For example, Customer orders Product is an association
 But when customers order products there is usually more that you
need to know, like when did they order the products? How many did
they order? What were the terms of the sale?
 An association class is connected to an association with a
dashed line

Department of Computer Science


Reflexive Association
 A reflexive association is an association involving objects
of the same class

Department of Computer Science


Qualified Association
 If one class uses a particular attribute as an index in the
association with another class, we can create a qualified
association.
 For example, customer uses orderno to look for an order
so the “customer places order” can be qualified:
 The qualifier goes next to the class of objects that will use the
value to do the look up

Department of Computer Science


Aggregation
 Aggregation is a special type of association where participating
objects are assembled together to create a new and more complex
object
 For example, a number of different parts are assembled to create a car
 To model aggregation on a Class diagram:
 Draw an association (a line) between the member class and the
aggregating class
 Draw a diamond on the end of the association attached to the
aggregating class
 Assign the appropriate multiplicities to each end of the association, and
add any roles and/or constraints that may be needed

Department of Computer Science


Composition
 Composition is a special case where the life span of the
member object depends on the life span of the aggregate
 i.e. a member object cannot exist except as part of an
aggregate object

Department of Computer Science


Example: Formula 1 Car Configuration

Department of Computer Science


Generalization
 Generalization is the process of creating a more general class
of objects that has properties common to a set of objects
 Eg mangoes and apples  fruits; fruits and meat  food
 Words such as kind of or type of are often used to describe a
generalization relationship between classes
 e.g. an apple is a kind of fruit or an apple is a type of fruit
 Many times the terms generalization and inheritance are used
synonymously
 If an apple is a kind of fruit, then it inherits all the properties of fruit
 On the flip side, we can talk about specialization
 an apple is a specialization of fruit because it inherits all the
generalized properties of fruit and adds some special (unique)
properties that only apply to apples

Department of Computer Science


Generalization

Department of Computer Science


Elements of Generalization
 To draw a generalization
relationship, we first need to
know what is meant by
superclass, subclass and
discriminator
 Superclass: a class that
contains some combination of
attributes, operations, and
associations that are common
to two or more types of
objects
 Subclass: a class that contains
some combination of
attributes, operations, and
associations that are unique to
a type of object
 Discriminator: is an attribute
that is used to determine the
set of subclasses for a
superclass
Department of Computer Science
Abstract Class
 An abstract class is a class that cannot create objects
(cannot be instantiated)
 Any superclass that defines at least one operation that does
not have a method is said to be abstract, or lacking a complete
definition
 For example, every polygon has area but the area for triangle is
different from area of rectangle
 It is therefore impossible to provide a complete definition (method)
for area of polygon
 So polygon as a class is abstract because it lacks complete definition
of area
 Only a superclass can be abstract
Department of Computer Science
Concrete Class
 A concrete class is a class that has a method for every
operation, so it can create objects
 The methods may be defined in the class or inherited from a
superclass.
 Superclasses may be concrete or abstract.
 But all classes at the bottom of a generalization hierarchy must
be concrete.
 e.g. triangle, rectangle and square as subclasses of polygon must be
complete

Department of Computer Science


Summary: Steps To Build a Class Diagram
1. Identify the classes, name them, and define them so you
know why they are part of the model
2. Identify, name, and define the associations between pairs
of classes. Assign multiplicity and constraints where
needed. If naming an association is difficult, try role
names
3. Evaluate each association to determine whether it
should be defined as aggregation. If it is aggregation,
then could it be composition?
4. Evaluate the classes for possible specialization or
generalization
Department of Computer Science
Problem Statement
1. “Customers place orders for one or more items”
2. “The Customer may inquire on the status of his Orders using the order number.”
3. “Customers place orders for one or more items. . . .”
4. “Each item corresponds to a product. We identify each product using a unique serial number.”
5. “Any items that have not been shipped are placed on a backorder with a reference to the original
order.”
6. “Customer orders are shipped as the products become available, so there may be more than one
shipment to satisfy a single customer order. But a single shipment may contain products from
multiple orders.”
7. “Shipments of products from vendors are received and placed into stock. . . . Customer orders are
shipped as the products become available.”
8. “Each product is assigned to a location so that we can easily find it later when filling orders. Each
location has a unique location identifier.”
9. “These products may be purchased directly from vendors and resold as is, or we can package
vendor products together to make our own custom product.”
10. “. . . we can package vendor products together to make our own custom product.”

Department of Computer Science


Example Class Diagram

Department of Computer Science

You might also like