#23 - Python SOLID Principle

#23 - Python SOLID Principle

By Ifeanyi Omeata


Topics:


1. SOLID Principle


1. SOLID Principle


>>Return to Menu

The SOLID Principles are:

  • S = Single Responsibility Principle (SRP)

  • O = Open Closed Principle (OCP)

  • L =  Liskov Substitution Principle (LSP)

  • I = Interface Segregation Principle (ISP)

  • D = Dependency Inversion Principle (DIP) 

  • S = Single Responsibility Principle (SRP) The Single Responsibility Principle (SRP) states that a function or a class should have one responsibility. It states that every class, method, and function should have only one job or one reason to change. The purposes of the single responsibility principle are to:

  • Create high cohesive and robust classes, methods, and functions.
  • Promote class composition
  • Avoid code duplication
  • Following this principle will help keep your functions small and manageable.

  • O = Open Closed Principle (OCP) The open/closed principle (OCP) states that a module should be both open and closed (but with respect to different aspects). When designing a class, for instance, we should carefully encapsulate the logic so that it has good maintenance, meaning that we will want it to be open to extension but closed for modification.

  • It states that a class, method, and function should be open for extension but closed for modification. The purpose of the open-closed principle is to make it easy to add new features (or use cases) to the system without directly modifying the existing code.

  • L =  Liskov Substitution Principle (LSP) The Liskov substitution principle states that a child class must be substitutable for its parent class. Liskov substitution principle aims to ensure that the child class can assume the place of its parent class without causing any errors.

  • I = Interface Segregation Principle (ISP) The interface segregation principle advises that the interfaces should be small in terms of cohesions. It states that an interface should be as small as possible in terms of cohesion. In other words, it should do ONE thing. Make fine grained interfaces that are client-specific. Clients should not be forced to implement interfaces they do not use.

  • For example, the Database interface can have the connect() and disconnect() methods because they must go together. If the Database interface doesn’t use both methods, it’ll be incomplete.
  • An interface is a description of behaviours that an object can do. In object-oriented programming, an interface is a set of methods an object must-have. The purpose of interfaces is to allow clients to request the correct methods of an object via its interface.
  • Python uses abstract classes as interfaces because it follows the so-called duck typing principle. - The duck typing principle states that “if it walks like a duck and quacks like a duck, it must be a duck.” In other words, the methods of a class determine what its objects will be, not the type of the class.

  • D = Dependency Inversion Principle (DIP)  The dependency inversion principle states that: High-level modules should not depend on the low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions. The dependency inversion principle aims to reduce the coupling between classes by creating an abstraction layer between them.

#End


Hope you enjoyed this! :) Follow me for more contents...


Get in Touch:
ifeanyiomeata.com

Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2022