Pages

Friday, June 24, 2016

How Implement Professional Class in Object Oriented Programming (OOP)



hi everyone 

in this post i will explain SOLID Principles that enable software architect to build good designed class in object oriented programming .


SOLID

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


Single Responsibility Principle :-

this principle mean that every class must have one and only one responsibility mean only one task to do and must there is separation of concerns 

for example
class can not do validation of inputs and save inputs in database and xml files then log this . all this is in the same class and same function this not valid design in class must there is separation of concerns 
  
Open/Close Principle :-
this mean class must be open  to extend its functionality but without hard modification in class 

for example 
if you want to make method open to extend but close to modification 
you cant make method virtual method with default implementation and if you want to extend or override this implementation you can do this by override like ToString() method in c# is virtual method and you can override its implementation without hard modification the default code

Liskov Substitution Principle:-
this mean if class B is sub class from A class
you can substitute and replace child class with base class to add generalization in your code

Interface Segregation Principle:-
this mean that must there is segregation and separation between structure of class and implementation 
and to do that us Interface or data contract that used to define structure of class and power point of its functionality such as name of method and its inputs and its output without any implementation 
and when class want to implement interface can write your logic code without problems and this is add abstraction level and generalization in code
  
Dependency Injection Principle:-
this mean that if class depend on another external class or logic 
must not make this hard in class because this add problems if you want to change dependencies later and difficult to test and maintain 
you must inject your dependencies as input to class and if you want to change dependencies objects just change input parameter 

for example 

if you design laptop class and this class depend on another electric source class to run laptop 
instead of make this hard implemented in laptop class and set for  electric source object in class 
you can inject this object and send as parameter to class 
this useful in if you want to change electric source object by another power source object like electric from gas engine or from solar power just inject another object without change any another thing

No comments:

Post a Comment