CSC/ECE 517 Fall 2007/wiki3 3 as

From Expertiza_Wiki
Jump to navigation Jump to search

Topic

Take the principle of Separation of Responsibility and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.

Separation of Responsibility

Separation of Responsibility states that a given problem involves different kinds of concerns, which should be identified and separated to cope with complexity, and to achieve the required engineering quality factors such as robustness, adaptability, maintainability, and reusability. The principle can be applied in various ways and it is an ubiquitous software engineering principle.


Separation of Responsibility has many similar terms or principles. All of them constitute similar ideas. These terms include Single Responsibility Principle, low coupling, Separation of Concerns, modularity, Grasp pattern and Aspect Oriented Programming.


In object-oriented methods the separated concerns are modeled as objects and classes, which are generally derived from the entities in the requirement specification and use cases. In structural methods, concerns are represented as procedures. In aspect-oriented programming, the term concern is extended with the so-called crosscutting properties such as synchronization, memory management and persistency.


We can define a responsibility to be “a reason for change.” If you can think of more than one motive for changing a class, then that class has more than one responsibility. Responsibility should be assigned to the information expert—the class that has all the necessary information. In general, if you need to assign a new responsibility to an object that needs to be an information expert on class X, first look to assign the responsibility to objects that are already information experts on class X.

Principle and Description

All programming paradigms aid developers in the process of Separation of Responsibility. For example, object-oriented programming languages such as Java can separate concerns into objects, and a design pattern like MVC can separate content from presentation and data-processing (model) from content. Service-oriented design can separate concerns into services. Procedural programming languages such as C and Pascal can separate concerns into procedures. Aspect-oriented programming languages can separate concerns into aspects and objects.

We have organized the examples found over the web into two broad categories

  1. Separation at class level
  2. Separation at structural level

Then we have attempted to give some concrete examples of both

Separation at Class Level

We tried to present the best description in the part above but it is hard to find information about Separation of Responsibilities by itself. In order to understand the concept one has to also read many similar ideas and principles. Now lets look to other descriptions found on the web, some including information about similar concepts to Separation of Responsibility:


http://designparadigm.wordpress.com/ In our opinion this website is best in order to get started with the principle. Although the discussion is short, a clear description and a simple example is given. Further information about naming conventions (naming classes and methods) and their relationship with the principle is also provided. This site is ideal for starting to read about the topic.

David Hayden Blog This site presents good methods on how to separate responsibility. This link is actually a web developers blog and he describes how to keep low coupling between classes. The author indicates 'identifying unstable areas of application' is the key point for separation.

http://www.objectmentor.com/resources/articles/srp.pdf This is a good text about Single Responsibility Principle. The text includes a section on what responsibility is and how to determine a responsibility for a code. This site also gives nice class diagrams on how the responsibility is separated in a few applications including GUI and modem connection. This is a good link which explains the separation at the class level.

Wikipedia-Grasp Separation of Responsibility is one of the General Responsibility Assignment Software Patterns (Grasp) and this wiki describes all these principles in one page. Controller pattern, factory pattern, high cohesion and low coupling are key concepts for separating responsibilities.

http://www.bookrags.com/Modularity_(programming) This site is about modularity in programming. It gives a very simple explanation of the concept with examples from the outside world. It gives a lot of advantages of separating responsibilities including 'ease to find errors', ' ease to built bigger projects' and code reuse in other projects.

Objects By Design This site also gives some information on Grasp but also gives definitions on what the responsibilities of a class is.

http://www.ugolandini.net/AccoppiamentoCoesioneE.html Here you can find definitions of low coupling and high cohesion. The link also presents how to achieve these concepts in your program by giving some input output examples.

http://jayflowers.com/WordPress/?p=92 This site is also about Single Responsibility Principle and gives a good definition of what a 'responsibility' should have. The author summarizes the importance of separation with the sentence: 'Finding and separating those responsibilities from one another is much of what software design is really about'.

Separation at Structural Level

http://en.wikipedia.org/wiki/Model-view-controller One of the most important examples of Separation of Responsibility at the architecture level is the MVC pattern. Wikipedia is one of the best sources to read about MVC. Here MVC pattern is described in many different languages' context such as Java, Perl, Php and many more.

IndiaWebDevelopers This link contains a description of MVC and gives many advantages for it. The reason for MVC pattern is given as: 'separating the application object (model) from the way it is represented to the user (view) from the way in which the user controls it (controller)' in this site

http://trese.cs.utwente.nl/taosad/separation_of_concerns.htm This link gives information about Separation of Responsibility in the structural context. In order to separate responsibilities this site uses an abstraction problem solving approach.

http://www.acsac.org/waepssd/papers/02-piessens.pdf This paper describes the importance of Separation of Responsibilities in security applications. More specifically, this paper positions the separation-of-concerns principle as an essential means to build secure software.

Examples

Separation at Class Level

Below you can see many sites which include Separation of Responsibility example. We also present some information about the sites next to their links.


Javacoffeebreak This site contains an example using the concept of Separation of Responsibility for a GUI-based application. The example is easy to understand and comes with a detailed explanation of the code and how the principle is used. This is a good site to see the Separation of Responsibility applied to Java.

In this example a small applet that prints 'Hello World' changes color according to the buttons pressed. Here is the code for the main method:

public void init() {

             // This routine is called by the system to initialize the 
             // applet. It creates the canvas and lays out the applet 
             // to consist of a bar of control buttons below the canvas.
          
          setBackground(Color.lightGray);

          canvas = new ColoredHelloWorldCanvas();

          Panel buttonBar = new Panel();  // panel to hold control buttons
       
          Button redBttn = new Button("Red");  // Create buttons, add them
          redBttn.addActionListener(this);     //    to the button bar.  The
          buttonBar.add(redBttn);              //    parameter to the Button
                                               //    constructor is the text
                                               //    that appears on Button.
       
          Button greenBttn = new Button("Green");
          greenBttn.addActionListener(this);
          buttonBar.add(greenBttn);
       
          Button blueBttn = new Button("Blue");
          blueBttn.addActionListener(this);
          buttonBar.add(blueBttn);
       
          setLayout(new BorderLayout(3,3));   // Set layout for applet.
          add(buttonBar, BorderLayout.SOUTH); // Put panel at the bottom.
          add(canvas, BorderLayout.CENTER);   // Canvas will fill any
                                              //         remaining space.
      }

And below you can see the ColoredHelloWorldCanvas class. This is good object-oriented program design: The ColoredHelloWorldCanvas class is responsible for displaying a colored greeting, so it should contain all the data and behaviors associated with its role. On the other hand, this class doesn't need to know anything about buttons, layouts, and events. Those are the job of the main applet class. This Separation of Responsibility helps reduce the overall complexity of the program.


 class ColoredHelloWorldCanvas extends Canvas {
        
              // A canvas that displays the message "Hello World" on
              // a white background in a big, bold font.  A method is
              // provided for changing the color of the message.

           Color textColor;  // Color in which "Hello World" is displayed.
           Font textFont;    // The font in which the message is displayed.
   
           ColoredHelloWorldCanvas() {
                 // Constructor.
              setBackground(Color.white);
              textColor = Color.red;
              textFont = new Font("Serif",Font.BOLD,24);
           }
   
           public void paint(Graphics g) {
                 // Show the message in the set color and font.
              g.setColor(textColor);
              g.setFont(textFont);
              g.drawString("Hello World!", 20,40);        
           }
   
           void setTextColor(Color color) {
                 // Set the text color and tell the system to repaint
                 // the canvas so the message will be in the new color.
              textColor = color;
              repaint();
           }
   
        }

You can read more on the web site about the example given here. Here are some other good examples that can be found on the web:


http://en.wikipedia.org/wiki/Aspect-oriented_programming This page describes Aspect Oriented Programming (AOP) which is one of the well-known applications of Separation of Concerns. AOP cares more on separating cross cutting concerns. The code given here is an example to weak Separation of Responsibility. The improved version of the code is not given hence this site is good for only observing where you need to Separation of Concerns.

Krishnan Viswanath's Blog This page provides a more advanced example on how to use the principle in logins and password encryption. The implementation of this example contains 4 different classes for different tasks including: creating a database, logging, checking the password from database and encryption.

Separation at Structural Level

Eclipse Documentation This document teaches how to draw shapes using the MVC pattern. The separated responsibility components included are called View / EditPart / IFigure. Although there is not much information about the principle, the example is described in detail. This is a good link for observing the principle in graphical modeling framework.

Sun This page includes a description and analysis of a project. Though it does not provide any code, you can see how Separation of Responsibility is achieved in the design process. The project also uses the MVC pattern. One need not understand the entire project in order to visualize how the Separation of Concerns helps in the design process hence the long description of the project should not discourage you.

http://msdn2.microsoft.com/en-us/library/ms838356.aspx A more advanced site about component-based development for mobile devices. This site depicts how the separation-of-responsibility can be accomplished with multi-tiering. It gives a complex example, yet it is a good source to see how the principle is used in various applications.

References

1. http://en.wikipedia.org/wiki/Separation_of_concerns

2. http://designparadigm.wordpress.com/

3. http://129.10.116.80/home/lieber/courses/csu670/f06/coupling-blog/690.aspx.htm

4. http://www.objectmentor.com/resources/articles/srp.pdf

5. http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)

6. http://www.bookrags.com/Modularity_(programming)

7. http://www.objectsbydesign.com/books/larman_notes/6-DesignAndImplementationTechniques.html

8. http://www.ugolandini.net/AccoppiamentoCoesioneE.html

9. http://jayflowers.com/WordPress/?p=92

10. http://en.wikipedia.org/wiki/Model-view-controller

11. http://www.indiawebdevelopers.com/technology/java/mvcarchitecture.asp

12. http://trese.cs.utwente.nl/taosad/separation_of_concerns.htm

13. http://www.acsac.org/waepssd/papers/02-piessens.pdf

14. http://www.javacoffeebreak.com/books/extracts/javanotesv3/c6/s6.html

15. http://en.wikipedia.org/wiki/Aspect-oriented_programming

16. http://weblogs.java.net/blog/tchangu/archive/2006/02/a_simple_utilit_1.html

17. http://weblogs.java.net/blog/tchangu/archive/2006/02/a_simple_utilit_1.html

18. http://java.sun.com/blueprints/code/jps11/archoverview.html

19. http://msdn2.microsoft.com/en-us/library/ms838356.aspx