Sparkles

that were shone when I got tempered!

SOLID – SRP (Single Relationship Principle)

leave a comment »

This is the principle that simply described through cohesion.

Facts :

Each responsibility should be a separate class.

Because each responsibility is an axis of change.

So simply, there should never be more than one reason for a class to change.

Example :

Bad SRP Design

WeatherDisplay.java

public class WeatherDisplay{

 private int temperature;
 private int windSpeed;

public WeatherDisplay(int temperature, int windSpeed){
 this.temperature = temperature;
 this.windSpeed = windSpeed;
 }

public void DisplayWeather(){
 DisplayTemperature();
 DisplayWindSpeed();
 }

private void DisplayTemperature(){
 System.out.println("The current temperature is: " + temperature + " degrees celcius.");
 }

 private void DisplayWindSpeed(){
 System.out.println("The current wind speed is: " + windSpeed + " meters per second.");
 }
}

Here, WeatherDisplay class has two distinct responsibilities (display Temperature and display Wind Speed). So,  There’s two reason to change the WeatherDisplay class in the future. (Eg :  the temperature should be displayed in Fahrenheit and the wind speed should be displayed in knots)

Good SRP Desing

WeatherDisplay.java

public class WeatherDisplay{

 private DisplayTemperature displayTemperature;
 private DisplayWindSpeed displayWindSpeed;

public WeatherDisplay(int temperature, int windSpeed){
 displayTemperature = new DisplayTemperature(temperature);
 displayWindSpeed = new DisplayWindSpeed(windSpeed);
 }

public void DisplayWeather(){
 displayTemperature.Display();
 displayWindSpeed.Display();
 }
}

DisplayTemperature.java


public class DisplayTemperature{
 private int temperature;

public DisplayTemperature(int temperature){
 this.temperature = temperature;
 }

public void Display(){
 System.out.println("The current temperature is: " + temperature + " degrees celcius.");
 }
}

DisplayWindSpeed.java


public class DisplayWindSpeed{
 private int windSpeed;

public DisplayWindSpeed(int windSpeed){
 this.windSpeed = windSpeed;
 }

public void Display(){
 System.out.println("The current wind speed is: " + windSpeed + " meters per second.");
 }
}

Here because of delegating the responsibilities for each classes, each class have one responsibility hence each have exact one reason to change it

Advertisements

Written by Namal Fernando

August 4, 2013 at 4:48 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!

%d bloggers like this: