Chapter 11 An introduction to object-oriented design

advertisement
Chapter 11
An introduction to object-oriented
design
Objectives
• To introduce object-oriented design
• To define objects, classes, attributes,
operations and information hiding
• To introduce public and private
operations, accessors and mutators
• To list the steps required to create an
object-oriented design solution to a
problem
11.1
Introduction to object-oriented
design
Introduction to objectoriented design
• Objects
– Can be considered as a container for
a set of data and the operations that
need to be performed on it
– Objects have the following properties
• It has an identity that is unique for the
lifetime of the object
• It has data in a form of a set of
characteristics or attributes
• A set of operations or methods that can
be performed on the date
• It is a instance (example) of a class
Introduction to objectoriented design
• Classes and objects
– Object is created from a template or
pattern, called a class which defines
the basic relationship, attributes and
operations available to the objects of
that class
– Each class in a system should bear a
unique name
– Process of creating objects from
classes is called instantiation and an
object is described as an instance of
its class
Introduction to objectoriented design
• Attributes
– An object’s data is stored in attributes
– Attributes are the properties or
characteristics that describe that
particular object
– Objects of the same class will have an
identical set of attributes
Introduction to objectoriented design
• Methods
– Objects usually have a set of
operations called ‘methods’
– Includes all operations required to be
performed on an object, and are the
same as modules in procedural
programming
Introduction to objectoriented design
• Inheritance
– Allows a new object to inherit the
same attributes and methods as an
existing object
Introduction to objectoriented design
• Class diagram
– Object-oriented programming
requires classes, attributes and
methods to be represented in a class
diagram
– Consists of a rectangular box divided
into three section with the name of
the class at the top, the attributes of
the class in the middle and the
methods at the bottom
Introduction to objectoriented design
• Encapsulation and information hiding
– Objects are said to be encapsulate
(enclose together in a single
indivisible unit)
– In object-oriented design, each object
can be regarded as a ‘black box’
whose internal workings are hidden
from all other objects
11.2
Public and private access methods
Public and private access
methods
• Necessary to consider whether the
attributes and operations of an object
are to have private or public access
• This concept is called ‘visibility’
• Private access means that the attribute
and methods are invisible to the rest of
the system
• Public access means that the operation
is visible other objects
Public and private access
methods
• Instantiating objects
– Every time an object is instantiated
from a class, a special operation, or
set of instructions known as a
‘constructor’ method is called or
invoked
– Constructors may:
• Have no parameters – new objects is
assigned all the default vales for its
attribute
• Have parameters that initialise the
attributes with specific values
Public and private access
methods
• Accessors and mutators
– The values in the attributes of an
object should be available to all
operations in that object, but hidden
from external objects
– Accessor operations pass attribute
values to external objects
– Mutator operations enable objects to
change the values stored in attributes
Public and private access
methods
• Message
– Communication is achieved when one
object passes a message to another
– Message from one object to another
usually initiates the processing of an
operation in the receiving object
11.3
Steps in creating an objectoriented solution
Steps in creating an objectoriented solution
• Three steps in creating an objectoriented solution for a problem with
just one class:
1. Identify the objects and their
attributes, responsibilities and
operations
2. Design the algorithms for the
operations and methods using
structured design
3. Develop a test or driver algorithm to
test the solution
Summary
• Object-oriented design focuses on the
objects that make up a program rather
than on the processes.
• An object can be defined as a container
for both a set of characteristics and a
set of operations that can be performed
on the data.
• Objects encapsulate their data and
operations, and can be regarded as
‘black boxes’ for the purposes of large
system design.
Summary
• Operations that are accessible by
external objects are described as
having public access.
• Operations that are internal to the
object are described as having private
access.
Summary
• The steps in designing an objectoriented solution for a programming
problem are:
1. Identify the classes and their
attributes, responsibilities and
operations.
2. Design the algorithms for the
operation, using structured design.
3. Develop a test or driver algorithm to
test the solution.
Download