Skip to content

rightek/solid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

SOLID

SOLID principles for kids.

[S]ingle Responsibility Principle

Class must have only one reason to change and only one responsibility.

Entites must not modify database, it's repository's job, do not make Swiss Army knife class

[O]pen Closed Principle

Code must be open for extension and closed for modification.

Consider discount class, if you use switch/case or if/else in order to handle different types of discounts, you violated this principle, because in order to handle new type of discount, you have to add new case/else to the class. you can use interfaces, e.g: IDiscount and add Calculate method to it, then add a class for each type of discounts, in order to handle new type of discount, you just need to add new class for it and there is no need to touch other discount classes.

[L]iskov Substitution Principle

Derived classes must be substitutable for their base classes.

A Square is a Rectangle, so inheriting form Rectangle class to make a Square makes sense, right? okay, can you use Square anywhere you expect a Rectangle? See: this Stackoverflow answer

[I]nterface Segregation Principle

Clients should not be forced to depend upon interfaces that they don't use.

Consider you have an interface called IFile with 2 methods, Read and Write, and now you want to add a class just for reading file, e.g: FileReader, if you use IFile interface for this class, you have to implement Write method too, and that's the problem.

[D]ependency Inversion Principle

High level modules should not depend on low level modules, both should depend on abstractions. Abstractions should not depend on details, details should depend upon abstractions.

High level classes should not work directly with low level classes, they should work with them through interfaces. for example in ASP.NET MVC, ProductController should not work directly with ProductRepository, it should work with IProductRepository

About

SOLID principles for kids.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published