You are on page 1of 17

1/17/2013

1
CLASS DESIGN
OVERVIEW OF CLASS DESIGN
The classes discovered during the analysis can
be thought of as the skeleton of the final
system, but new classes are needed.
In particular operation found during analysis
must be expressed as algorithms, the classes,
association and attributes must be
implemented with specific data structure.
New class will be identified during this activity.
For example intermediate results might have
to be stored to avoid the need for
recompilation.
CONTD
OO design is an iterative process. When
designing OO system you should consider
some level of abstraction.
For each level you may need to add new
operation, attributes, and classes.
CONTD
Class design involve following step.
1) Bridging the gap
2) Realizing use case
3) Designing algorithms
4) Recursing downward
5) Refactoring
6) Design optimization
7) Reification of behavior
8) Adjustment of inheritance
9) Organizing a class design.
1/17/2013
2
BRIDGING THE GAP
Bridge the gap from high-level requirements
to lower-level services.
You must invent intermediate element to
bridge the gap.
Class diagram can be used to model both the
application domain and the solution domain.
Application object represent the concept of
domain that are relevant to the system.
Solution object represent components that do
not have a counterpart in the application
domain.
CONTD
During analysis we also identify solution
object that are visible to user such as
boundary and control object representing the
form and transaction defined by the system.
During system design we identify more
solution object in terms of software and
hardware platform.
Analysis reduces gap between the problem
and the machine by identifying objects
representing problem-specific concepts.
CONT
System design reduce gap between the problem
and the machine in two ways.
1) First system design results in a virtual machine
that provides a higher level of abstraction than
the machine. This is done by selecting off-the-
half components for std. services such as
middleware, user interface toolkit etc.
2) Second system design identifies off-the-shelf
components for application domain objects such
as reusable class libraries of banking object.
CONTD
New object often needed during object design
as use of design patterns introduces new
classes, implementation of algorithms may
necessitate object to hold values and new low
level operation may be needed during the
decomposition of high-level operation.
During object design we close the gap
between the application object and the off-
shelf component by identifying additional
solution object & refining existing object.
1/17/2013
3
RELAIZING USE CASES
Use case defines the required behavior, but
they do not define its realization.
The cases define system behavior during
design you must invent new operation and
new object that provide behavior.
Step 1 : list the responsibilities of a use case or
operation.
A responsibilities is something that an object
knows or something it must do.
Example consider online theater ticket system
CONTD
Finding unoccupied seat for desired show,
Making the seat as occupied,
Obtaining payment from customer,
Arranging delivery of the ticket , and
Crediting payment to the proper account.
CONTD
Step 2: each operation will have various
responsibilities.
group the responsibility into clusters and try
to make each cluster coherent. That is each
cluster should consist of related
responsibilities that can be serviced by a
single lowerlevel operation.
CONTD
Step 3: define an operation for each
responsibility cluster.
define operation so that is not restricted to
special circumstances, but do not make it
general that it is unfocused. The goal is to
anticipate future uses of new operation.
Step 4:Assign the new lower level operation to
classes.
If there is no good class to hold an operation,
you may need to invent new lower-level class.
1/17/2013
4
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
client, but the algorithms show how it is done.
CONTD
Steps for designing algorithms
1) Choose algorithms that minimize the cost of
implementing operation.
2) Select data structures appropriate to the
algorithms.
3) Define new internal classes & operations as
necessary.
4) Assign operations to appropriate classes.
CHOOSING ALGORITHMS
Many operations are straightforward because
they simply traverse the class model to
retrieve or change attributes or links.
However , a class model traversal cannot fully
express some operations. Pseudo code is
often use to handle these situations. Pseudo
code helps us to think about the algorithms
while deferring programming details.
When efficiency is not an issue you should use
simple algorithms .
CONTD
Few operations tend to be application
bottlenecks. For the remaining operations, it is
better to have a design that is simple,
understandable and easy to program than to
writing out minor improvement.
1/17/2013
5
CONTD
Consideration in choosing among alternative
algorithm include
Computational complexity :
It is essential to think about complexity i.e. how the
execution time (memory) grows with the number
of input values.
Example bubble sort algorithms time nearer to n^2.
most other algorithms time nearer to n logn.
CONTD
Ease of implementation and
understandability:
it is worth giving up some performance on non
critical operation if they can be implemented
quickly with simple algorithm.
Flexibility
Most of programs will be extended sooner or later. A
highly optimized algorithms often sacrifices readability
and ease of change.
One possibility is to provide two implementations of
critical algorithms.
CONTD
Fine timing the object model.
we have to think, whether there would be any
alternatives, if the object model were
structured differently.
CHOOSING DATA STRUCTURES
Choosing algorithms involves choosing the
data structures they work on. We must choose
the form of data structure that will permit
efficient algorithm. The data structures
organize data in from convenient for the
algorithm that use it.
Data structure are an instance of container
classes. Data structure include arrays, lisgts,
queues, stacks, trees etc.
1/17/2013
6
DEFINING INTERNAL CLASSES &
OPERATION
During the expansion of algorithms, new
classes of object may be needed to hold
intermediate results. New, low level
operations may be invented during the
decomposition of high-level operations.
CONTD
a complex operation can be defined in terms
of lower level operations on simpler objects.
These low level operation must be defined
during object design because most of them
are not externally visible
ASSIGNING OPERATION TO
CLASSES
When class is meaningful in the real world,
the operation on its are usually clear.
How do you decide what class owns
operation? When more than one objects are
involved in operation then it is difficult to
decide which object play lead role in the
operation. To decide ask yourself following
question.
CONTD
Receiver of action. Is one object acted on
while the other object perform the action?
It is best to associate the operation with the target
of the operation rather than initiator.
Query vs. update. Is one object modified by
the operation, while other objects are only
queried for their information?
the object changed is the target of the operation.
Focal class. Looking at the classesand the
association that are involved in the operation.
1/17/2013
7
CONTD
Analogy to real world. If the object were not
software, but were the real- world objects
what real object would you push, move,
activate, or otherwise manipulate to initiate
operation?
RECURSING DOWNWARD
Recursing downward organize operations as
layers. Operations in higher layers invoke
operations in lower layers.
Two ways of downward recursion.
1) By functionality
2) By mechanism
any large system mixes functionality layers
and mechanism layers.
FUNCTIONALITY LAYERS
Functionality recursion required high-level
functionality is broken into lesser operations.
If decomposition is done arbitrarily and the
pieces do not relate well to classes. To avoid
this combine similar operations and attach the
operations to classes.
An operation should be meaningful and not an
arbitrarily portion of code.
1/17/2013
8
MECHANISM LAYER
Mechanism recursion means that you build
the system out of the layers of needed
support mechanisms. In providing
functionality you need various mechanism to
store information, sequence control,
coordinate objects, transmit information,
perform computation , and provide other
kinds of computing infrastructure.
These mechanisms dont show up explicitly in
the high-level responsibility of a system, but
they are needed to make it all work.
CONTD
Example computing architecture includes
data structure and algorithms, and control
patterns.
Any large system mixes functionality layers
and mechanism layers. A system designed
entirely with functionality recursion is brittle
and supersensitive to changes in
requirements. A system designed entirely
doesnot actually do any thing useful.
REFACTORING
Refactoring is a process of improving a codes
structure without changing its functionality.
Refactoring produces highly decoupled object
which make them easy to test, easy to use,
more flexible, and therefore , more
changeable.
The initial design of a set of operation will
contain inconsistencies, redundancies &
inefficiencies.
CONTD
You make decision that are ultimately linked
to other decisions.
Revisit your design and rework the classes and
operations so that they cleanly satisfy all their
uses and are conceptually sound.
It means that you step back , look across many
different classes and operations and recognize
them to support further development better
Refactoring is continuous development.
1/17/2013
9
DESIGN OPTIMIZATION
A good way to design a system is to first get
the logic correct and then optimize it.
The basic design model uses the analysis
model as the framework for implementation.
The analysis model captures the logical
information about the system, while design
model must add development detail.
You can optimize analysis model to improve
performance, but an optimized system is more
obscure and less likely to be reusable.
CONTD
Design optimization involves the following
tasks.
1) Add redundant association for efficient
access.
2) Rearrange execution order for efficiency.
3) Save derived values(attributes) to avoid
recomputation.
ADD REDUNDANT ASSOCIATION
FOR EFFICIENT ACCESS
During analysis, it is undesirable to have
redundancy in association network because
redundant associations do not add any
information. During design we evaluate the
structure of class model for an
implementation.
For that we have to answer the following
questions:
CONTD
Is there any specific arrangement of the
network that would optimize critical aspects
of completed system?
Should network be restructured by adding
new association?
Can exist association be omitted?
The association that are useful during useful
during analysis may not form the most
efficient network when the access pattern and
relative frequencies of different kind of access
are considered.
1/17/2013
10
CONTD
In case where the number of hits from a query
is low because only a fraction of object satisfy
the test, we can build an index to improve
access to objects that must be frequently
retrieved.
Analysis the use of paths in the association
network as follows:
1) Examine each operation and see what
associations it must traverse to obtain its
information.
CONTD
Note which associations are traversed in both
directions, and which are traversed in a single
direction only,
For each operation note the following items:
Frequency of access: How often is the
operation called? How costly is to perform?
Fan-out: What is the fan-out along a path
through the network? Estimate the average
count of each many association
encountered along the path.
CONTD
Selectivity: What is the fraction of hits on
the final class, that is, objects that meets
selection criteria and is operated on? if most
object are rejected during traversal for some
reason, then a simple nested loop may be
inefficient at finding target objects.
REARRANGING EXECUTION ORDER
FOR EFFICIENCY
You should optimize the algorithm first.
Algorithms and data structure should be
considered first.
One key to algorithms optimization is to
eliminate dead paths as early as possible.
Sometimes the execution order of a loopmust
be inverted.
1/17/2013
11
SAVING DERIVED VALUES TO AVOID
RECOMPUTATION
Data can be 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 are
changed.
Derived attribute must be change as base
values change.
CONTD
Explicit update:
Each attributes is defined in terms of one or more
fundamental base objects.
The designer determined which derived attributes
are affected by each change to a fundamental
attributes and insert code into the update
operation on the base object to explicitly update
the derived attributes that depends on it.
Periodic re-computation:
Application often update values in bunches. You
could re-compute all derived attributes
periodically, instead of each source change.
CONTD
Active values:
An active values is a value that is automatically
kept consistent with its source values
A special registration mechanism records the
dependency of derived attributes on source
attributes. The mechanism monitor the value of
source attributes and updates the values of the
derived attributes whenever there is a change.
REIFICATION OF BEHAVIOR
Reification is the promotion of something that
is not an object into an object.
It is helpful technique for meta application
because it lets you shift the level of
abstraction.
As an example of reification, consider a
database manager. A developer could write
code for each application so that it can read
and write from files.
1/17/2013
12
CONTD
A database manager has abstract functionality
that provides a general purpose solution to
accessing data reliably and quickly for multiple
user.
Behavior written in code is rigid. You can
execute it, but cannot manipulate it at run
time. Most of the time this is fine, because all
you want to do is execute it. But if you need to
store, pass, or modify the behavior at run
time, you should reify it.
CONTD
Reification is the promotion of something that
is not an object into an object. Behavior
usually meets this description.
It isnt usually something that you normally
manipulate. If you reify behavior, you can
store it, pass it to other operations, and
transform it. Reification adds complexity but
can dramatically expand flexibility of a system.
You reify behavior by encoding it into object &
decoding it when it is run. The run-time cost
may or may not significant.
CONTD
If encoding invoked high-level procedures->
the cost is only few indirections.
If the entire behavior is encoded in a different
language, however , it must be interpreted,
which can be magnitude slower than direct
execution of code.
If the encode behavior constitutes a small part
of the run-time execution time, the
performance overhead may not matter.
ADJUSTMENT OF INHERITANCE
As class design progresses, you can often
adjust the definitions of classes and
operations to increase inheritance by
performing the following steps.
1) Rearrange classes and operations to increase
inheritance.
2) Abstract common behavior out of group of
classes.
3) Use delegation to share behavior when
inheritance is semantically invalid.
1/17/2013
13
Rearrange classes and operations
to increase inheritance
Sometimes several classes define the same
operation and can easily inherit it from a
common ancestor, but more often operation in
different classes are similar but not identical by
adjusting the definitions of the operation, you
may be able to cover them with a single
inherited operation.
you can use the following kinds of adjustments
to increase chance of inheritance.
CONTD
Operation with optional argument: some
operation may have fewer arguments. The
missing argument can be added but ignored.
Operation that are special cases: some
operation may have fewer arguments because
they are special cases of more general
arguments. Implements the special operations
by calling the general operation with
appropriate parameters values.
CONTD
Inconsistent names: 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.
Irrelevant operations: several classes in a
group may define an operation, but some
others may not. Define operation on the
common ancestor class and declare it as a no-
op on the classes that dont care about it.
ABSTRACT OUT COMMON
BEHAVIOR
Re-examine the object model looking for
commonly between classes. New classes and
operation are often added during design.
If a set of operation / attributes seems to be
repeated in two classes , it is possible that the
two classes are specialized variations of
things. When common behavior is recognize ,
a common super class can be created.
1/17/2013
14
CONTD
Usually the superclass is abstract meaning no
direct instances. Sometimes a superclass is
abstract even when there is only one subclass;
here there is no need of sharing.
Superclass may be reusable in future projects.
When a project is completed, the reusable
classes should be collected, documented and
generalized so that they may be used in future
projects.
CONTD
Another advantages of abstract superclasses
other than sharing and reuse is modularity.
Abstract superclasses improve the
extensibility of a software product. It helps in
the configuration mgmt of software
maintenance and distribution.
USE DELEGATION TO SHARE
IMPLEMENTATION
Sometimes programmer use inheritance as an
implementation technique with no intention
of guaranteeing the same behavior.
Sometimes existing class implements some of
the behavior that we want to provide in newly
defined class, although in other respects the
two classes are different. The designer ay
inherit from existing class to achieve part of
the implementation of the new class.
DELEGATION
Delegation is the alternative to
implementation inheritance that should be
used when reuse is desired. A class is said to
be delegate to another class if it implements
an operation by resending a message to
another class. Delegates makes explicit the
dependencies between the reused class and
new class.
1/17/2013
15
ORGANIZE A CLASS DESIGN
You can improve the organization of class
design with the following steps:
1) Hiding internal information from outside
view.
2) Maintain coherence of entities.
3) Constructing physical modules.
Hiding internal information from
outside view
One design goal is to treat classes as black
box , whose external interface is public but
whose internal details are hidden from view.
Hiding internal information permit as
implementation of a class to be changed
without requiring any client of the class to
modify code.
During design the public interface of each
class must be defined carefully.
CONTD
Designer must decide which attributes should
be accessible from outside the class. These
decision should be recorded in the object
model by adding the annotation {private} after
attributes that are to be hidden.
Taken to an extreme a method on a class
could traverse all the association of the object
model to locate and access other object in
system.
CONTD
We need top define the bound of visibility
that each method requires. Specifying what
other classes a method can see defines the
dependencies between classes.
Each operation should have limited knowledge
of the entire model, including structure of
classes, associations and operation. The fewer
things operation know the less likely it
affected by any changes.
1/17/2013
16
CONTD
Design principle
1) Allocate to each class the responsibility of
performing operations and providing
information that pertains to it.
2) Call an operation to access attributes
belonging to an object of another class.
3) Avoid traversing associations that are not
connected to the current class.
4) Define interfaces at as high a level of
abstraction as possible.
CONTD
5) 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.
6) Avoid applying a method to the result of
another method, unless the result class is
already a supplier of methods to the caller.
COHERENCE OF ENTITIES
An entity is coherent if it is organized on a
consistent plan and all its parts fit together
towards common goal.
A method should not contain both policy and
implementation.
Policy involves making decisions, gathering
global information, interacting with out side
would and interpreting special cases.
All information to implementation method is
supplied as arguments.
CONTD
An implementation does one operation
without making any decision or assumption.
1/17/2013
17
CONSTRUCTING PHYSICAL
METHOD
The initial organization may not be suitable for
final packaging of system implementation.
1) New classes added to existing module or
layer or separate module.
2) Modules should be defined so that interfaces
are minimal and well defined.
3) Connectivity of object model can be used as
a guide for partitioning modules.
CONTD
4) Classes that are closely connected by
associations should be in the same module
loosely connected classes should be grouped
in separate modules.
5) Classes in a module should represent similar
kind of things in the application or should be
components of the same composite object.
6) Try to encapsulate strong coupling within a
module. Coupling is measured by number of
different operations that traverse a given
association.

You might also like