RT 701 Object Oriented Modeling and Design - Module 4
RT 701 Object Oriented Modeling and Design - Module 4
RT 701 Object Oriented Modeling and Design - Module 4
Module 4
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.
Overview of Object Design
During object design, the designer carries out the strategy chosen during the system design and
fleshes out the details. There is a shift in emphasis from application domain concepts toward
computer concepts. The objects discovered during analysis serve as the skeleton of the design,
but the object designer must choose among different ways to implement them with an eye
toward minimizing execution time, memory and other measures of cost. The operations
identified during the analysis must be expressed as algorithms, with complex operations
decomposed into simpler internal operations. The classes, attributes and associations from
analysis must be implemented as specific data structures. New object classes must be
introduced to store intermediate results during program execution and to avoid the need for
recomputation. Optimization of the design should not be carried to excess, as ease of
implementation, maintainability, and extensibility are also important concerns.
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.
b) Designing algorithms
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:
a) To implement functions for which no procedural specification
b) 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.
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 clients description of the problem. These low-level classes are the
implementation elements out of which the application classes are built.
iv) Assigning Responsibility for Operations
Many operations have obvious target objects, but some operations can be performed at several
places in an algorithm, by one of the several places, as long as they eventually get done. Such
operations are often part of a complex high-level operation with many consequences.
Assigning responsibility for such operations can be frustrating, and they are easy to overlook
in laying out object classes because they are easy to overlook in laying out object classes
because they are not an inherent part of one class. When a class is meaningful in the real
world, then the operations on it are usually clear. During implementation, internal classes are
introduced.
How do you decide what class owns an operation?
When only one object is involved in the operation, tell the object to perform the operation.
When more than one object is involved, the designer must decide which object plays the lead
role in the operation. For that, ask the following questions:
Is one object acted on while the other object performs the action? It is best to associate
the operation with the target of the operation, rather than the initiator.
Is one object modified by the operation, while other objects are only queried for the
information they contain? The object that is changed is the target.
Looking at the classes and associations that are involved in the operation, which class is
the most centrally-located in this subnetwork of the object model? If the classes and
associations form a star about a single central class, it is the target of the operation.
If the objects were not software, but the real world objects represented internally, what
real world objects would you push, move, activate or manipulate to initiate operation?
Assigning an operation within a generalization hierarchy can be difficult. Since the definitions
of the subclasses within the hierarchy are often fluid and can be adjusted during design as
convenient. It is common to move an operation up and down in the hierarchy during design, as
its scope is adjusted.
Design Optimization
The basic deign 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
i) 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.
Analyze the use of paths in the association network as follows:
Examine each operation and see what associations it must traverse to obtain its
information. Note which associations are traversed in both directions, and which are
traversed in a single direction only, the latter can be implemented efficiently with one way
pointers.
For each operation note the following items:
What is the fan-out along a path through the network? Estimate the average count of
each many 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 one links do not increase the fan-out, although they increase
the cost of each operation slightly, dont worry about such small effects.
What is the fraction of hits on the final class , that is , objects that meets 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 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.
ii)Rearranging Execution Order for Efficiency
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.
iii) Saving Derived Attributes to Avoid Recomputation:
Data that is redundant because it can be derived from other data can be cached or store
in its computed form to avoid the overhead of recomputing 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:
approach. Periodic recomputation 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 recomputation 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.
Implementation of Control
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
handled using error subroutines , exception handling supported by the language , or
setting and testing of status flags. In the case of exception handling, use goto statements.
ii) State machine engine
The most direct approach to control is to have some way of explicitly
representing and executing state machine. For example, state machine engine class helps
execute state machine represented by a table of transitions and actions provided by the
application. Each object instance would contain its own independent state variables but
would call on the state engine to determine next state and action. This approach allows you
to quickly progress from analysis model to skeleton prototype of the system by defining
classes from object model state machine and from dynamic model and creating stubs of
action routines. A stub is a minimal definition of function /subroutine without any internal
code. Thus if each stub prints out its name , technique allows you to execute skeleton
application to verify that basic flow of control is correct. This technique is not so difficult.
iii) Control as Concurrent Tasks
An object can be implemented as task in programming language /operating system. It
preserves inherent concurrency of real objects. Events are implemented as inter task calls
using facilities of language/operating system. Concurrent C++/Concurrent Pascal support
concurrency. Major Object Oriented languages do not support concurrency.
Adjustment of Inheritance
The definitions of classes and operations can often be adjusted to increase the amount of
inheritance.
The designer should:
i) Rearrange classes and operations
10
Sometimes the same operation is defined across several classes and can easily be inherited
from a common ancestor, but more often operations in different classes are similar but not
identical. By slightly modifying the definitions of the operations or the classes , the operations
can often be made to match so that they can be covered by a single inherited operation. Before
inheritance can be used , each operation must have the same interface and the types of
arguments and results. If the signatures match, then the operations must be examined to see if
they have the same semantics. The following kinds of adjustments can be used to increase the
chance of inheritance.
Some operations may have fewer arguments than others .The missing arguments can be
added but ignored.
Some operations may have few arguments because they are special cases of more
general arguments .Implement the special operations by calling the general operation
with appropriate parameter values.
Similar attributes in different classes may have different names. Give the attributes the
same name and move them to a common ancestor class. These operations that access
the attributes will match better.
Any operation may be defined on several different classes in a group but undefined on
the other classes. Define it on the common ancestor class and define it as no operation
on the values that do not care about it.
11
12
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.
13
14
If it is a one-one association , link attribute is stored in any one of the classes involved.
Object Representation
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.
15
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.
Physical Packaging
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.
Packaging involves the following issues:
i) Hiding internal information from outside view.
One design goal is to treat classes as black boxes , whose external interface is public
but whose internal details are hidden from view. Hiding internal information permits
implementation of a class to be changed without requiring any clients of the class to modify
code. Additions and changes to the class are surrounded by fire walls that limit the effects of
any change so that changes can be understood clearly. Trade off between information hiding
and optimization activities. During analysis , we are concerned with information hiding.
During design , the public interface of each class must be defined carefully. The designer must
decide which attributes should be accessible from outside the class. These decisions should be
recorded in the object model by adding the annotation {private} after attributes that are to be
hidden , or by separating the list of attributes into 2 parts. Taken to an extreme a method on a
class could traverse all the associations of the object model to locate and access another object
in the system .This is appropriate during analysis , but methods that know too much about the
entire model are fragile because any change in representation invalidates them. During design
we try to limit the scope of any one method. We need top define the bounds of visibility that
each method requires. 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.
16
The following design principles help to limit the scope of knowledge of any operation:
Avoid traversing associations that are not connected to the current class.
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.
17
During analysis and system design phases we partitioned the object model into modules.
* The initial organization may not be suitable for final packaging of system implementation
new classes added to existing module or layer or separate module.
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.
18