cicid/blog/2019-05-29-first-blog-post.md
2024-11-05 16:27:23 +05:30

4.8 KiB

slug title authors tags
SOLID Unveiling the Initial 5 Principles of Object-Oriented Design SOLID Unveiling the Initial 5 Principles of Object-Oriented Design
slorber
hola
docusaurus

import CodeBlock from '@site/src/components/CodeBloack';


Welcome to the world of SOLID, a set of five fundamental principles in Object-Oriented Design (OOD) coined by Robert C. Martin, lovingly known as Uncle Bob.

Before we dive in, it's important to know that while these principles are applicable across different programming languages, our examples here will be using PHP.

Now, let's break down SOLID:

S - Single-responsibility Principle
This principle encourages each part of your code to have just one job. It makes things simpler to understand and maintain.

O - Open-closed Principle
The open-closed principle suggests that your code should be open for extension but closed for modification. In simpler terms, you can add new features without changing existing code.

L - Liskov Substitution Principle
This principle deals with using derived classes that can be substituted for their base classes without affecting the program's functionality. It promotes consistency and reliability.

I - Interface Segregation Principle
The interface segregation principle advises breaking down large interfaces into smaller, more specific ones. This makes it easier for classes to implement only what they need.

D - Dependency Inversion Principle
This principle emphasizes relying on abstractions rather than concretions. It helps in creating flexible and scalable systems.

In the upcoming sections, we'll explore each of these principles individually. By understanding and applying SOLID, you'll not only become a better developer but also enhance your ability to create software that's easy to maintain and expand as your project grows


Single-responsibility Principle:

<CodeBlock code={class Painter: def color_shape(self, shape): # Code for coloring the shape pass} />


Open-closed Principle:

<CodeBlock code={class Painter: def color_shape(self, shape): # Code for coloring the shape class ShapeDrawer: def draw_shapes(self, shapes): for shape in shapes: shape.draw() pass} />

<CodeBlock code={class Painter: def color_shape(self, shape): # Code for coloring the shape class ShapeDrawer: def draw_shapes(self, shapes): for shape in shapes: shape.draw() pass} />

<CodeBlock code={class Square: def draw(self): # Code for drawing a square pass} />

<CodeBlock code={class Circle: def draw(self): # Code for drawing a circle pass } />

Liskov Substitution Principle:

<CodeBlock code={class ResizableCircle(Circle): def resize(self, factor): # Code for resizing the circle pass } />

Interface Segregation Principle:

<CodeBlock code={class TextPrintable: def print_text(self): # Code for printing text pass} />

<CodeBlock code={class ImagePrintable: def print_image(self): # Code for printing an image pass } />

Dependency Inversion Principle:

<CodeBlock code={class APIConnector: def connect(self): # Code for connecting to the API pass } />

<CodeBlock code={`class App: def init(self, api_connector): self.api_connector = api_connector

def perform_operation(self):
    # Code using the API connector without knowing its specific implementation
    self.api_connector.connect() `} /> 

Conclusion -
SOLID principles are like a set of guidelines that help you write code that's easy to understand, modify, and expand. They encourage you to create classes with one clear purpose, add new features without breaking existing code, ensure consistency in your code, focus on what each class needs to do, and rely on abstractions for flexibility. By applying these principles, you'll be on your way to becoming a better and more efficient coder. Happy coding!