Object Desigin
Object Desigin
MODULE IV
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.
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, time n2 Most
other algorithms, time n log n
b) Ease of implementation and understandability:
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.
ii) Choosing Data Structures
Choosing algorithms involves choosing the data structures they work on. We must choose
the form of data structures that will permit efficient algorithms. The data structures do not
add information to the analysis model, but they organize it in a form convenient for the
algorithms that use it.
iii) Defining Internal Classes and Operations
During the expansion of algorithms, new classes of objects may be needed to hold
intermediate results. New, low level operations may be invented during the
decomposition of high level operations. A complex operation can be defined in
terms of 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
clients description of the problem. These low-level classes are the implementation
elements out of which the application classes are built.
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?
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?
*
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:
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 Recomputation: Base values are updated in bunches. Recompute all derived
attributes periodically without recomputing derived attributes after each base value is
changed. Recomputation 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 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.
4.6 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
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.
Fig 4.1
.
Fig 4.2
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.
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.
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.
parts. A method should do one thing well .a single method should not contain both policy
and implementation.
A policy is the making of context dependent decisions.
Implementation is the execution of fully specified algorithms.
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 doesnt 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 dont 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 shouldnt serve
too many purposes.
iii) Constructing physical modules.
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.
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 artifacts. 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.