简体   繁体   中英

Code smell - only 1 concrete class and interface

I have an interface:

interface DataExtractor {
  public function extractData($dataSource);
}

I have ended up only having a single concrete example which is general purpose enough to serve all of my required needs.

Im am therefore left with an issue on what to name the concrete class and this leads me to ask if this is a code smell and i should not actually have the interface in the first place.

  • DefaultDataExtractor
  • BaseDataExtractor
  • changing the interface to iDataExtractor and the concrete class to DataExtractor

These all seem wrong

I would go with the third option--removing the interface. You will drive yourself batty putting interfaces on all your classes when you don't need the indirection; it's just confusing. Less code is better code. When you need the abstraction, that's when you should add the interface. Refactoring tools will make that task very simple when the time comes.

Benefits besides having more than one implementation -

  1. Testing - classes may seal public methods which then cannot be overridden to support proper testing.
  2. Same class different interfaces - your only one implementation may need to inherit from some framework class, if your language does not allow for multiple inheritance then it becomes a problem.
  3. Distribution - shipping interfaces and implementation separately. If your implementation will never be used in one system, but the class which declare it will, it is useless to ship the implementation only the deceleration.
  4. Memory consumption - some run time environments load class objects upon deceleration. By separating deceleration, interface will allow you to postpone the loading of the implementation class until actually used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM