CSC/ECE 517 Fall 2009/wiki3 17 VR

From Expertiza_Wiki
Jump to navigation Jump to search

Single choice principle
Bertrand Meyer is the author of the book, "Object Oriented Software Construction", which is considered a foundational text of object-oriented programming book. In this book, he has mentioned about the five principles which explain modularity requirements. This wiki will explore through the Single Choice Principle in particular and its usage.

Introduction

In 1978, Yourdon and Constantine defined a software module as “a lexically contiguous sequence of program statements, bounded by boundary elements, having an aggregate identifier.” They had also proposed two important techniques namely – “Coupling” and “Cohesion” for measuing the dependability between modules. The various fundamental requirements resulting from a "modular" design method are :

  • DECOMPOSABILITY
    Meyer defined decomposability as :
    " A software construction method satisfies Modular Decomposability if : it helps in the task of decomposing a software problem into a small number of less complex sub problems, connected by a simple structure, and independent enough to allow further work to proceed separately on each of them."
    

  • COMPOSABILITY
  • UNDERSTANDABILITY
  • CONTINUITY
  • PROTECTION

In order to ensure modularity the following five rules must be followed:

  • Direct mapping
  • Small interfaces(weak coupling)
  • Explicit interfaces
  • Information Hiding
  • Few interfaces

There are five principles of software construction which are to be followed along with the above stated requirements and rules. They are:

The Linguistic Modular Units principle  :

This principle states that modules must correspond to syntactic units in the language used.

The self-documentation principle

 :

This principle states that the designer of a module should strive to make all information about the module part of the module itself.

The uniform access principle

 :

This principle states that all services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation.

The open-closed principle

 :

This principle states that the modules should be both open and closed.

The single choice principle

 :

This principle states that whenever a software system must support a set of alternatives, one and only one module in the system should know their exhaustive list.

In detail

This principle is a consequence of both the open closed principle and the information hiding rule. Consider the following example: Assume you are building a system to manage a library. Considering Publications of a library, this type can be represented as follows in Pascal-Ada syntax:

type PUBLICATION =
record
author, title: STRING;
publication_year: INTEGER

case pubtype: (book, journal, conference_proceedings) of
book: (publisher: STRING);
journal: (volume, issue: STRING);
proceedings: (editor, place: STRING) -- Conference proceedings
end

This particular form uses the Pascal-Ada notion of “record type with variants” to describe sets of data structures with some fields (here author, title, publication_year) common to all instances, and others specific to individual variants.

The use of a particular syntax is not crucial here; Algol 68 and C provide an equivalent mechanism through the notion of union type. A union type is a type T defined as the union of pre-existing types A, B,... : a value of type T is either a value of type A, or a value of type B,...

Record types with variants have the advantage of clearly associating a tag, here book, journal, conference_proceedings, with each variant.

Let A be the module that contains the above declaration or its equivalent using another mechanism. As long as A is considered open, you may add fields or introduce new variants. To enable A to have clients, however, you must close the module; this means that you implicitly consider that you have listed all the relevant fields and variants. Let B be a typical client of A. B will manipulate publications through a variable such as

p: PUBLICATION

and, to do just about anything useful with p, will need to discriminate explicitly between the various cases, as in:

case p of
book: ...Instructions which may access the field p l publisher...
journal: ...Instructions which may access fields p l volume, pl issue...
proceedings: ...Instructions which may access fields p l editor, pl place...
end

The case instruction of Pascal and Ada comes in handy here; it is of course on purpose that its syntax mirrors the form of the declaration of a record type with variants. Fortran and C will emulate the effect through multi-target goto instructions (switch in C).

In these and other languages a multi-branch conditional instruction

if ...then ...elseif ...elseif ...else ...end

will also do the job.

Aside from syntactic variants, the principal observation is that to perform such a discrimination every client must know the exact list of variants of the notion of publication supported by A. The consequence is easy to foresee. Sooner or later, you will realize the need for a new variant, such as technical reports of companies and universities. Then you will have to extend the definition of type PUBLICATION in module A to support the new case. Fair enough: you have modified the conceptual notion of publication, so you should update the corresponding type declaration. This change is logical and inevitable. Far harder to justify, however, is the other consequence: any client of A, such as B, will also require updating if it used a structure such as the above, relying on an explicit list of cases for p. This may, as we have seen, be the case for most clients.

What we observe here is a disastrous situation for software change and evolution: a simple and natural addition may cause a chain reaction of changes across many client modules.

The issue will arise whenever a certain notion admits a number of variants. Here the notion was “publication” and its initial variants were book, journal article, conference proceedings; other typical examples include:

  • In a graphics system: the notion of figure, with such variants as polygon, circle,ellipse, segment and other basic figure types.
  • In a text editor: the notion of user command, with such variants as line insertion, line deletion, character deletion, global replacement of a word by another.
  • In a compiler for a programming language, the notion of language construct, with such variants as instruction, expression, procedure.
  • In any such case, we must accept the possibility that the list of variants, although fixed and known at some point of the software’s evolution, may later be changed by the addition or removal of variants. To support our long-term, software engineering view of the software construction process, we must find a way to protect the software’s structure against the effects of such changes. Hence the Single Choice principle:
    By requiring that knowledge of the list of choices be confined to just one module, we prepare the scene for later changes: 
    if variants are added, we will only have to update the module which has the information — the point of single choice. 
    All others, in particular its clients, will be able to continue their business as usual.

    Once again, as the publications example shows, traditional methods do not provide a solution; once again, object technology will show the way, here thanks to two techniques connected with inheritance: polymorphism and dynamic binding. No sneak preview in this case, however; these techniques must be understood in the context of the full method.

    The Single Choice principle prompts a few more comments:

    • The number of modules that know the list of choices should be, according to the principle, exactly one. The modularity goals suggest that we want at most one module to have this knowledge; but then it is also clear that at least one module must possess it. You cannot write an editor unless at least one component of the system has the list of all supported commands, or a graphics system unless at least one component has the list of all supported figure types, or a Pascal compiler unless at least one component “knows” the list of Pascal constructs.
    • Like many of the other rules and principles studied in this chapter, the principle is about distribution of knowledge in a software system. This question is indeed crucial to the search for extendible, reusable software. To obtain solid, durable system architectures you must take stringent steps to limit the amount of information available to each module. By analogy with the methods employed by certain human organizations, we may call this a need-to-know policy: barring every module from accessing any information that is not strictly required for its proper functioning.
    • You may view the Single Choice principle as a direct consequence of the Open-Closed principle. Consider the publications example in light of the figure that illustrated the need for open-closed modules: A is the module which includes the original declaration of type PUBLICATION; the clients B, C,... are the modules that relied on the initial list of variants; A' is the updated version of A offering an extra variant (technical reports).
    • You may also understand the principle as a strong form of Information Hiding. The designer of supplier modules such as A and A' seeks to hide information (regarding the precise list of variants available for a certain notion) from the clients.

    Conceptual Analysis

    Examples

    Conclusion

    References