Interfaces & Constructors

  • Thread starter Thread starter hufaunder
  • Start date Start date
H

hufaunder

I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
of the interface should have the freedom to add any
function/constructor he wants on top of the interface. But can't I at
least force that one particular constructor must be present? I tried it
but got the following error "Interfaces cannot contain constructors".
Therefore, I wonder if it is impossible to do what I want to do. If so
where does this limitation come from?

Thanks
 
I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
of the interface should have the freedom to add any
function/constructor he wants on top of the interface. But can't I at
least force that one particular constructor must be present? I tried it
but got the following error "Interfaces cannot contain constructors".
Therefore, I wonder if it is impossible to do what I want to do. If so
where does this limitation come from?

You can't specify this, I'm afraid. .NET itself doesn't have support
for it.

With generic constraints you can enforce that the type being used has a
parameterless constructor, but that's all.
 
Hi,

(e-mail address removed) napisa³(a):
I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
of the interface should have the freedom to add any
function/constructor he wants on top of the interface. But can't I at
least force that one particular constructor must be present? I tried it
but got the following error "Interfaces cannot contain constructors".
Therefore, I wonder if it is impossible to do what I want to do. If so
where does this limitation come from?

Thanks

As Michael mentioned before, you have to consider the
abstract class.
An interface is a contract which works on a class instances,
after they are initialized (not before)!

With regards
Marcin
 
Additionally, wanting something like this usually indicates a flaw in
the object design, see if you can solve your problem with:
- a factory pattern
- (abstract) base class (maybe in combination with a (simple) interface)
 
Additionally, wanting something like this usually indicates a flaw in the
object design, see if you can solve your problem with:
- a factory pattern
- (abstract) base class (maybe in combination with a (simple) interface)

Abstract class without a default construct would definitely be the simplest
and easiest way to do it.


At the same time, you may want to have a look at ObjectBuilder pattern --
part of Smart Client Software Factory.
SCSF definitely will need a lot of study before you can go ahead to start
using them. In simplest bird's-eye view for using SCSF, you will need to
implement a "strategy" of "identifying the constructors" for instantiation.

But then, SCSF may or may not be suitable... will depend on the "bigger
picture" of your application.
 
An abstract class is what I should use. I think that will work just
fine for my scenario. Thanks for the tip.
 
Abstract class without a default construct would definitely be the
simplest and easiest way to do it.

This can't prevent the derived class from providing a default constructor,
nor prevents the derived class from requiring additional parameters.

abstract class A
{
public A(bool needed) {}
}

class B : A
{
public B() : base(false) {} // see -- still have a default
constructor
}

There is no way to force or forbid a derived class to provide a constructor
with a particular signature. You'd need reflection to call it dynamically
anyway -- use the Factory pattern instead.
 
Points taken and duly accepted.
This can't prevent the derived class from providing a default constructor,
nor prevents the derived class from requiring additional parameters.

However, the base (abstract) class is always provided with a parameter
to its constructor(s).
There is no way to force or forbid a derived class to provide a
constructor with a particular signature. You'd need reflection to call it
dynamically anyway -- use the Factory pattern instead.

Precisely... where one may wish to use ObjectBuilder pattern.
 
Back
Top