Friday, April 8, 2016

Architecture fundamentals -SOLID


· BDUF: Big Design, Up Front

Big Design Up Front is an approach in software development that software design should be perfect and completed before implementation started.

· Separation of Concerns: Defined as logical grouping of responsibilities.

It is one of the design principle. Divide the application into different section. Every section should refer to different concern.

· SRP: Single Responsibility Principle, to reduce coupling

Single Responsibility principle states that every class should have only one reason to change. It means every class should have single responsibility.

· DRYP: Don’t repeat yourself principle.

DRDYP states that “every piece of information should be unique and unambiguous. You can implement the DRYP by breaking the code into functions and call them instead of copy paste the code more than one place.

· Open Closed principle.

A class should be open for extension but closed for modification.

· Liskov Substitution principle.

A parent should be able to replace its child. Means if a class A inherited an interface which contains 3 method but one method no use for class A than better is break this interface into two interfaces.
Example :

We have an interface

Public Interface IDevice{
Void Open();
Void Read();
Void Close();
}
and two classes


public class PCIDevice:IDevice {
   public void Open(){
   // Device specific opening logic
   }
   public void Read(){
   // Reading logic specific to this device
   }
   public void Close(){
   // Device specific closing logic.
   }


}

public class NetWorkDevice:IDevice{

 public void Open(){
 // Device specific opening logic
 }
 public void Read(){
 // Reading logic specific to this device
 }
 public void Close(){
 // Device specific closing logic.
 }


}

We need one more class which has Refresh Method.

public class USBDevice:IDevice{
 public void Open(){
    // Device specific opening logic
 }
 public void Read(){
    // Reading logic specific to this device

 }
public void Close(){
   // Device specific closing logic.
 }
 public void Refresh(){
   // specific only to USB interface Device
 }
}

Here is when Liskov Substitution comes in light in above case we have to  write this code

Public void Acquire(){

USBDevice obj = new USBDevice ();

obj.Refresh();

//Here we can not replace USBDevice with IDevice it is the violation of Liskov. So solution is Refresh method should be declare in IDevice.
}

}



· Interface Segregation

A client should not be force to use an interface if it does not need it.

When you solve the above problem of Liskov by defining Refresh method in interface IDevice another problem arises that is IDevice will force other classes to implement Refresh method even if they have not required. Than Interface Segregation says that split that interface in two interface where one interface would contain Refresh method and other interface would contain other three method.

· Dependency Inversion Principle.

High Level Module should not depend on Low level module. Both should depend on abstraction.

No comments:

Followers

Link