hkucuk

SOLID - Single-Responsibility Principle

May 25, 2021 • ☕️ 3 min read • 🏷 computer, software, solid

Translated by author into: English


The single-responsibility principle is a programming principle that stipulates that a class or function should have only one task. This principle makes our programs less error-prone, easier to understand, and easier to maintain.

If the class or function we are developing serves more than one purpose, it means that we are in an illegal development process. When we realize this, you have to break it down in accordance with the purposes.

As an example, let’s assume that a class should only be a database access object. This class should only contain functionality related to accessing the database. For example, adding records to the database, reading data from the database. This class must not contain any functionality other than accessing the database.

An example of applying the Single-responsibility principle in PHP is as follows:

<?php

// A class that is just a database access object
class Database {
  public function connect() {
    // Connecting to the database
  }

  public function insert() {
    // Adding records to database
  }

  public function select() {
    // Reading data from database
  }
}

$db = new Database();
$db->connect();
$db->insert();
$db->select();

In this example, the Database class contains only database access related functionality and no other functionality. Therefore, the Database class implements the Single-responsibility principle.

The PHP code below is an example of a class that violates the Single-responsibility principle.

<?php

// A class that contains database access and data manipulation functionality
class Database {
  public function connect() {
    // Connecting to the database
  }

  public function insert() {
    // Adding records to database
  }

  public function select() {
    // Reading data from database
  }

  public function processData() {
    // Data processing
  }
}

$db = new Database();
$db->connect();
$db->insert();
$db->select();
$db->processData();

In this example, the Database class includes both database access functionality and data manipulation functionality. Therefore, the Database class violates the Single-responsibility principle.


An example to apply the Single-responsibility principle in GoLang is as follows:

// A class that is just a database access object
type Database struct {}

// Database access functionality
func (d *Database) Connect() {
  // Connecting to the database
}

func (d *Database) Insert() {
  // Adding records to database
}

func (d *Database) Select() {
  // Reading data from database
}

db := &Database{}
db.Connect()
db.Insert()
db.Select()

In this example, the Database class includes only database access functionality and no other functionality. Therefore, the Database class implements the Single-responsibility principle.

Similar to the PHP example, the GoLang code below is an example that violates the Single-responsibility principle.

// A class that contains database access and data manipulation functionality
type Database struct {}

// Database access functionality
func (d *Database) Connect() {
  // Connecting to the database
}

func (d *Database) Insert() {
  // Adding records to database
}

func (d *Database) Select() {
  // Reading data from database
}

// Data processing functionality
func (d *Database) ProcessData() {
  // Data processing
}

db := &Database{}
db.Connect()
db.Insert()
db.Select()
db.ProcessData()

In this example, the Database class includes both database access functionality and data manipulation functionality. Therefore, the Database class violates the Single-responsibility principle.


Resources