Rob Kraft's Software Development Blog

Software Development Insights

Archive for May, 2008

Inheritance or Interfaces or Just One Big Class?

Posted by robkraft on May 27, 2008

I often struggle with choosing between interfaces, inheritance, or just a big class with switch statements. Here are some scenarios along with my suggested design. Real world scenarios vary, and there are good arguments for not using my suggested approach in similar situations.

1) Create a class or classes to contain products for my Tier 1 and Tier 2 customers”. “All of my Tier 1 customers can purchase all products, but my Tier 2 customers can only purchase products priced less than $10,000.

  • Incorrect – Interface. An interface should not be used to provide similarity across classes when the classes also can share code.
  • Incorrect – Inheritance. It is difficult to determine which class would inherit from the other; though both classes could inherit from the same base class. The only difference may be the source of the data retrieved. Having separate classes, even inherited, may require the UI to code additional logic to access the two differently named classes.
  • Correct – One big class. The UI would prefer to treat each class using the same code. If I use the same class, then the UI won’t need conditional logic to instantiate and handle the objects of the correct type. At the data retrieval level, you will need to run a different query to get the correct set of products, but that can probably be handled by passing in a paramater on the constructor.

2) Create a class or classes for Products, OnOrderProducts, and ObsoleteProducts.

  • Incorrect – Interface. An interface should not be used to provide similarity across classes when the classes also can share code.
  • Incorrect – One big class. Assuming that the 3 classes share a lot of business rules, yet have a few slightly different business rules and properties, then one big class should not be used because it does not clearly separate the different business logic for each class, and it does not allow the UI to easily work differently with the different rules for each.
  • Correct – Inheritance. You may have all 3 product classes inherit from a single abstract base class, or you may have 2 of the product classes inherit from the 3rd and simply add additional properties and methods. In many cases, it is better to inherit from a base abstract class as that makes it simple to exclude properties and methods from any class, as well as include properties and methods unique to one class. 

3) Add a SaveAsXML method to several classes.

  • Incorrect – Inheritance. Because the code to persist each class may be different, and because most of the other properties and methods in the classes are not the same, we should not use inheritance to add this feature.
  • Incorrect – A separate function that receives the class as input. Generally each class should implement its own behavior. The UI should call a method on the class to save it as XML; the UI should not call a function and pass the class to it because the former technique is a more object-oriented approach.
  • Correct – Use an interface. Create an Interface that includes the SaveAsXML and any other related methods. Have each class that will be able to SaveAsXML implement the interface. They could also possibly share the same bit of code from an existing base class they already share, or each class could call a common SaveAsXML class from within their own code. This approach allows the UI to identify which classes implement the interface, and to safely cast any such class to that interface and perform the SaveAsXML operation on them.

Some guidelines I use to help me choose which approach to use are:

  1. When I am considering multiple classes, and all the classes will have a lot of the same properties and methods, then I should probably use inheritance instead of an interface.
  2. When I am considering multiple classes, and all the classes can use the same code, then I should probably use inheritance instead of an interface.
  3. When I have some functionality that I would like to apply to classes that already have (or will have) different properties and methods, then I should probably use an interface.
  4. When I am considering multiple classes, and all the classes will have the same properties and methods, then I may want to use a single class instead of inheritance.
Advertisements

Posted in Code Design, Visual Studio 2008 | Tagged: , | Leave a Comment »