Please provide the basics of SOLID principles. If any example is provided please use C++.
- User
Please provide the basics of SOLID principles. If any example is provided please use C++.
- Assistant
Certainly! The SOLID principles are a set of five design principles meant to make software designs more understandable, flexible, and maintainable. Let's break down each principle along with a simple example in C++.
-
Single Responsibility Principle (SRP)
Definition: A class should have only one reason to change, meaning it should have only one job or responsibility.
Example:
// Before applying SRP class Report { public: std::string getData() { return "Report Data"; } void formatHtml(std::string data) { std::cout << "<html><body>" << data << "</body></html>" << std::endl; } void formatJson(std::string data) { std::cout << "{ \"data\": \"" << data << "\" }" << std::endl; } }; // After applying SRP class Report { public: std::string getData() { return "Report Data"; } }; class HtmlFormatter { public: void format(std::string data) { std::cout << "<html><body>" << data << "</body></html>" << std::endl; } }; class JsonFormatter { public: void format(std::string data) { std::cout << "{ \"data\": \"" << data << "\" }" << std::endl; } }; -
Open/Closed Principle (OCP)
Definition: Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
Example:
// Base class class Shape { public: virtual double getArea() const = 0; }; // Square class extends Shape class Square : public Shape { private: double side; public: explicit Square(double s) : side(s) {} double getArea() const override { return side * side; } }; // Circle class extends Shape class Circle : public Shape { private: double radius; public: explicit Circle(double r) : radius(r) {} double getArea() const override { return 3.14159 * radius * radius; } }; -
Liskov Substitution Principle (LSP)
Definition: Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.
Example:
class Rectangle { protected: int width, height; public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } int getWidth() const { return width; } int getHeight() const { return height; } virtual int getArea() const { return width * height; } }; class Square : public Rectangle { public: void setWidth(int w) override { width = w; height = w; } void setHeight(int h) override { height = h; width = h; } }; void printArea(const Rectangle& r) { std::cout << "Area: " << r.getArea() << std::endl; } -
Interface Segregation Principle (ISP)
Definition: A client should not be forced to implement an interface that it doesn't use.
Example:
// Before applying ISP class Worker { public: virtual void work() = 0; virtual void eat() = 0; }; // After applying ISP class Workable { public: virtual void work() = 0; }; class Eatable { public: virtual void eat() = 0; }; class Human : public Workable, public Eatable { public: void work() override { std::cout << "Human working" << std::endl; } void eat() override { std::cout << "Human eating" << std::endl; } }; class Robot : public Workable { public: void work() override { std::cout << "Robot working" << std::endl; } }; -
Dependency Inversion Principle (DIP)
Definition: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
Example:
// Before applying DIP class Light { public: void turnOn() { std::cout << "Light turned on" << std::endl; } void turnOff() { std::cout << "Light turned off" << std::endl; } }; class Switch { private: Light* light; public: Switch(Light* l) : light(l) {} void operate() { light->turnOn(); } }; // After applying DIP class Switchable { public: virtual void turnOn() = 0; virtual void turnOff() = 0; virtual ~Switchable() = default; }; class Light : public Switchable { public: void turnOn() override { std::cout << "Light turned on" << std::endl; } void turnOff() override { std::cout << "Light turned off" << std::endl; } }; class Switch { private: Switchable* device; public: Switch(Switchable* d) : device(d) {} void operate() { device->turnOn(); } };
These examples illustrate the core ideas behind the SOLID principles in the context of C++ programming. By following these principles, you can create more robust, maintainable, and flexible code.
-