banner
Tenifs

Tenifs

雄关漫道真如铁,而今迈步从头越。
github
follow
zhihu
email

The Six Principles of Object-Oriented Programming

  • Single Responsibility Principle (SRP): A class should have only one reason to change, that is, a class should only be responsible for one task. For example, a Person class should only be responsible for tasks related to Person, and should not handle unrelated tasks.
  • Open/Closed Principle (OCP): Software entities should be open for extension, but closed for modification. For example, define a shape class, and then let different types of shapes inherit from this class without needing to modify the shape class itself.
  • Liskov Substitution Principle (LSP): Subclass objects should be able to replace all parent class objects.
  • Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use, meaning interfaces should be small and specific.
  • Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules, both should depend on abstractions; abstractions should not depend on details, details should depend on abstractions. For example, a company class containing a department class should consider using composition relationships instead of inheritance relationships.
  • Law of Demeter: An object should have minimal knowledge of other objects, and should only interact with its immediate friends.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.