M
Marcel Overweel
Hi,
I'm wondering if there's a better solution for the following:
I've got a base class called Channel.
Channel is build up mostly out of abstract methods.
A few derived classes, called ComChannel, NetChannel and
ModemChannel contains the actual implementation.
The user of these classes doesn't need to know anything about
the derived classes.
My idea was to give 'Channel' three static methods.
For instance (simplified example, not actual code):
public class Channel
{
static Channel CreateChannel(int a) {...} // returns a ComChannel
static Channel CreateChannel(double a) {...} // returns a NetChannel
static Channel CreateChannel(string a) {...} // returns a ModemChannel
...
other non-static methods and such
}
The derived classes are defined as:
internal class ComChannel : Channel {...}
internal class NetChannel : Channel {...}
internal class ModemChannel : Channel {...}
The class Channel doesn't have a (public) constructor so this forces
the user to call any of the three 'Create...' methods.
Is this a wise design or are there better solutions?
I know I could make the three internal classes public, but then the
user has to decide which class it should instantiate.
The code will be part of an SDK, so I don't want to introduce
classes which are of no concern to the user.
The interface should be as simple as possible.
regards,
Marcel
I'm wondering if there's a better solution for the following:
I've got a base class called Channel.
Channel is build up mostly out of abstract methods.
A few derived classes, called ComChannel, NetChannel and
ModemChannel contains the actual implementation.
The user of these classes doesn't need to know anything about
the derived classes.
My idea was to give 'Channel' three static methods.
For instance (simplified example, not actual code):
public class Channel
{
static Channel CreateChannel(int a) {...} // returns a ComChannel
static Channel CreateChannel(double a) {...} // returns a NetChannel
static Channel CreateChannel(string a) {...} // returns a ModemChannel
...
other non-static methods and such
}
The derived classes are defined as:
internal class ComChannel : Channel {...}
internal class NetChannel : Channel {...}
internal class ModemChannel : Channel {...}
The class Channel doesn't have a (public) constructor so this forces
the user to call any of the three 'Create...' methods.
Is this a wise design or are there better solutions?
I know I could make the three internal classes public, but then the
user has to decide which class it should instantiate.
The code will be part of an SDK, so I don't want to introduce
classes which are of no concern to the user.
The interface should be as simple as possible.
regards,
Marcel