Single Responsibility Principle in Object Oriented Design

SOLID Principals of OOD POST #1 : SINGLE RESPONSIBLITY PATTEN

Single Responsibility Principle in Object Oriented Design deals with atomizing a complex task and not letting each function do more than one.

“Walking on water and developing software from a specification are easy if both are frozen.” – Edward V. Berard

We all know that software development specifications aren’t static! The software that you develop is bound to change anytime. The software does what it is supposed to do today. Is that enough ? Is your software is designed in a way to support changes?

Applying good Object Oriented Design is the key.

Oh. I know what Object Oriented Design is and I have also applied it in all my projects.

If this is what you are thinking right now, ask these questions to yourself.

1. Is your code Re-usable?

2. Can be changed with minimal effort if requirement changes ?

3. Can be extended without changing existing code?

Don’t worry. We are not alone 🙂 Let’s learn together on how to make our software more flexible to changes.

Many people have already thought lot about this and have come up with good OOD principles.

There are 5 principles which we have to master.

S = Single Responsibility Principle
O = Opened Closed Principle 
L = Liscov Substitution Principle
I = Interface Segregation Principle
D = Dependency Inversion Principle

Single Responsibility Principle:

A class should have one, and only one, reason to change.

First let’s understand this term with real time examples

Single Responsibility Principle illustration

Consider you work for one of the software firms in India. In my spare time you do some writing, newspaper editing and your routine task (coding). Basically, you have multiple responsibilities/interests in my life.

ALSO READ  Creating a cron job in cPanel to run PHP script

When something bad happens at work place, your boss scolds for some mistake and you get distracted. Basically, if one thing goes bad, everything will mess up.

The most famous example:

Single Responsibility Principle in Object Oriented Design

– “From codeProject.com”

Just because you can implement all the features in a single device, will you do it? It’s really hard to maintain and manage.

So this principle says if you have a class that handles more than one responsibility then you have to rethink about the class and split the class. Shocking Right! Usually we apply this rule for methods/functions.

Let’s discuss with a piece of code.

   <?php 
        class Student { 

            public $studentName = ''; 
                public $studentRegNo = ''; 


            // Responsibility 1
            function insert($aStudent) { 
                    // Logic for inserting a student to database.
            } 

            function selectStudent($aStudent){
                //logic
            }

            function deleteStudent($aStudent){
                //logic
            }

            // Responsibility 2
            function generateMarkSheet($aStudent) { 
                    //logic for generating marks.
            } 

        } 
?>

If you notice ‘Student’ class it has many responsibilities.This class should handle all CURD operations and should also print mark sheet for student. The problem here is the methods inside the class are dependent on each other.

Every time one gets changed there is a chance that the other also gets changed because both are staying in the same home and both have same parent.

Solution:

Single Responsibility Principle in Class Level:

Let’s split the classes and we will give one responsibility for each class according to SRP.

Class 1 : Student (Contains Properties (Data))

Class 2 : StudentDB (CURD operations)

Class 3 : StudentReport (Generates reports)

class Student {
        private $studentName = ''; 
        	private $studentRegNo = '';

        public function getstudentName()
        {
            return $this->studentName;
        }

        public function setstudentName($studentName)
            {
                $this->studentName = $studentName;
        }
    }


class StudentDB {

       function selectStudent($aStudent){
                    //logic
        }

        function deleteStudent($aStudent){
                    //logic
        }
    }

class StudentDB {
        function generateMarkSheet($aStudent) { 
                        //logic for generating marks.
        } 
    }

} 

Single Responsibility Principle in Method Level:

Single Responsibility principle must be strictly followed when it comes to methods to avoid spaghetti code.

ALSO READ  Using MySQL Indexes for Performance Optimization

Example:

public function insertStudent($aStudent){
   /*
    1. establish mysql connection.
    2. validate aStudent object.
    3. insert data to database.
}

As we see insertStudent function duty is to insert student data to database and not to establish mysql connection or validate the object. Here we can delegate other responsibilities to separate functions. By doing that it will be easy for us to debug if something goes wrong.

Leave a Reply