120 lines
4.8 KiB
Markdown
120 lines
4.8 KiB
Markdown
---
|
|
slug: first-blog-post
|
|
title: First Blog Post
|
|
authors: [slorber, yangshun]
|
|
tags: [hola, docusaurus]
|
|
---
|
|
|
|
<!-- truncate -->
|
|
import CodeBlock from '@site/src/components/CodeBloack';
|
|
|
|
<br/>
|
|
<span className="main-head"> SOLID: Unveiling the Initial 5 Principles of Object-Oriented Design </span>
|
|
|
|
<div className="text">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. <br/><br/>
|
|
<div style={{ marginTop: '-16px'}}> 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.</div> <br/>
|
|
<span style={{ marginTop: '-16px' , fontSize: '24px' , fontFamily: 'Open Sans' , fontWeight: '700' }}>Now, let's break down SOLID: </span> <br/> <br/>
|
|
<span className="custom-text">S - Single-responsibility Principle </span> <br/>
|
|
This principle encourages each part of your code to have just one job. It makes things simpler to understand and maintain.<br/> <br/>
|
|
<span className="custom-text">O - Open-closed Principle </span> <br/>
|
|
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. <br/> <br/>
|
|
<span className="custom-text">L - Liskov Substitution Principle</span> <br/>
|
|
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. <br/> <br/>
|
|
<span className="custom-text">I - Interface Segregation Principle</span> <br/>
|
|
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. <br/> <br/>
|
|
<span className="custom-text">D - Dependency Inversion Principle</span> <br/>
|
|
This principle emphasizes relying on abstractions rather than concretions. It helps in creating flexible and scalable systems. <br/> <br/>
|
|
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 </div> <br/>
|
|
<br/>
|
|
<span className="head" >Single-responsibility Principle: </span>
|
|
<br/>
|
|
|
|
<CodeBlock code={`class Painter:
|
|
def color_shape(self, shape):
|
|
# Code for coloring the shape
|
|
pass`} />
|
|
|
|
<!-- class Painter:
|
|
def color_shape(self, shape):
|
|
# Code for coloring the shape
|
|
pass -->
|
|
|
|
|
|
<br/>
|
|
<span className="head" >Open-closed Principle:</span>
|
|
|
|
|
|
<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 `} /> <br/>
|
|
|
|
|
|
|
|
<span className="head" >Liskov Substitution Principle: </span>
|
|
|
|
<CodeBlock code={`class ResizableCircle(Circle):
|
|
def resize(self, factor):
|
|
# Code for resizing the circle
|
|
pass `} /> <br/>
|
|
|
|
|
|
|
|
<span className="head" >Interface Segregation Principle:</span>
|
|
|
|
<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 `} /> <br/>
|
|
|
|
|
|
<span className="head" >Dependency Inversion Principle:</span>
|
|
|
|
<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() `} />
|
|
<br/>
|
|
<span className="con">Conclusion - </span>
|
|
|
|
<div className="text">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! </div>
|
|
|