Choosing the right way to design a class library

P

PepsiDrinker

Hello! :^)

I am trying to implement a class library for dealing with time series.
I have to use time series with seasonable component and without it.
Also, time series can be stored in memory or in a database.
The problem is to make 4 following classes:
1. non-seasonable and storable in memory
2. seasonable and storable in memory
3. non-seasonable and storable in database
4. seasonable and storable in database
In C++ this problem could be solved by using multiple inheritance. But
I can't inherit class more than from one class in C# and, if I use
interfaces I must write the same implementation of abstract members
myself. For example, if I make an interface ISeasonable, I will need
to write the same code for in-memory and database-storable time
series.

Please, help me to implement my task in C# (if it is possible)

Thanx for attention
 
B

Bob Grommes

The designers of C# regard MI as Evil. Although I agree with this
generally, I agree that mixins would be useful.

To solve your problem my tendency would be to separate the relevant parts of
the TimeSeries implementation into separate classes and store instances of
those in private fields of a single TimeSeries class based on how the object
is created, i.e. with a constructor like:

public TimeSeries(bool isSeasonable,TimeSeriesStorageType storageType) {

if (isSeasonable) {
this.myImplementation = new SeasonableTimeSeriesImplementation();
} else {
this.myImplementation = new NonSeasonableTimeSeriesImplementation();
}

switch (storageType) {
case TimeSeriesStorageType.Memory:
this.myStorageLogic = new MemoryTimeSeriesStorageImplementation();
case TimeSeriesStorageType.Database:
this.myStorageLogic = new DatabaseTimeSeriesStorageImplementation();
default:
throw new NotImplementedException(String.Format("Time Series Storage
Type {0} is not implemented.",storageType));
}

}

.... where TimeSeriesStorageType is an enum:

public enum TimeSeriesStorageType {Memory,Database}

Now your internal implementation for various methods would reference the
myImplementation or myStorageLogic reference at runtime to access the
appropriate implementation logic.

Probably there are other ways to do this, but that's my $0.02.

--Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top