Page 175 - DCAP308_OBJECT_ORIENTED_ANALYSIS_AND_DESIGN
P. 175

Unit 14: Steps for Class Design




               definitions. Often the simple definition is also the best algorithm for computing the  Notes
               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.


                 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.


                 Example: For a bubble sort algorithm, time ∝ n 2
                    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.
          2.   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.
          3.   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
               client’s description of the problem. These low-level classes are the implementation
               elements out of which the application classes are built.

          4.   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



                                           LOVELY PROFESSIONAL UNIVERSITY                                   169
   170   171   172   173   174   175   176   177   178   179   180