Notes Ooad Unit 3
Notes Ooad Unit 3
UNIT – 3
Object oriented Design
Unit-03/Lecture-01
Characteristics of OOD
Objects are abstractions of real-world or system entities and manage
themselves.
Objects are independent and encapsulate state and representation information.
System functionality is expressed in terms of object services.
Shared data areas are eliminated.
Objects communicate by message passing.
Objects may be distributed and may execute sequentially or in parallel.
Advantages of OOD
Easier maintenance. Objects may be understood as stand-alone entities.
Objects are potentially reusable components.
For some systems, there may be an obvious mapping from real world entities to
system objects.
Object Design
The object design phase determines the full definitions of the classes and associations
used in the implementation, as well as the interfaces and algorithms of the methods
used to implement operations. The object design phase adds internal objects for
implementation and optimizes data structures and algorithms.
Steps of Design:
During object design, the designer must perform the following steps:
1. Combining the three models to obtain operations on classes.
2. Design algorithms to implement operations.
3. Optimize access paths to data.
4. Implement control for external interactions
5. Adjust class structure to increase inheritance. 6. Design associations.
7. Determine object representation.
8. Package classes and associations into modules.
After analysis, we have object, dynamic and functional model, but the object model is
the main framework around which the design is constructed. The object model from
analysis may not show operations. The designer must convert the actions and
activities of the dynamic model and the processes of the functional model into
operations attached to classes in the object model. Each state diagram describes the
life history of an object. A transition is a change of state of the object and maps into
an operation on the object.
We can associate an operation with each event received by an object. In the state
diagram, the action performed by a transition depends on both the event and the
state of the object. Therefore, the algorithm implementing an operation depends
on the state of the object. If the same event can be received by more than one
state of an object, then the code implementing the algorithm must contain a case
statement dependent on the state. An event sent by an object may represent an
operation on another object.
Events often occur in pairs, with the first event triggering an action and the second
event returning the result on indicating the completion of the action. In this case, the
event pair can be mapped into an operation performing the action and returning the
control provided that the events are on a single thread. An action or activity
initiated by a transition in a state diagram may expand into an entire dfd in the
functional model .The network of processes within the dfd represents the body of an
operation.
The flows in the diagram are intermediate values in operation. The designer convert
the graphic structure of the diagram into linear sequence of steps in the
algorithm .The process in the dfd represent sub operations. Some of them, but
we dont take any liability for the notes correctness. http://www.rgpvonline.com
3
not necessarily all may be operations on the original target object or on other
objects. Determine the target object of a sub operation as follows:
* If a process extracts a value from input flow then input flow is the target.
* Process has input flow or output flow of the same type, input output flow is the
target.
* Process constructs output value from several input flows, then the operation is a
class operation on output class.
* If a process has input or an output to data store or actor, data store or actor is the
target.
UNIT-03
TOPIC: DESIGNING ALGORITHMS
UNIT-03/LECTURE-02
Designing algorithms [RGPV /DEC-2012(10),DEC-2011(10)]
Each operation specified in the functional model must be formulated as an algorithm.
The analysis specification tells what the operation does from the view point of its
clients, but the algorithm shows how it is done. The analysis specification tells what
the operation does from the view point of its clients, but the algorithm shows how it is
done. An algorithm may be subdivided into calls on simpler operations, and so on
recursively, until the lowest-level operations are simple enough to implement directly
without refinement .The algorithm designer must decide on the following:
i) Choosing algorithms
Many operations are simple enough that the specification in the functional model
already constitutes a satisfactory algorithm because the description of what is done
also shows how it is done. Many operations simply traverse paths in the object link
network or retrieve or change attributes or links.
Non trivial algorithm is needed for two reasons:
To implement functions for which no procedural specification
To optimize functions for which a simple but inefficient algorithm serves as
a definition.
Some functions are specified as declarative constraints without any procedural
definition. In such cases, you must use your knowledge of the situation to invent an
algorithm. The essence of most geometry problems is the discovery of appropriate
algorithms and the proof that they are correct. Most functions have simple
mathematical or procedural definitions. Often the simple definition is also the best
algorithm for computing the function or else is also so close to any other algorithm
that any loss in efficiency is the worth the gain in clarity. In other cases, the simple
definition of an operation would be hopelessly inefficient and must be implemented
with a more efficient algorithm.
For example, let us consider the algorithm for search operation .A search can be done in
two ways like binary search (which performs log n comparisons on an average) and a
linear search (which performs n/2 comparisons on an average).Suppose our search
algorithm is implemented using linear search , which needs more comparisons. It
would be better to implement the search with a much efficient algorithm like binary
search.
Considerations in choosing among alternative algorithm include:
a) Computational Complexity:
It is essential to think about complexity i.e. how the execution time (memory) grows
with the number of input values.
For example: For a bubble sort algorithm, ti e ∞ n2
Most other algorithms, time ∞ log n
b) Ease of implementation and understand ability:
It is worth giving up some performance on non critical operations if they can be
implemented quickly with a simple algorithm.
c) Flexibility:
Most programs will be extended sooner or later. A highly optimized algorithm often
sacrifices readability and ease of change. One possibility is to provide two
Implementations of critical applications, a simple but inefficient algorithm that can be
implemented, quickly and used to validate the system, and a complicated but efficient
algorithm whose correct implementation can be checked against the simple one.
d) Fine Timing the Object Model:
We have to think, whether there would be any alternatives, if the object model were
structured differently.
lower level operations on simpler objects. These lower level operations must be
defined during object design because most of them are not externally visible. Some of
these operations were found from shopping –list . There is a need to add new
internal operations as we expand high –level functions. When you reach this point
during the design phase, you may have to add new classes that were not mentioned
directly in the client’s description of the problem. These low-level classes are the
implementation elements out of which the application classes are built.
UNIT-03
TOPIC: DESIGN OPTIMIZATION
UNIT-03/LECTURE-03
The basic design model uses the analysis model as the framework for implementation.
The analysis model captures the logical information about the system, while the design
model must add details to support efficient information access. The inefficient but semantically
correct analysis model can be optimized to make the implementation more efficient,
but an optimized system is more obscure and less likely to be reusable in another context.
The designer must strike an appropriate balance between efficiency and clarity. During
design optimization, the designer must
Add Redundant Associations for Efficient Access During analysis, it is undesirable to have
redundancy in association network because redundant associations do not add any information.
During design, however we evaluate the structure of the object model for an implementation.
For that, we have to answer the following questions:
* Is there a specific arrangement of the network that would optimize critical aspects of the
completed system?
* Should the network be restructured by adding new associations? * Can existing associations be
omitted?
The associations that were useful during analysis may not form the most efficient network
when the access patterns and relative frequencies of different kinds of access are
considered. In cases where the number of hits from a query is low because only a fraction of
objects satisfy the test, we can build an index to improve access to objects that must be
frequently retrieved.
What is the fan-out along a path through the network? Estimate the average count of each
any association encountered along the path. Multiply the individual fan-outs to obtain
the fan-out of the entire path; which represents the number of accesses on the last class in
the path. Note that o e links do not increase the fan-out, although they increase the cost
of each operation slightly, don’t worry about such small effects.
What is the fraction of hits on the final class, that is, objects that meet selection criteria
(if any ) and is operated on? If most objects are rejected during the traversal for some reason,
then a simple nested loop may be inefficient at finding target objects. Provide indexes for
we dont take any liability for the notes correctness. http://www.rgpvonline.com
9
frequent, costly operations with a low hit ratio because such operations are inefficient to
implement using nested loops to traverse a path in the network.
After adjusting the structure of the object model to optimize frequent traversal, the next thing
to optimize is the algorithm itself. Algorithms and data structures are directly related to each
other, but we find that usually the data structure should be considered first. One key to
algorithm optimization is to eliminate dead paths as early as possible. Sometimes the
execution order of a loop must be inverted.
Data that is redundant because it can be derived from other data can be ached or store in its
computed form to avoid the overhead of re computing it. The class that contains the cached
data must be updated if any of the objects that it depends on are changed.
Derived attributes must be updated when base values change. There are 3 ways to recognize
when an update is needed:
Explicit update: Each attribute is defined in terms of one or more fundamental base
objects. The designer determines which derived attributes are affected by each change
to a fundamental attribute and inserts code into the update operation on the base object
to explicitly update the derived attributes that depend on it.
Periodic Re computation: Base values are updated in bunches. Re compute all derived attributes
periodically without re computing derived attributes after each base value is changed. Re
computation of all derived attributes can be more efficient than incremental update because
some derived attributes may depend on several base attributes and might be updated more
than once by incremental approach. Periodic re computation is simpler than explicit update
and less prone to bugs. On the other hand, if the data set changes incrementally a few
objects at a time, periodic re computation is not practical because too many derived
attributes must be recomputed when only a few are affected.
Active values: An active value is a value that has dependent values. Each dependent value
registers itself with the active value, which contains a set of dependent values and update
operations. An operation to update the base value triggers updates all dependent values, but
the calling code need not explicitly invoke the updates. It provides modularity
UNIT-03
TOPIC:IMPLEMENTATION OF CONTROL
UNIT-03/LECTURE-04
Implementation of Control [RGPV /June-2005(10)]
The designer must refine the strategy for implementing the state – event models present
in the dynamic model. As part of system design, you will have chosen a basic strategy for
realizing dynamic model, during object design flesh out this strategy.
There are three basic approaches to implementing the dynamic model:
i) State as Location within a Program:
This is the traditional approach to representing control within a program. The location of
control within a program implicitly defines the program state. Any finite state machine can be
implemented as a program. Each state transition corresponds to an input statement. After
input is read, the program branches depending on the input event received. Each input
statement need to handle any input value that could be received at that point. In highly
nested procedural code, low –level procedures must accept inputs that they may know
nothing about and pass them up through many levels of procedure calls until some
procedure is prepared to handle them. One technique of converting state diagram to code is
as follows:
1. Identify the main control path. Beginning with the initial state, identify a path through
the diagram that corresponds to the normally expected sequence of events. Write the
name of states along this path as a linear sequence of events. Write the names of states
along this path as a linear sequence .This becomes a sequence of statements in the program.
2. Identify alternate paths that branch off the main path and rejoin it later. These become
conditional statements in the program.
3. Identify backward paths that branch off the main loop and rejoin it earlier .These
become loops in program. If multiple backward paths that do not cross, they become nested
loops. Backward paths that cross do not nest and can be implemented with goto if all else
fails, but these are rare.
4. The status and transitions that remain correspond to exception conditions. They can be
we dont take any liability for the notes correctness. http://www.rgpvonline.com
11
After receiving input they pass them up through many levels of procedure calls. There must
be some procedure prepared to handle these lower level calls. The technique of converting
a state diagram to code is given as under:
a) Identify all the main control paths. Start from the initial state; choose a path
through the diagram that corresponds to the normally expected sequence of events.
Write the names of states along the selected path as a linear sequence. This will be a
sequence of statements in the program.
b) Choose alternate paths that branch off the main path of the program and rejoin it
later. These could be conditional statements in the program.
c) Identify all backward paths that branch off the main loop of the program and rejoin
it earlier. This could be the loop in the program. All non-intersecting backward paths
become nested loops in the program.
d) The states and transitions that remain unchecked correspond to exception
conditions. These can be handled by applying several techniques, like error subroutines,
exception handling supported by the language, or setting and testing of status flags.
To understand control as a state within a program, let us take the state model for the ATM
class showing the state model of the ATM class and the pseudo code derived from it.
In this process first, we choose the main path of control, this corresponds to the reading of
a card querying the user for transaction information, processing the transaction, printing a
receipt, and ejecting the card.
we dont take any liability for the notes correctness. http://www.rgpvonline.com
13
If the customer wants to process for some alternates control that should be provided. For
example, if the password entered by the customer is bad, then the customer is asked to try
again.
Pseudocode of ATM control. The pseudocode for the ATM is given as under:
These lines are the pseudocode for the ATM control loop, which is another form of
representation of Figure 1. Furthermore, you can add cancel event to the flow of control,
which could be implemented as goto exception handling code. Now, let us discuss controls
as a state machine engine.
As far as OO programming languages are concerned, there are some languages, such as
Concurrent Pascal or Concurrent C++, which support concurrency, but the application of
such languages in production environments is still limited. Ada language supports
concurrency, provided an object is equated with an Ada task, although the run-time cost is
very high. The major object oriented languages do not yet support concurrency.
UNIT-03
TOPIC: DESIGN OF AASSOCIATIONS
UNIT-03/LECTURE-05
During object design phase, we must formulate a strategy for implementing all associations
in the object model. We can either choose a global strategy for implementing all
associations uniformly, or a particular technique for each association.
Many associations are traversed in both directions, although not usually with equal
frequency. There are three approaches to their implementation:
Implement as an attribute in one direction only and perform a search when a backward
traversal is required. This approach is useful only if there is great disparity in traversal
frequency and minimizing both the storage cost and update cost are important.
Implement as attributes in both directions. It permits fast access, but if either attribute is
updated then the other attribute must also be updated to keep the link consistent .This
approach is useful if accesses outnumber updates
If it is a one-one association, link attribute is stored in any one of the classes involved.
UNIT-03
TOPIC: INHERITANCE ADJUSMENT
UNIT-03/LECTURE-06
As you know in object oriented analysis and design the terms inheritance defines a
relationship among classes, wherein one class shares the structure or behavior defined in
one or more classes. As object design progresses, the definitions of classes and operations
can often be adjusted to increase the amount of inheritance. In this case, the designer
should:
Rearrange and adjust classes and operations to increase inheritance
Abstract common behaviour out of groups of classes
Use delegation to share behaviour when inheritance is semantically invited.
Similar attributes in different classes may have different names. Give the attributes the
same name and move them to a common ancestor class. Then operations that access the
attributes will match better. Also, watch for similar operations with different names. You
should note that a consistent naming strategy is important to avoid hiding similarities.
Let us describe abstraction Abstraction means to focus on the essential, inherent aspects
of an entity and ignoring its accidental properties . In other words, if a set of operations
and/or attributes seem to be repeated in two classes. There is a scope of applying
inheritance. It is possible that the two classes are really specialised variations of the
something when viewed at a higher level of abstraction.
When common behaviour has been recognised, a common super class can be created that
implements the shared features, leaving only the specialised features in the subclasses.
This transformation of the object model is called abstracting out a common super class or
common behaviour. Usually, the resulting super class is abstract, meaning that there are no
direct instances of it, but the behaviour it defines belongs to all instances of its subclasses.
For example, again we take a draw operation of a geometric figure on a display screen
requires setup and rendering of the geometry.
The rendering varies among different figures, such as circles, lines, and spines, but the
setup, such as setting the colour, line thickness, and other parameters, can be inherited by
all figure classes from abstract class figure.
The creation of abstract super classes also improves the extensibility of a software product,
by keeping space for further extension on base of abstract class.
The term delegation Delegation consists of catching an operation on one object and
sending it to another object that is part, or related to the first object. In this process, only
meaningful operations are delegated to the second object, and meaningless operations can
be prevented from being inherited accidentally . It is true that Inheritance is a mechanism
for implementing generalization, in which the behaviour of super class is shared by all its
we dont take any liability for the notes correctness. http://www.rgpvonline.com
22
Let us take the example of implementation of inheritance. Suppose that you are about to
implement a Stack class, and you already have a List class available. You may be tempted to
make Stack inherit from List. Pushing an element onto the stack can be achieved by adding
an element to the end of the list and popping an element from a stack corresponds to
removing an element from the end of the list. But, we are also inheriting unwanted list
operations that add or remove elements from arbitrary positions in the list.
Often, when you are tempted to use inheritance as an implementation technique, you
could achieve the same goal in a safer way by making one class an attribute or associate of
the other class. In this way, one object can selectively invoke the desired functions of
another class, by using delegation rather than applying inheritance.
A safer implementation of Stack would delegate to the List class. Every instance of Stack
contains a private instance of List. The Stack :: push operation delegates to the list by
calling its last and add operations to add an element at the end of the list, and the pop
operation has a similar implementation using the last and remove operations. The ability to
corrupt the stack by adding or removing arbitrary elements is hidden from the client of the
Stack class.
UNIT-03
TOPIC:OBJECT REPRESENTATION
UNIT 3/LECTURE 7
Implementing objects is mostly straight forward, but the designer must choose when to use
primitive types in representing objects and when to combine groups of related objects.
Classes can be defined in terms of other classes, but eventually everything must be
implemented in terms of built-in-primitive data types, such as integer strings, and enumerated
types. For example, consider the implementation of a social security number within an
employee object. It can be implemented as an attribute or a separate class.
Defining a new class is more flexible but often introduces unnecessary indirection. In a similar
vein, the designer must often choose whether to combine groups of related objects The
object designer has to choose when to use primitive types in representing the objects or
when to combine the groups of objects. A class can be defined in terms of other classes but
ultimately all data members have to be defined in terms of built- in data types supported by a
programming language. For example, roll no. can be implemented as integer or string. In
another example, a two dimensional can be represented as one class or it can be
implemented as two classes – Line class and Point class.
UNIT-03
TOPIC: PHYSICAL PACKAGING
UNIT 3/LECTURE 8
Programs are made of discrete physical units that can be edited, compiled, imported, or
otherwise manipulated. In C and Fortran the units are source files; In Ada, it is packages. In
object oriented languages, there are various degrees of packaging. In any large project, careful
partitioning of an implementation into packages is important to permit different persons to
cooperatively work on a program.
Specifying what other classes a method can see defines the dependencies between classes.
Each operation should have a limited knowledge of the entire model, including the structure of
Classes, associations and operations. The fewer things that an operation knows about, the less
likely it will be affected by any changes. The fewer operations know about details of a class, the
easier the class can be changed if needed.
The following design principles help to limit the scope of knowledge of any operation:
Allocate to each class the responsibility of performing operations and providing
information that pertains to it.
Call an operation to access attributes belonging to an object of another class Avoid
traversing associations that are not connected to the current class. Define interfaces at
as high a level of abstraction as possible.
Hide external objects at the system boundary by defining abstract interface classes, that
is, classes that mediate between the system and the raw external objects.
Avoid applying a method to the result of another method, unless the result class is
already a supplier of methods to the caller. Instead consider writing a method to
combine the two operations.
ii) Coherence of entities.
One important design principle is coherence of entities. An entity, such as a class, an operation,
or a module, is coherent if it is organized on a consistent plan and all its parts fit together
toward a common goal. It shouldn’t be a collection of unrelated parts. A method should do one
thing well .a single method should not contain both policy and implementation.
A poli is the aki g of o te t depe de t de isio s. I ple e tatio is the e e utio of
full spe ified algorith s.
Policy involves making decisions, gathering global information, interacting with outside world
and interpreting special cases. Policy methods contain input output statements, conditionals
and accesses data stores. It doesn’t contain complicated algorithms but instead calls various
implementation methods. An implementation method does exactly one operation without
making any decisions, assumptions, defaults or deviations .All information is supplied as
arguments (list is long). Separating policy and implementation increase reusability. Therefore
implementation methods don’t contain any context dependency. So they are likely to be
reusable Policy method need to be rewritten in an application , they are simple and consists of
high level decisions and calls on low-level methods. A class shouldn’t serve too many purposes.
Modules should be defined so that interfaces are minimal and well defined. Connectivity of
object model can be used as a guide for partitioning modules. Classes that are closely
connected by associations should be in the same module. Loosely connected classes should be
grouped in separate modules. Classes in a module should represent similar kinds of things in
the application or should be components of the same composite object.
Try to encapsulate strong coupling within a module. Coupling is measured by number of
different operations that traverse a given association. The number expresses the number of
different ways the association is used, not the frequency.
Modularity is the property of a system that has been decomposed into a set of cohesive
and loosely coupled modules. Modules serve as the physical containers in which we declare
the classes and objects of our logical designs. A module can be edited, compiled or
imported separately. Different object-oriented programming languages support the packing
in different ways. For example, Java supports in the form of package, C++ in the form of
header files etc.
Modules are program units that manage the visibility and accessibility of names. Following
purposes can be solved by modularity.
A module typically groups a set of class definitions and objects to implement some
service or abstraction.
A module is frequently a unit of division of responsibility within a
programming team. A module provides an independent naming
environment that is separate from other modules within the program.
Modules support team engineering by providing isolated name spaces.
Information Hiding
Coherence of Entities
Constructing Physical Modules
we dont take any liability for the notes correctness. http://www.rgpvonline.com
29
Information Hiding: During analysis phase we are not concerned with information hiding. So,
visibilities of class members are not specified during analysis phase. It is done during object
design phase. In a class, data members and internal operations should be hidden, so, they
should be specified as private. External operations form the interface so they should be
specified as public.
Coherence of Entities: Module, class, method etc. are entities. An entity is said to coherent, if
it is organized on a consistent plan and all its parts fit together toward a common goal.
Constructing Physical Modules: Modules of analysis phase have changed as more classes
and associations have been added during object design phase. Now, the object designer has
to create modules with well-defined and minimal interfaces. The classes in a module should
have similar kinds of things in the system. There should be cohesiveness or unity of the
purpose in a module. So the classes, which are strongly associated, should be put into a single
module.
UNIT 3
TOPIC:DOCUMENTING DESIGN DECISIONS
UNIT 3/LECTURE 9
The above design decisions must be documented when they are made, or you will become
confused. This is especially true if you are working with other developers. It is impossible to
remember design details for any non trivial software system, and documentation is the best
way of transmitting the design to others and recording it for reference during maintenance.
The design document is an extension of the Requirements Analysis Document.
-> The design document includes revised and much more detailed description of the object
model-both graphical and textual. Additional notation is appropriate for showing
implementation decisions, such as arrows showing the traversal direction of associations and
pointers from attributes to other objects.
-> Functional model will also be extended. It specifies all operation interfaces by giving their
arguments, results, input-output mappings and side effects.
-> Dynamic model – if it is implemented using explicit state control or concurrent tasks then
the analysis model or its extension is adequate. If it is implemented by location within program
code, then structured pseudocode for algorithms is needed.
Keep the design document different from analysis document .The design document includes
many optimizations and implementation artefacts. It helps in validation of software and for
reference during maintenance. Traceability from an element in analysis to element in design
document should be straightforward. Therefore the design document is an evolution of
analysis model and retains same names.
The Design Document will include a revised and much more detailed description of the
Object Model in both graphical form (object model diagrams) and textual form (class
descriptions). You can use additional notation to show implementation decisions, such as
arrows showing the traversal direction of associations and pointers from attributes to other
objects.
The Functional Model can also be extended during the design phase, and it must be kept
current. It is a seamless process because object design uses the same notation as analysis,
but with more detail and specifics. It is good idea to specify all operation interfaces by
we dont take any liability for the notes correctness. http://www.rgpvonline.com
32