CSC/ECE 517 Fall 2009/wiki3 7 Single Choice Pattern am: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=='''Single Choice Principle'''==
===Introduction===


Single Choice 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. It is one of the five principles of software construction as stated by Bertrand Meyer, the other four being the [http://hebb.cis.uoguelph.ca/~dave/343/Lectures/swquality.html#linguistic Linguistic Modular Units Principle], the Self- Documentation Principle, the [http://en.wikipedia.org/wiki/Uniform_access_principle Uniform- Access Principle], and the [http://en.wikipedia.org/wiki/Open/closed_principle Open- Closed Principle]. The Single Choice Principle may be viewed as a consequence of both the Open-Closed and information Hiding rules. It was applied when designing [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Eiffel].This principle is a particular case of the Don't repeat yourself principle.
===Violation of SCP===
Consider a  type used to manage a declared in Pascal-Ada syntax :
<pre>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</pre>
Let B be a typical client of A. B will manipulate publications through a variable such as
p:PUBLICATION
and to do anything useful with p, will need to discriminate explicitly between the various cases, as in:
<pre> case p of
book:...Instructions which may access the field p.publisher
journal:...Instructions which may access fields p.volume,p.issue
proceedings:...Instructions which may access fields p.editor,p.place
end</pre>
Now, whenever there is a need for a new variant, say technical reports of companies in this case, there is a need to extend the definition of type PUBLICATION in module A to support the new case, as well as  any client of A, such as B, will also require updating if it is used as a structure such as above, relying on an explicit list of cases for p.
What is observed is a violation of the SCP and is a disastrous situation for software change and evolution as a simple and natural addition may cause a chain reaction of changes across many client changes. This violation will occur whenever a certain notion admits a number of variants for ex. -
*In graphics system: the notion of figure, with such variants as polygon, circle, ellipse, and other basic figure types.
<pre>For example old style procedural code to print different "shape" objects might read:
if (type==CIRCLE)
  print "Circle.  r=" + radius;
else if (type==Square)
  print "Square.  sides=" + sideLength;
else if (type==Rectangle)
  print "Rect.  h=" + height + " w=" + width;
endif</pre> 
This same list of alternatives would be used in computing the area, and it rendering to a GUI and in checking for overlap with another shape, etc., etc. So these lists of cases  have to be modified in all locations whenever you add a new shape.       
*In a text editor: the notion of user command, with such variants as line insertion, line deletion, character deletion.
*In a compiler for a programming language, the notion of language construct, with such variants as instruction, expression, procedure.
===Implementation of SCP===
===Other Highlights===
===Conclusion===

Latest revision as of 00:31, 20 November 2009