JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

Object-oriented design principle - Single Responsibility Principle

Author:JIYIK Last Updated:2025/03/19 Views:

The single responsibility principle is the simplest principle in object-oriented design. The so-called single responsibility, to put it simply, is that a class is responsible for only one function. The single responsibility principle is defined as follows:

Single Responsibility Principle: A class is only responsible for the corresponding responsibilities in a functional area, and the functional responsibility should be completely encapsulated by this class.

In our program design process, a class should not have too many methods, as long as they can complete their corresponding functions. If a class contains two functions, it is possible that when we modify one function, it will affect the other function.

Suppose we have an instance of a shopping cart

//购物车
class Cart
{
	// 添加商品
	public function addItem(){ /* ... */}

	// 移除商品
	public function deleteItem(){/* ... */}

	// 获取商品
	public function getItem(){/* ... */}

	// 设定订单信息
	public function setOrderInfo($info){/* ... */}

	// 取得付款信息
	public function getPaymentInfo(){/* ... */}

	// 保存订单
	public function saveOrder(){/* ... */}

	// 发送订单确认邮件
	public function sendMail(){/* ... */}

}

$cart = new Cart();
$cart->addItem(); // 添加商品
$cart->getPaymentInfo(); //获取付款信息

We can see that in a shopping cart class, in addition to the methods of adding, deleting and getting items, there are also methods for setting order information, obtaining payment information, saving orders and sending order confirmation emails. Obviously, this class has done something that it should not be responsible for. If we use the order somewhere else, we still have to instantiate an instance of a shopping cart, which is very strange. Therefore, according to the single responsibility principle, we need to split this class. Take out the order method and define a separate Order class.

// 订单
class Order
{

	// 设定订单信息

	public function setOrderInfo($info){/* ... */}
	
	// 取得付款信息

	public function getPaymentInfo(){/* ... */}
	
	// 保存订单

	public function saveOrder(){/* ... */}

	// 发送订单确认邮件

	public function sendMail(){/* ... */}

}

// 重构后的购物车
class Cart
{
	// 添加商品
	public function addItem(){ /* ... */}	

	// 移除商品
	public function deleteItem(){/* ... */}	

	// 获取商品
	public function getItem(){/* ... */}
}

$cart = new Cart();
$order = new Order();
$cart->addItem(); // 添加商品
$order->getPaymentInfo(); //获取付款信息

With this split, when we need to use the order in other places, we only need to instantiate an Order object instead of a Cart object. Even if we need an order in our shopping cart, we can just instantiate an Order object and use the methods in it.

In this way, even if the order function needs to be modified in the future, it will not affect the basic functions of the shopping cart. Such code is also very convenient to maintain in the future. And the readability is also very high.

In a nutshell, the single responsibility principle actually means don't interfere in other people's business and just do what you are supposed to do.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

The road to learning sorting algorithms - heap sort

Publish Date:2025/03/19 Views:145 Category:ALGORITHM

Like other sorting algorithms, let's first look at the definition of heap sort. Heapsort : refers to a sorting algorithm designed using the heap data structure. A heap is a structure that approximates a complete binary tree and satisfies th

Common sorting algorithms

Publish Date:2025/03/19 Views:66 Category:ALGORITHM

This article introduces several commonly used sorting algorithms in software engineering. The core codes of all sorting algorithms are introduced in "Core Codes of Common Sorting Algorithms" 1. Insertion Sort The basic idea of ​​inserti

The road to learning sorting algorithms - selection sort

Publish Date:2025/03/19 Views:160 Category:ALGORITHM

Selection sort is a simple and intuitive sorting algorithm. Its basic idea is to select a maximum (or minimum) element in an unsorted sequence and put it at the end (note: this is the end of the unsorted sequence, which can be considered as

The road to learning sorting algorithms - bubble sort

Publish Date:2025/03/19 Views:80 Category:ALGORITHM

Bubble sort is also a simple and intuitive sorting algorithm. The idea is that it repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The work of visiting the sequence

Core code of commonly used sorting algorithms

Publish Date:2025/03/19 Views:122 Category:ALGORITHM

In the article "Common Sorting Algorithms", we briefly introduced the ideas and implementation steps of various sorting algorithms. In this article, I will share the core codes of these sorting algorithms with you. The complete code of all

Learning the Sorting Algorithm - Radix Sorting (LSD)

Publish Date:2025/03/19 Views:177 Category:ALGORITHM

Radix Sort : It is a non-comparative integer sorting algorithm. The basic principle of radix sorting is to group integers according to the number of digits in the integer. In the grouping process, the missing digits are filled with 0. Accor

The road to learning sorting algorithms - Radix Sort (MSD)

Publish Date:2025/03/19 Views:140 Category:ALGORITHM

In the article "Radix Sort (LSD)" , we explained the concept and efficiency analysis of radix sort. We will not repeat it in this article. You can refer to that article to get a general understanding of the idea of ​​radix sort. Next, w

PHP finds the common prefix of multiple strings [Case]

Publish Date:2025/03/19 Views:94 Category:ALGORITHM

This article shares with you the application of a small algorithm - finding the common prefix of a string array: array ( 'abcdefg' , 'abcdfio' , 'abcdqle' ) In the above array string, the common prefix is ​​'abcd'. The first thing that

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial