CSC/ECE 517 Fall 2007/wiki3 3 qq: Difference between revisions
No edit summary |
|||
Line 10: | Line 10: | ||
== Why Separation of Responsibility == | == Why Separation of Responsibility == | ||
* Decreases the coupling between two classes. One class do not need to know the what happened in the other class and what kind of object the pointer points to. | * Decreases the coupling between two classes. | ||
One class do not need to know the what happened in the other class and what kind of object the pointer points to. | |||
*Don't Repeat Yourself | *Don't Repeat Yourself | ||
If you have more than one way to express the same thing, at some point the two or three different representations will most likely fall out of step with each other. Even if they don't, you're guaranteeing yourself the headache of maintaining them in parallel whenever a change occurs. And change will occur. Don't repeat yourself is important if you want flexible and maintainable software. | If you have more than one way to express the same thing, at some point the two or three different representations will most likely fall out of step with each other. Even if they don't, you're guaranteeing yourself the headache of maintaining them in parallel whenever a change occurs. And change will occur. Don't repeat yourself is important if you want flexible and maintainable software. | ||
*Once and Only Once | *Once and Only Once |
Revision as of 22:09, 16 November 2007
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.
Defination of Separation of Responsibility
In Object-Oriented Design (OOD)
- One class should be responsible for knowing and maintaining a set of data, even if that data is used by many other classes.
- Different responsibilities should be divided among different objects. Ideally, each individual object has one single responsibility, which we call Single Responsibility Principle (SRP).
Why Separation of Responsibility
- Decreases the coupling between two classes.
One class do not need to know the what happened in the other class and what kind of object the pointer points to.
- Don't Repeat Yourself
If you have more than one way to express the same thing, at some point the two or three different representations will most likely fall out of step with each other. Even if they don't, you're guaranteeing yourself the headache of maintaining them in parallel whenever a change occurs. And change will occur. Don't repeat yourself is important if you want flexible and maintainable software.
- Once and Only Once
Each and every declaration of behavior should occur once, and only once. This is one of the main goals, if not the main goal, when refactoring code. The design goal is to eliminate duplicated declarations of behavior, typically by merging them or replacing multiple similar implementations with a unifying abstraction.
A Example
Let's think about a example shown below: Each node consists of data and a link (next) and the LinkedList implemented from Nodes.
class Node { Object data; Node next; //point to next node } class Linklist implement Node{ ... ... public Object currentNode() { Node temp=cursor(); return temp.data; } }
Then, we make a object client responsible for traversing a list which and keeping track of where it is.
class client{ Linklist a }
The Single Responsibility Principle says that a class should have one, and only one, reason to change. As an example, imagine the following class:
class Employee {
public Money calculatePay() public void save() public String reportHours()
} This class violates the SRP because it has three reasons to change:
The business rules having to do with calculating pay.
The database schema.
The format of the string that reports hours.
We don't want a single class to be impacted by these three completely different forces. We don't want to modify the Employee class every time the accounts decide to change the format of the hourly report