There is 5 principles widely followed in OOP or Object-oriented programming, this was proposed by Robert C. Martin also known as Uncle Bob.
Single-responsibility principle
A class should have only a single or one function. It means that your method should only contains a specific task not multiple task inside that method.
Open-closed principle
Any class should be open for extension but closed for modification. It means that the class should easily extends not modify the class for new logic.
Liskov substitution principle
FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE
CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES
WITHOUT KNOWING IT.
The above is a paraphrase of the Liskov Substitution Principle (LSP). Barbara Liskov.
This principle means that every subclass or derived class should be an alternative or representation for their parent class. You must create a Base class that closed for modification and create a descendant class to extends the function of its base class, so you must have an abstraction for the base class. If you follow Liskov substition principle, your class hierarchy will follow Open-closed P implicitly.
Interface segregation principle
Segregate your Interface, if you have Interface1 that your client is currently using, then don't add any other function or change that, create an Interface2 if you want to add or modify a methods. Do not force your customer or clients to use an interface that they don’t use.
Dependency Inversion principle
Avoid the dependencies of each class to one another. If you have a main function it must be use an abstraction.
Single-responsibility principle
A class should have only a single or one function. It means that your method should only contains a specific task not multiple task inside that method.
Open-closed principle
Any class should be open for extension but closed for modification. It means that the class should easily extends not modify the class for new logic.
Liskov substitution principle
FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE
CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES
WITHOUT KNOWING IT.
The above is a paraphrase of the Liskov Substitution Principle (LSP). Barbara Liskov.
This principle means that every subclass or derived class should be an alternative or representation for their parent class. You must create a Base class that closed for modification and create a descendant class to extends the function of its base class, so you must have an abstraction for the base class. If you follow Liskov substition principle, your class hierarchy will follow Open-closed P implicitly.
Interface segregation principle
Segregate your Interface, if you have Interface1 that your client is currently using, then don't add any other function or change that, create an Interface2 if you want to add or modify a methods. Do not force your customer or clients to use an interface that they don’t use.
Dependency Inversion principle
Avoid the dependencies of each class to one another. If you have a main function it must be use an abstraction.
Comments
Post a Comment