CSC/ECE 517 Fall 2007/wiki3 3 ab: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 51: Line 51:
#[http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf Elegance and classes]
#[http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf Elegance and classes]
#[http://ieeexplore.ieee.org/iel5/32/21774/01010059.pdf?arnumber=1010059 A Logical Theory of Interfaces and Objects]
#[http://ieeexplore.ieee.org/iel5/32/21774/01010059.pdf?arnumber=1010059 A Logical Theory of Interfaces and Objects]
#[http://www.owlnet.rice.edu/~comp201/07-spring/assignments/finalproject/]
#[http://www.genwise.com/documentation/index.html?beginners_guide_to_orm_and_nhibernate.html]
#[http://msdn2.microsoft.com/en-us/library/ms954621.aspx]
#[http://designparadigm.wordpress.com/ Java by Experience]
#[http://designparadigm.wordpress.com/ Java by Experience]
#[http://www.codeproject.com/vb/net/OOPbyExample.asp OO Programming By Example]
#[http://www.codeproject.com/vb/net/OOPbyExample.asp OO Programming By Example]
Line 59: Line 56:
#[http://www.babysentry.com/tech_specs_benefits.htm Benefits of the Three-Tiered Architecture]
#[http://www.babysentry.com/tech_specs_benefits.htm Benefits of the Three-Tiered Architecture]
#[http://en.wikipedia.org/wiki/Separation_of_concerns Separation of concerns]
#[http://en.wikipedia.org/wiki/Separation_of_concerns Separation of concerns]
#[http://www.owlnet.rice.edu/~comp201/07-spring/assignments/finalproject/]
#[http://www.genwise.com/documentation/index.html?beginners_guide_to_orm_and_nhibernate.html]
#[http://msdn2.microsoft.com/en-us/library/ms954621.aspx]

Revision as of 19:18, 17 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.

Introduction

Separation of Responsibility

In Object-Oriented Design (OOD), each individual object should have as few responsibilities as possible, ideally one responsibility per object. That is to say,

Advantage of using separation of responsibility

Principle of Separation of Responsibility

There are several principles of Separation of Responsibility [1]:

  • Single Responsibility Principle (SRP). Different responsibilities should be divided among different objects, in another word, one object should have only one responsibility in ideal situation.
  • Encapsulation. One class should be responsible for knowing and maintaining a set of data, even if that data is used by many other classes. In another word, Data should be kept in only one place.
  • Expert pattern. The object that contains the necessary data to perform a task should be the object that manipulates the data.
  • The Dry principle. Code should not be duplicated. A given functionality should be implemented only in one place in the system.

Example

Java Example

There is an Java example of principle of Separation of responsibility.

public void createCustomer(Map requestParameters) {
	Customer customer = new Customer();
	customer.setName = requestParameters.get("name");

	//Check if a customer was already registered with that name
	if (customerService.getCustomerByName(customer.getName()) != null) {
		System.out.println("Customer already exists");
		return;
	}
	customer.setShoppingCart(new ShoppingCart());

	customerService.save(customer);
}

The method name is create. While others viewing the the code will expect to have a create function. But instead it has three functionality, creates a customer, checks if it's already exist and then save it. It should divide these functionality into 4 methods.

  1. bindValidateAndSave The application method. It tells what to do rather than how's done.
  2. bindCustomer bind and add new shoppingCart.
  3. validateCustomer validate if customer exist.
  4. saveCustomer save customer


The last method saveCustomer has only 1 line of code, why were we let it become a individual method? Although it might not improve readability for programmers, it’s a paradigm shift in how the method is addressed. Because it calls customerService.save(). it’s responsible that the save method is actually called right. Instead if we let it delegate to a newly extracted method (saveCustomer) it isn’t responsible for the explicit saving.

Reference

  1. Elegance and classes
  2. A Logical Theory of Interfaces and Objects
  3. Java by Experience
  4. OO Programming By Example
  5. TDD Design Starter Kit – Responsibilities, Cohesion, and Coupling
  6. Benefits of the Three-Tiered Architecture
  7. Separation of concerns
  8. [1]
  9. [2]
  10. [3]