Lädt...


🔧 Understanding SOLID Principles with C# (short intro)


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The SOLID principles are a set of design guidelines in object-oriented programming aimed at creating robust, maintainable, and scalable software. Coined by Robert C. Martin (Uncle Bob), these principles help developers avoid common pitfalls and write cleaner code.

S: Single Responsibility Principle (SRP)

Definition: A class should have only one reason to change.

Explanation: Each class should focus on a single responsibility or functionality. This makes code easier to understand, test, and maintain.

Example:

Instead of creating a User class that handles data storage and email notifications, split it into User for managing user data and EmailService for handling email notifications.

public class UserService 
{ 
    public void AddUser(string name) 
    { 
       // Implement logic 
    } 
}

public class EmailService 
{ 
    public void SendEmail(string email) 
    { 
        // Implement logic 
    } 
}

O: Open/Closed Principle (OCP)

Definition: Classes should be open for extension but closed for modification.

Explanation: Code should allow new functionality to be added without altering existing code. This promotes stability and reduces the risk of bugs.

Example:

Instead of modifying a class to add a new type of payment method, use interfaces or inheritance to extend functionality.

public interface IPayment 
{ 
    void Pay(); 
}

public class CardPayment : IPayment 
{ 
    public void Pay() 
    { 
        // Card payment 
    } 
}

public class PaymentProcessor 
{ 
    public void Process(IPayment payment) 
    { 
        payment.Pay(); 
    } 
}

L: Liskov Substitution Principle (LSP)

Definition: Subtypes must be substitutable for their base types without altering the correctness of the program.

Explanation: If a class B is a subclass of class A, then objects of type A should be replaceable with objects of type B without breaking the application.

Example:

If Rectangle is a superclass and Square inherits from it, ensure the subclass doesn’t alter behavior, such as how dimensions are set.

public interface IShape 
{ 
    int Area(); 
}

public class Rectangle : IShape 
{ 
    public int Area() => Width * Height; 
}

public class Square : IShape 
{ 
    public int Area() => Side * Side; 
}

I: Interface Segregation Principle (ISP)

Definition: A class should not be forced to implement interfaces it does not use.

Explanation: Split large interfaces into smaller, more specific ones. This ensures classes implement only what they need.

Example:

Instead of one large Animal interface with methods like Fly(), Swim(), and Run(), create specific interfaces like Flyable, Swimmable, and Runnable.

public interface IFlyable 
{ 
    void Fly(); 
}

public interface IWalkable 
{ 
    void Walk(); 
}

public class Bird : IFlyable, IWalkable 
{ 
    public void Fly() 
    { 
        // Flying logic 
    }

    public void Walk() 
    { 
        // Walking logic 
    } 
}

D: Dependency Inversion Principle (DIP)

Definition: High-level modules should not depend on low-level modules; both should depend on abstractions.

Explanation: Rely on abstractions rather than concrete implementations to reduce coupling between components.

Example:

Instead of hardcoding a SQLDatabase in a class, use an interface like IDatabase, allowing easy switching to other databases (e.g., NoSQL).

public interface IDatabase 
{ 
    void Save(string data); 
}

public class SQLDatabase : IDatabase 
{ 
    public void Save(string data) 
    { 
        // Save to SQL 
    } 
}

public class DataManager 
{ 
    public DataManager(IDatabase db) 
    { 
        db.Save("data"); 
    } 
}

Why Use SOLID?

  1. Better Maintainability: Easier to understand and modify code.
  2. Scalability: Adapts well to new features or requirements.
  3. Reduced Bugs: Minimizes side effects when changes are made.
  4. Reusability: Code can be reused across projects.
...

🔧 Understanding SOLID Principles with C# (short intro)


📈 53.53 Punkte
🔧 Programmierung

🔧 SOLID Principles Aren't Principles


📈 35.43 Punkte
🔧 Programmierung

🔧 SOLID Principles / Open - closed principles -


📈 35.43 Punkte
🔧 Programmierung

🔧 SOLID Principles: They're Rock-Solid for Good Reason!


📈 35 Punkte
🔧 Programmierung

🔧 Understanding SOLID Principles: A Guide for Junior Developers


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding the Core of SOLID Principles in Object-Oriented Programming


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding SOLID design principles with easy coding examples


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding SOLID Principles with Python Examples


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding SOLID Principles and Their Implementation in React


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding the SOLID Principles in Programming


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding SOLID Principles in PHP


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding SOLID Principles in Software Design


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding SOLID principles


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding SOLID Principles in C#


📈 29.87 Punkte
🔧 Programmierung

🔧 Understanding SOLID Principles: A Beginner's Guide


📈 29.87 Punkte
🔧 Programmierung

🔧 Improve your understanding of SOLID principles in OOPS.


📈 29.87 Punkte
🔧 Programmierung

🔧 Introduction to the Developer's Intro to Data Science Video Series (1 of 28) | Dev Intro to Data Science


📈 24.53 Punkte
🔧 Programmierung

🔧 How do programming principles equate to life's principles?


📈 23.91 Punkte
🔧 Programmierung

📰 Data Protection Principles: The 7 Principles of GDPR Explained


📈 23.91 Punkte
📰 IT Security Nachrichten

📰 Data Protection Principles: The 7 Principles of GDPR Explained


📈 23.91 Punkte
📰 IT Security Nachrichten

🔧 📦 "OaaS" : A short intro to "Operations as a Service" & its tremendous benefits


📈 23.66 Punkte
🔧 Programmierung

📰 ChatGPT Outperforms Undergrads In Intro-Level Courses, Falls Short Later


📈 23.66 Punkte
📰 IT Security Nachrichten

🔧 A Short Intro to Prompt Engineering And Generative AI


📈 23.66 Punkte
🔧 Programmierung

🔧 A Short Intro to Prompt Engineering And Generative AI


📈 23.66 Punkte
🔧 Programmierung

🔧 A Short Intro to Prompt Engineering And Generative AI


📈 23.66 Punkte
🔧 Programmierung

🔧 Environment Variables: a very short intro for JS development


📈 23.66 Punkte
🔧 Programmierung

🔧 The Foundation of Good Software Engineering: Mastering SOLID Principles


📈 23.48 Punkte
🔧 Programmierung

🔧 SOLID Principles


📈 23.48 Punkte
🔧 Programmierung

📰 Scale your Machine Learning Projects with SOLID principles


📈 23.48 Punkte
🔧 AI Nachrichten

🔧 Applying SOLID Principles in JavaScript and TypeScript Framework


📈 23.48 Punkte
🔧 Programmierung

🔧 Mastering SOLID Principles in Java: A Practical Guide


📈 23.48 Punkte
🔧 Programmierung

🔧 SOLID principles for JavaScript


📈 23.48 Punkte
🔧 Programmierung

🔧 Mastering SOLID Principles in React: Elevating Your Code Quality


📈 23.48 Punkte
🔧 Programmierung

matomo