Translate

Chủ Nhật, 4 tháng 12, 2016

Program to interface not implementation

 Created by Thang Vu

In Object-Oriented Programming (OOP), the desired software is not only runnable but also extensible, maintainable and testable. In this post, I will present a principle in OOP: "Program to interface not implementation". Before digging deeper in this principle, i will present basic concepts about concrete classabstractclass and interface.

1. Basic concepts

1.1. Concrete class

Concrete class is a class that can be used to specify any specific entity. For example, class Car:
?
  1. class Car{  
  2.  string color;  
  3.  string model;  
  4. public:  
  5.  string getColor(){  
  6.   return this->color;  
  7.  }  
  8.  void setColor(string color){  
  9.   this->color = color;   
  10.  }   
  11.  string getModel(){  
  12.   return this->model;  
  13.  }  
  14.  void setModel(string model){  
  15.   this->model = model;  
  16.  }  
  17. }  
The class Car has two attributes color and model, so any object instantiated by class Car will be determistic with these attributes.

1.2. Abstract class

Suppose that we add a new method called getTax() to class Car. But this class does not know how to implement this because tax may be depended on car type (assuming car has two types: RegularCar and SupperCar). So we mark virtual keyword for the getTax() function. The class Car now is considered to be abstract.
  1. class Car{  
  2.  string color;  
  3.  string model;  
  4. public:  
  5.  string getColor(){  
  6.   return this->color;  
  7.  }  
  8.  void setColor(string color){  
  9.   this->color = color;   
  10.  }   
  11.  string getModel(){  
  12.   return this->model;  
  13.  }  
  14.  void setModel(string model){  
  15.   this->model = model;  
  16.  }  
  17.  virtual float getTax();  
  18. }  
?
We cannot use abstract class directly and the classes inheriting from this class have to provide implementation of the virtual methods.
  1. class SportCar : public Car{  
  2. public:  
  3.  float getTax(){  
  4.    return 2000f;  
  5.  }  
  6. }  
?

1.3. Interface

In C++ we may consider an interface as a pure abstract class (with no implementation code). For example: 
?
  1. class Shape{  
  2. public:  
  3.  virtual float getArea();  
  4.  virtual float getPerimeter();  
  5. }  
  6.   
  7. class Square : public Shape{  
  8.  int sideLength;  
  9. public:  
  10.  float getArea(){  
  11.   return sideLength*sideLength;  
  12.  }  
  13.  float getPerimeter(){  
  14.   return 4*sideLength;  
  15.  }  
  16. }  

1.4. Why do we need Interfaces and Abstract Classes

We use interfaces and abstract class when:
 - We need multiple class to behave in a polymorphic way
 - We need some kind off contract to enforce on the classes.

2. Program to an interface, not an implementation 

This principle means uses interfaces from other part of the application rather than the concrete implementation. When we work on a software, we often spend much time maintaining than developing. If we programming to an implementation, when the requirement change, we need much more efforts in modifying code. For example:
  1. class Dog{  
  2. public:  
  3.  void run(){  
  4.   cout <<"A dog is running";  
  5.  }  
  6. }  
  7.    
  8. Class Collection{  
  9. public:   
  10.  void performAnimalAction(){  
  11.   Dog *dog = new Dog;  
  12.   dog -> run();  
  13.  }  
  14. }  
?
This example, use concrete implementation (Dog), it work fine if we has only Dog in Collection. Imagine that we add a new class called Cat to the class Collection later, the performAnimalAction() function will be messed up.

To handle this problem,  we create a interface Animal, and use interface instead of concrete implementation in performAnimalAction() function.

  1. class Animal{  
  2. public:  
  3.  virtual void run();  
  4. }  
  5.    
  6. class Dog : public Animal{  
  7. public:  
  8.  void run(){  
  9.   cout <<"A dog is running";  
  10.  }  
  11. }  
  12.    
  13. class Cat : public Animal{  
  14. public:  
  15.  void run(){  
  16.   cout <<"A cat is running";  
  17.  }  
  18. }  
  19. class Collection{  
  20. public:   
  21.  void performAnimalAction(Animal *_animal){  
  22.   Animal *animal = _animal;  
  23.   animal->run();  
  24.  }  
  25. }  
  26.    
  27. int main(){  
  28.  Collection *collection = new Collection;  
  29.  collection->performAnimalAction(new Dog);  
  30.  collection->performAnimalAction(new Cat);  
  31. }  
?

Reference

Không có nhận xét nào:

Đăng nhận xét