Object-oriented design principle - Single Responsibility Principle
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.
Related Articles
Learning the Sorting Algorithm - Insertion Sort (Concepts)
Publish Date:2025/03/19 Views:96 Category:ALGORITHM
-
What is "insertion sort"? The concept is as follows: each time a record to be sorted is inserted into the previously sorted sequence according to its key size, until all records are inserted. Concepts are always somewhat abstract, and can a
Learning path of sorting algorithm - direct insertion sort
Publish Date:2025/03/19 Views:176 Category:ALGORITHM
-
This article follows up on Insertion Sort (Concepts) and presents the implementation steps and code for direct insertion sort. Since the Concepts section already has a large number of illustrations, it would be a bit long-winded to provide
Learning the sorting algorithm - Binary Insertion Sort
Publish Date:2025/03/19 Views:143 Category:ALGORITHM
-
This article follows the insertion sort (concept article) and presents the implementation steps and implementation code of the binary insertion sort Binary Insertion Sort Algorithm Steps Treat the first element of the first sequence to be s
The road to learning sorting algorithms - table insertion sort
Publish Date:2025/03/19 Views:194 Category:ALGORITHM
-
Table insertion sort was briefly mentioned in Insertion sort (concept) . I briefly summarized it and wrote this article. You can refer to it if you need it. Table insertion sort, as the name implies, uses an index table to sort the original
The road to learning sorting algorithms - Hill sort
Publish Date:2025/03/19 Views:52 Category:ALGORITHM
-
Hill sort is named after the designer of the algorithm, Hill. It is an improvement of Hill on the basis of insertion sort and can be said to be a special insertion sort. Here are the properties of insertion sort: First of all, the insertion
Things about the singleton design pattern
Publish Date:2025/03/19 Views:53 Category:ALGORITHM
-
The singleton design pattern is one of the most commonly used design patterns. The singleton design pattern, just by its name, you can roughly know its meaning. Single means one; instance means instance object. So a singleton has only one i
The road to learning sorting algorithms - merge sort
Publish Date:2025/03/19 Views:158 Category:ALGORITHM
-
Let's first look at the definition of merge sort Merge sort is an effective sorting algorithm based on the merge operation. This algorithm is a very typical application of the Divide and Conquer method. Merge the ordered subsequences to obt
The road to learning sorting algorithms - merge sort (non-recursive implementatio
Publish Date:2025/03/19 Views:189 Category:ALGORITHM
-
In the article "Merge Sort", we introduced the principles and operation steps of merge sort, and finally implemented the sorting algorithm using PHP code. In the program, we used the principle of recursion to implement the algorithm. In fac
The road to learning sorting algorithms - quick sort
Publish Date:2025/03/19 Views:122 Category:ALGORITHM
-
Quick sort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. In the worst case, O(n2) comparisons are required, but this is uncommon. In fact, quick sort is often significantly faste