You are on page 1of 43

Object Oriented Analysis & Design

Lecture # 3

Department of Computer Science and Technology


University of Bedfordshire
Written by David Goodwin,
based on the book Applying UML and Patterns (3rd ed.)
by C. Larman (2005).

Modelling and Simulation, 2012


Object
Outline Oriented
Analysis &
Design
Introduction to Object Orientation
Objects
Class Introduction to
Attributes Object
Orientation
Operations Objects
Associations Class
Aggregation Attributes
Operations
Composition Associations
Aggregation
Inheritance Composition

Dependency Inheritance

Dependency
Multiplicity
Multiplicity
Polymorphism Polymorphism

Encapsulation Encapsulation

Interface
Interface
Package
Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Introduction to Objects

Class

Object Orientation Attributes


Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Object Orientation Oriented
Analysis &
Design

Introduction to
Object
Orientation

I Knowing and object-oriented language (such as Java) is Objects

a necessary but insufficient step to create object Class


Attributes
systems. Operations
Associations
I UML is just a diagramming tool; it’s full use isn’t Aggregation

realised without object oriented design. Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
OOA/D Oriented
Analysis &
Design

I Object-oriented analysis (OOA):


I process of analysing a task (also known as a problem
Introduction to
domain) Object
I finding and describing objects Orientation
I typical: Objects

I set of use cases Class


I one or more class diagrams Attributes
Operations
I a number of interaction diagrams Associations

I Object-oriented design (OOD): Aggregation


Composition
I defining software objects and how they collaborate to Inheritance
fulfill the requirements.
Dependency
I constraints to conceptual model produced in
Multiplicity
object-oriented analysis
Polymorphism
I Concepts in the analysis model are mapped onto
Encapsulation
implementation classes and interfaces resulting in a
model of the solution domain. Interface

Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Objects Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Definition of an object Oriented
Analysis &
Design

I Object: Introduction to
Object
I Objects directly relate to real-world ‘entities’. Orientation
I An object has identity, state & behaviour. Objects
I Identity: the property of an object that distinguishes it Class
from other objects Attributes
Operations
I State: describes the data stored in the object
Associations
I Behaviour: describes the methods in the object’s Aggregation
interface by which the object can be used Composition

I The state of an object is one of the possible conditions Inheritance

in which an object may exist. Dependency


I The state of an object is represented by the values of its Multiplicity
properties (attributes). Polymorphism

Encapsulation

Interface

Package
Object
Definition of an object Oriented
Analysis &
Design

I Real-world objects share three characteristics: Introduction to


Object
I Identity: Orientation
I Dog Objects
I Bicycle Class
I State: Attributes
Operations
I Dogs have state (name, color, breed, hungry)
Associations
I Bicycles also have state (current gear, current pedal Aggregation
cadence, current speed) Composition

I Behaviour: Inheritance
I Dogs have behaviour (barking, fetching, wagging tail) Dependency
I Bicycles also have behaviour (changing gear, changing Multiplicity
pedal cadence, applying brakes) Polymorphism

Encapsulation

Interface

Package
Object
Identifying & using objects’ states Oriented
Analysis &
Design

I “What possible states can this object be in?” Introduction to


Object
Orientation
I “What possible behaviour can this object perform?”
Objects
I Some objects will contain other objects. Class
I By attributing state (current speed, current pedal Attributes
Operations
cadence, and current gear) and providing methods for Associations
changing that state, the object remains in control of Aggregation

how the outside world is allowed to use it. Composition

Inheritance
I if the bicycle only has 6 gears, a method to change
Dependency
gears could reject any value that is less than 1 or
Multiplicity
greater than 6.
Polymorphism

Encapsulation

Interface

Package
Object
Advantages of using objects Oriented
Analysis &
Design

I Bundling code into individual software objects provides


a number of benefits, including:
I Modularity: Introduction to
Object
I The source code for an object can be written and Orientation
maintained independently of the source code for other Objects
objects.
Class
I Information-hiding: Attributes

I By interacting only with an object’s methods, the Operations


Associations
details of its internal implementation remain hidden Aggregation
from the outside world. Composition

I Code re-use: Inheritance


I If an object already exists, you can use that object in Dependency
your program. Multiplicity
I Pluggability and debugging ease: Polymorphism
I If a particular object turns out to be problematic, you Encapsulation
can simply remove it from your application and plug in Interface
a different object as its replacement.
Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Class Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
What is a Class? Oriented
Analysis &
Design

Introduction to
I Many individual objects can be of the same kind: Object
Orientation
I There may be thousands of other bicycles in existence, Objects
all of the same make and model.
Class
I Each bicycle was built from the same set of blueprints Attributes
and therefore contains the same components. Operations
Associations
I In object-oriented terms, we say that your bicycle is an Aggregation

instance of the class of objects known as bicycles. Composition

Inheritance
I A class is the blueprint from which individual objects Dependency
are created. Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Class example in Java Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Class
I The fields cadence, speed, Attributes
and gear represent the Operations

object’s state, and the Associations


Aggregation
methods (changeCadence,
Composition
changeGear, speedUp etc.)
Inheritance
define its interaction.
Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Class example in Java Oriented
Analysis &
Design

Introduction to
Object
Orientation
I Not a complete
Objects
application; it’s just
the blueprint for Class
Attributes
bicycles that might be Operations
used in an application. Associations
The responsibility of Aggregation

creating and using new Composition

Bicycle objects belongs Inheritance

to some other class in Dependency


your application. Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Class notation Oriented
Analysis &
Design

Introduction to
Object
Orientation

Name appears in the top Objects

division of the class box Class


Attributes

Attribute appears in the middle Operations


Associations
division of the class box Aggregation
Composition
Operation appears in the bottom
Inheritance
division of the class box
Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
What is an attribute? Oriented
Analysis &
Design

Introduction to
Object
I An element of data that helps to describe an object. Orientation

Objects
I Attribute types may be restricted by a programming
Class
language. Attributes
Operations
I A class that includes many attributes may be Associations

decomposed into other classes. Aggregation


Composition
I Complex attribute lists in a class may be defined Inheritance
elsewhere. Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
What is an operation? Oriented
Analysis &
Design

Introduction to
I It helps to specify the behaviour of a class (object). Object
Orientation
I Can use the syntax of a programming language. Objects
I Specify the visibility of each operation: Class
Attributes
I public (+); Operations
I private (-); Associations

I protected (#). Aggregation


Composition
I Consider the responsibilities of each class: Inheritance
I Responsibilities often imply more than one operation. Dependency
I A method is the implementation of an operation. Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Class Associations Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects
I Classes (objects) must interact with each other so that Class
a piece of software can do something. Attributes
Operations
I How do classes/objects therefore interact? Associations
Aggregation
I How are associations modelled? Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Class Associations Oriented
Analysis &
Design

I An association represents a family of links.


I Binary associations (with two ends) are normally
represented as a line. Introduction to
Object
I An association can be named, and the ends of an Orientation

Objects
association can be adorned with role names, ownership
Class
indicators, multiplicity, visibility, and other properties. Attributes

I There are four different types of association: Operations


Associations
I bi-directional, uni-directional, Aggregation (includes Aggregation

Composition aggregation) and Reflexive. Composition

Inheritance
I Bi-directional and uni-directional associations are the
Dependency
most common ones.
Multiplicity
I For instance, a flight class is associated with a plane
Polymorphism
class bi-directionally. Association represents the static
Encapsulation
relationship shared among the objects of two classes.
Interface
Example: “department offers courses”, is an association
relation Package
Object
Aggregation Oriented
Analysis &
Design

Introduction to
Object
Orientation
I Whole/Part relationship. Objects
I ‘Smaller’ classes are parts of ‘larger’ classes. Class
Attributes
I Objects belonging to an aggregate can belong to more Operations

than one class. Associations


Aggregation
I Each class that makes up an aggregation relationship is Composition

a class in its own right. Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Aggregation Oriented
Analysis &
Design

I Aggregation is a variant of the “has a” or association


relationship; aggregation is more specific than
association. Introduction to
Object
Orientation
I It is an association that represents a part-whole or
Objects
part-of relationship.
Class
I As a type of association, an aggregation can be named Attributes
Operations
and have the same adornments that an association can. Associations
Aggregation
I However, an aggregation may not involve more than Composition
two classes. Inheritance

I Aggregation can occur when a class is a collection or Dependency

container of other classes, but where the contained Multiplicity

classes do not have a strong life cycle dependency on Polymorphism

the container-essentially, Encapsulation

Interface
I if the container is destroyed, its contents are not. Package
Object
Aggregation Notation Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Composition Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects
I A stronger form of aggregation.
Class
I Objects may only be part of one composite at a time. Attributes
Operations
I The composite object has sole responsibility for the Associations
Aggregation
creation and destruction of all of its parts. Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Composition Oriented
Analysis &
Design

Introduction to
Object
I Composition is a stronger variant of the “owns a” or Orientation

association relationship; composition is more specific Objects

than aggregation. Class


Attributes
I Composition usually has a strong life cycle dependency Operations

between instances of the container class and instances Associations


Aggregation
of the contained class(es): Composition

I If the container is destroyed, normally every instance Inheritance


that it contains is destroyed as well. Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Composition Notation Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Aggregation vs. Composition Oriented
Analysis &
Design
I When attempting to represent real-world whole-part
relationships,
I e.g., an engine is a part of a car, the composition
relationship is most appropriate. Introduction to
Object
I However, when representing a software or database Orientation

relationship, Objects

Class
I e.g., car model engine ENG01 is part of a car model Attributes
CM01, an aggregation relationship is best, as the Operations

engine, ENG01 may be also part of a different car Associations


Aggregation
model. Composition
I The whole of a composition must have a multiplicity of Inheritance
0..1 or 1, indicating that a part must belong to only one Dependency
whole; the part may have any multiplicity. Multiplicity
I For example, consider University and Department Polymorphism
classes. A department belongs to only one university, so Encapsulation
University has multiplicity 1 in the relationship. A Interface
university can (and will likely) have multiple Package
departments, so Department has multiplicity 1..*.
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Inheritance Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Inheritance (Generalisation) Oriented
Analysis &
Design

I Inheritance is a code reuse mechanism to build new


objects out of old ones. Introduction to
Object
Orientation
I Inheritance defines a relationship among classes where
Objects
one or more classes share the behaviour and/or Class
attributes of another class. Attributes
Operations
I For example, a class called ink-jet printer inherits all the Associations

behaviour and attributes of the class computer printer. Aggregation


Composition

I Inheritance also enables ‘polymorphism’. Inheritance

I The relationship superclass and subclass is used. Dependency

Multiplicity
I Each subclass is a specialised version of its superclass.
Polymorphism
I Within UML, generalisation is the preferred term. Encapsulation

Interface

Package
Object
Inheritance (Generalisation) Oriented
Analysis &
Design

I Different kinds of objects often have a certain amount


in common with each other. Introduction to
Object
I Mountain bikes, road bikes, and tandem bikes, for Orientation

example, all share the characteristics of bicycles (current Objects

speed, current pedal cadence, current gear). Class


Attributes
I Additional features that make them different: tandem Operations
bicycles have two seats and two sets of handlebars; road Associations

bikes have drop handlebars; some mountain bikes have Aggregation


Composition
an additional chain ring, giving them a lower gear ratio.
Inheritance
I Object-oriented programming allows classes to inherit Dependency
commonly used state and behavior from other classes. Multiplicity
I In this example, Bicycle now becomes the superclass of Polymorphism
MountainBike, RoadBike, and TandemBike. Encapsulation

Interface

Package
Object
Generalisation Notation Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Dependency Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Dependency Oriented
Analysis &
Design

Introduction to
Object
I Weaker form of relationship which indicates that one Orientation

Objects
class depends on another.
Class
I One class depends on another if the latter is a Attributes

parameter variable or local variable of a method of the Operations


Associations
former. Aggregation
Composition
I This is different from an association, where an attribute Inheritance
of the former is an instance of the latter. Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Multiplicity Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Multiplicity Oriented
Analysis &
Design

I The association relationship indicates that (at least) one Introduction to


of the two related classes makes reference to the other. Object
Orientation
I The UML representation of an association is a line with Objects
an optional arrowhead indicating the role of the Class
object(s) in the relationship, and an optional notation Attributes

at each end indicating the multiplicity of instances of Operations


Associations
that entity (the number of objects that participate in Aggregation

the association). Composition

I 0..1 No instances, or one instance (optional) Inheritance

I 1 Exactly one instance Dependency

I 0..* or * Zero or more instances Multiplicity

I 1..* One or more instances (at least one) Polymorphism

Encapsulation

Interface

Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Polymorphism Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Polymorphism Oriented
Analysis &
Design

I Polymorphism means having many forms.


Introduction to
I Invokes many different kinds of behaviour. Object
Orientation
I It requires an inheritance hierarchy to be present. Objects

I Each class in an inheritance hierarchy is dependent Class


Attributes
upon other classes, and are therefore not classes in their Operations

own right. Associations


Aggregation
I objects of various types define a common interface of Composition

operations for users Inheritance

Dependency
I the “+” operator which allows similar or polymorphic
Multiplicity
treatment of numbers (addition), strings Polymorphism
(concatenation), and lists (attachment). Encapsulation

Interface

Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Encapsulation Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Encapsulation Oriented
Analysis &
Design

I Encapsulation is the process of hiding the


Introduction to
implementation details of an object. Object
Orientation
I The only access to manipulate the object data is Objects
through its interface. Class
I It protects an object’s internal state from being Attributes
Operations
corrupted by other objects. Associations
Aggregation
I Also, other objects are protected from changes in the Composition

object implementation. Inheritance

I Encapsulation allows objects to be viewed as ‘black Dependency

Multiplicity
boxes’.
Polymorphism
I Communication is achieved through an ‘interface’. Encapsulation

Interface

Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Interface Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Interface Oriented
Analysis &
Design

I Object’s interface with the outside world; Introduction to


I the buttons on the front of your television set, for Object
Orientation
example, are the interface between you and the electrical Objects
wiring on the other side of its plastic casing. You press
Class
the “power” button to turn the television on and off. Attributes
Operations
I In its most common form, an interface is a group of Associations

related methods with empty bodies. Aggregation


Composition
I Implementing an interface allows a class to become Inheritance
more formal about the behaviour. Dependency

I Interfaces form a contract between the class and the Multiplicity

outside world. Polymorphism

Encapsulation

Interface

Package
Object
Oriented
Analysis &
Design

Introduction to
Object
Orientation

Objects

Package Class
Attributes
Operations
Associations
Aggregation
Composition

Inheritance

Dependency

Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Package Oriented
Analysis &
Design

I A package is a namespace that organises a set of Introduction to


Object
related classes and interfaces. Orientation

I Conceptually you can think of packages as being similar Objects

to different folders on your computer. Class


Attributes
I You might keep HTML pages in one folder, images in Operations
another, and scripts or applications in yet another. Associations
Aggregation
I Because software can be composed of hundreds or Composition

thousands of individual classes, it makes sense to keep Inheritance

things organised by placing related classes and Dependency

interfaces into packages. Multiplicity

Polymorphism

Encapsulation

Interface

Package
Object
Packages in Java Oriented
Analysis &
Design

I The Java platform provides an enormous class library (a


set of packages) suitable for use in your own
applications. This library is known as the “Application
Introduction to
Programming Interface”, or “API”. Object
Orientation
I a String object contains state and behavior for character
Objects
strings;
Class
I a File object allows a programmer to easily create, Attributes
delete, inspect, compare, or modify a file on the Operations

filesystem; Associations
Aggregation
I a Socket object allows for the creation and use of Composition
network sockets; Inheritance
I various GUI objects control buttons and checkboxes and Dependency
anything else related to graphical user interfaces.
Multiplicity
I There are literally thousands of classes to choose from. Polymorphism

This allows you, the programmer, to focus on the Encapsulation

design of your particular application, rather than the Interface

infrastructure required to make it work. Package

You might also like