Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base Class for multiple classes in different projects

This is more of a design question.

Currently I have multiple classes (in different projects) which have different roles but there is one common method which performs the same duty. I was thinking about having a base class for all theses classes so each of them could inherit this class and implement this method to save duplication.

My question is should I have one base class for all the classes which are in multiple projects, or should I have one base class per project?

Thanks,

like image 772
CodersSC Avatar asked May 12 '15 13:05

CodersSC


People also ask

Can a class inherit from multiple base classes?

You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.

How many base classes are there in multiple inheritance?

Explanation: For the implementation of multiple inheritance, there must be at least 3 classes in a program. At least 2 base classes and one class to inherit those two classes. If lesser, it becomes single level inheritance.

What is multiple base class?

A class can be derived from more than one base class. In a multiple-inheritance model (where classes are derived from more than one base class), the base classes are specified using the base-list grammar element.

How many base classes can a class have?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class.


2 Answers

I would define an interface that contains that one shared method. I would then put that interface in it's own project. Then have you concrete implementations reference that project and implement the interface.

Something like this..

  • Repositories.proj
    • IRepository
  • EntityFrameworkImpl.proj
    • Repository : IRepository
  • NHibernateImpl.proj
    • Repository : IRepository
like image 182
Alex Avatar answered Sep 23 '22 17:09

Alex


In general 1 class for all your projects is a lot better than creating the same class for each project.

Actually I dare to say that in almost any case, reusing the same code, rather than copying it. Always is better

In general static libraries are the best tool to solve the problem you are currently facing.

EDIT:

If you are working in visual studio i would solve this as follows. Create a project containing the base class declaration and definition. This projects should be of the type static library.

The other projects should include the directory containing the header file in inclusion dirs. And the directory containing the lib file in the additional dependency directories.

like image 34
laurisvr Avatar answered Sep 24 '22 17:09

laurisvr