| About Us | Contact Us | Privacy Policy | Join Us+ |

WorldTrueSight

Get the latest news and reviews about technology!

19 Sep 2019

SOLID Design Principles with Designs and Coding Examples

S = Single Responsibility Principle
O = Open Close Principle
L = Liskov Substitution Principle
I = Interface Segregation Principle
D = Dependency inversion Principle

SINGLE RESPONSIBILITY PRINCIPLE (SRP) :

The single responsibility principle (SRP) states that every class or module in a program should have responsibility for just a single piece of that program's functionality.

Example:

BAD DESIGN :

SOLID Design Principles with Designs and Coding Examples

BAD CODE:

SOLID Design Principles with Designs and Coding Examples

GOOD DESIGN :

SOLID Design Principles with Designs and Coding Examples

GOOD CODE :

SOLID Design Principles with Designs and Coding Examples

OPEN CLOSE PRINCIPLE (OCP):

In object-oriented programming, the open/closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"; that is, such an entity can allow its behavior to be extended without modifying its source code.

BAD DESIGN :

SOLID Design Principles with Designs and Coding Examples

BAD CODE :

SOLID Design Principles with Designs and Coding Examples

GOOD DESIGN:

SOLID Design Principles with Designs and Coding Examples

GOOD CODE:

SOLID Design Principles with Designs and Coding Examples

LISKOV SUBSTITUTION PRINCIPLE (LSP):

The Liskov Substitution Principle (LSP) functions that use pointers to base classes must be able to use objects of derived classes without knowing it. The Liskov Substitution Principle is a way of ensuring that inheritance is used correctly.

BAD DESIGN:

SOLID Design Principles with Designs and Coding Examples

BAD CODE:

SOLID Design Principles with Designs and Coding Examples

GOOD DESIGN:

SOLID Design Principles with Designs and Coding Examples

GOOD CODE:

SOLID Design Principles with Designs and Coding Examples

INTERFACE SEGREGATION PRINCIPLE(ISP):

Interface segregation principle. In the field of software engineering, the interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use.

BAD DESIGN:

SOLID Design Principles with Designs and Coding Examples

BAD CODE:

SOLID Design Principles with Designs and Coding Examples

GOOD DESIGN:

SOLID Design Principles with Designs and Coding Examples

GOOD CODE:

DEPENDENCY INVERSION PRINCIPLE(DIP):

The general idea of this principle is as simple as it is important: High-level modules, which provide complex logic, should be easily reusable and unaffected by changes in low-level modules, which provide utility features. To achieve that, you need to introduce an abstraction that decouples the high-level and low-level modules from each other.

BAD DESIGN:

SOLID Design Principles with Designs and Coding Examples
BAD CODE:
SOLID Design Principles with Designs and Coding Examples

GOOD DESIGN:

SOLID Design Principles with Designs and Coding Examples

GOOD CODE:

SOLID Design Principles with Designs and Coding Examples

No comments:

Post a Comment