65.9K
CodeProject is changing. Read more.
Home

Refactoring Tips - Tip 5

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.75/5 (8 votes)

Mar 1, 2010

CPOL
viewsIcon

7370

Refactoring Tips - Tip 5Tip 5Always Programme for interface/abstract than concrete classes, Which will gives the option for extension, loose coupling and plugin.This will also be in line with one of the design principle - LSP(Liskov's substitution Principle) - in simple word it...

Refactoring Tips - Tip 5 Tip 5 Always Programme for interface/abstract than concrete classes, Which will gives the option for extension, loose coupling and plugin. This will also be in line with one of the design principle - LSP(Liskov's substitution Principle) - in simple word it summarises that, Every subclass should be substitutable for a base class. For eg: if i have to write a data persistent class. Bad practice
 public class SqlRepository
 {
  void Crete(parameters)
  {
    //Logic to get the data...
  }
  void Delete(id)
  {
   //Logic to delete the data based on the id
  }
  //Some more specific methods 
 }
Good practice
 public interface IRepository
 {
   void Create(object);
   void Delete(id);
   //Some more methods...
 }

 public class SqlRepository :IRepository
 {
   void Crete(parameters)
   {
    //Logic to get the data...
   }
   void Delete(id)
  {
   //Logic to delete the data based on the id
  }

  //Some more specific methods

 }
This arrangement gives us the flexibility that tomorrow, if i want to write a repostory for some other store then also i can write without much impact with the exisiting clients. I hope this helps!. Regards, -Vinayak