Introduction:
Welcome to my article where we’ll explore a super important concept in programming called the Single Responsibility Principle (SRP). Single Responsibility Principle (SRP)! It’s a key concept in programming that helps keep our code neat and easy to understand. So, let’s dive in and make sure you’ve got it all figured out!
Robert C. Martin describes it:
A class should have one and only one reason to change, meaning that a class should have only one job.
What is the Single Responsibility Principle?
So, what’s this SRP thing all about? Well, think of it like this: imagine you have a bunch of toys in your room. Each toy should have its own job, right? Like, a ball is for bouncing, and a teddy bear is for cuddling. In programming, the SRP says that each part of your code should have just one job to do. It’s like keeping your toys organized so you can find them easily when you need them!
SRP in Action: A Practical Example
Now, let’s dive into a real-life example to see how SRP works in action. We’ll look at a piece of code that’s not following the SRP and compare it to a version that does. Trust me, you’ll see a big difference!
class Product {
private string $name;
private string $color;
private int $price;
public function __construct(
$name,
$color,
$price
) {
$this->name = $name;
$this->color = $color;
$this->price = $price;
}
public function getPrice() {
return $this->price;
}
public function getProductDetails(): void {
printf(
"Product name: %s,\nColor: %s,\nPrice: %d",
$this->name,
$this->color,
$this->price
);
}
}
class Invoice {
private Product $product;
private int $quantity;
public function __construct(Product $product, int $quantity) {
$this->product = $product;
$this->quantity = $quantity;
}
public function calculateTotal(): int {
return $this->product->getPrice() * $this->quantity;
}
public function invoicePrint(): void {
echo "Invoice print.";
}
public function saveToDB(): void {
echo "Invoice saved in DB.";
}
}
Here we have a simple PHP code snippet with two classes: Product and Invoice.
The Product class holds details about a product like its name, color, and price. Pretty straightforward, right? It's like having a label on a box that tells you exactly what's inside.
Now, let’s talk about the Invoice class. Its job is to handle invoices, but here's the catch: it's doing more than it should! According to the Single Responsibility Principle (SRP) by Robert C. Martin, each class should have just one reason to change. But in our case, the Invoice class is handling three things: calculating the total, printing the invoice, and saving invoice data to the database.
It’s like having one person trying to do three different jobs at once — it’s bound to get messy! To follow SRP, we’d want to split up these responsibilities into separate classes. That way, each class can focus on doing one thing really well, making our code easier to understand and maintain.
By refactoring our code to follow SRP, we’ll end up with classes that are simpler, more flexible, and easier to work with. And that’s a win-win for everyone involved! 🚀💻
Now Single Responsibility Principle comes in action:
class Product {
private string $name;
private string $color;
private int $price;
public function __construct(
$name,
$color,
$price
) {
$this->name = $name;
$this->color = $color;
$this->price = $price;
}
public function getPrice() {
return $this->price;
}
public function getProductDetails(): void {
printf(
"Product name: %s,\nColor: %s,\nPrice: %d",
$this->name,
$this->color,
$this->price
);
}
}
class Invoice {
private Product $product;
private int $quantity;
public function __construct(Product $product, int $quantity) {
$this->product = $product;
$this->quantity = $quantity;
}
public function calculateTotal(): int {
return $this->product->getPrice() * $this->quantity;
}
}
class InvoicePrinter {
private Invoice $invoice;
public function __construct(Invoice $invoice) {
$this->invoice = $invoice;
}
public function print(): void {
echo "Invoice print";
}
}
class InvoiceDB {
private Invoice $invoice;
public function __construct(Invoice $invoice) {
$this->invoice = $invoice;
}
public function save(): void {
echo "Invoice saved in DB.";
}
}
In this updated code snippet, we’ve successfully applied the Single Responsibility Principle (SRP), keeping our classes focused and organized.
The Product class is responsible for holding product details such as name, color, and price. It's like a neat little package that tells us everything we need to know about a product.
Then, we have the Invoice class, which now has a single responsibility: calculating the total amount based on the product price and quantity. It's like a dedicated accountant crunching the numbers to give us the final bill.
But wait, there’s more! We’ve introduced two new classes: InvoicePrinter and InvoiceDB. These classes each handle a specific task related to invoices. The InvoicePrinter class is responsible for printing invoices, while the InvoiceDB class takes care of saving invoice data to the database.
By breaking down responsibilities into separate classes, we’ve made our code cleaner, easier to understand, and more flexible. Now, each class can focus on its own job without getting tangled up in unrelated tasks. It’s like having a well-oiled machine where each part does its job smoothly and efficiently.
With our code structured this way, future changes and updates will be a breeze. And that’s what following SRP is all about — making our codebase more robust and maintainable for the long haul. 🌟💻
Conclusion:
In conclusion, by following the Single Responsibility Principle (SRP), we’ve made our code easier to work with and understand, kind of like organizing your toys into different boxes so you can find them easily when you need them!
Breaking down responsibilities into smaller, focused tasks has made our codebase more flexible and adaptable, just like having a toolbox with specific tools for different jobs.
Whether you’re a beginner just starting out or an advanced developer, SRP helps keep our code neat and tidy, making it easier for everyone to collaborate and contribute.
So, remember, when in doubt, keep it simple and focused — your future self (and your teammates) will thank you for it! Happy coding! 🚀💻
Thank you for your precious time to read :)
Top comments (0)