Attach constants to an interface

  • Thread starter Thread starter _DD
  • Start date Start date
D

_DD

I've encountered situations that call for an interface but also
require enums/constants that go hand-in-hand with the interface.
Abstract classes won't do, cause I may need to inherit more than one.
Any elegant solutions come to mind?
 
Perhaps I've misunderstood your question, but is this not something that can
be achieved with a property defined on the interface with only a getter (i.e.
a read-only property)? Obviously you cannot define values within the
interface definition, but in this way you can require your implementors to
supply one...

interface IMyInterface
{
string Name { get; }
}

class MyClass : IMyInterface
{
public string Name
{
get { return "Foo"; }
}
}
 
_DD said:
I've encountered situations that call for an interface but also
require enums/constants that go hand-in-hand with the interface.
Abstract classes won't do, cause I may need to inherit more than one.
Any elegant solutions come to mind?

Just use a separate enum type. Just because two concepts are related
doesn't mean they have to be part of the same type.
 
Perhaps I've misunderstood your question, but is this not something that can
be achieved with a property defined on the interface with only a getter (i.e.
a read-only property)? Obviously you cannot define values within the
interface definition, but in this way you can require your implementors to
supply one...

I was thinking of simple constants. For instance, if an interface had
a function: PaintBackgroundPurple(), it would make sense to have a
defined Purple that could travel with the interface. In other words,
the implementation of how to paint the background would be variable,
but you don't want the particular shade of Purple to vary. Everyone
that implements the interface would use the same Purple.

In that sense, it would be nice to have tighter coupling between the
Interface and the defined colors that it is using.

This may not be the perfect example, but I do run into variants on the
scenario once in a while.

I understand that allowing constants within the Interface could result
in diamond inheritance, with multiple Purple defines converging when
multiple interfaces are inherited, but I was wondering if there is
another way to implement this.
 
_DD said:
I was thinking of simple constants. For instance, if an interface had
a function: PaintBackgroundPurple(), it would make sense to have a
defined Purple that could travel with the interface. In other words,
the implementation of how to paint the background would be variable,
but you don't want the particular shade of Purple to vary. Everyone
that implements the interface would use the same Purple.

In that sense, it would be nice to have tighter coupling between the
Interface and the defined colors that it is using.

It seems more sensible to have PaintBackground(PredefinedColor) and
have a PredefinedColor enum to go alongside the interface.

I just don't see the advantage of having the constants *in* the
interface rather than as a separate type to go alongside it.
 
Sorry I don't know a way of introducing simple constants within interfaces
You might be able to get an equivalent *look-and-feel* through nested
namespaces.

I think that being able to introduce constants into interfaces is very
important since some interfaces have a real-world analogy.

eg. // Contrived example

interface IRS232_Physical
{
const int MIN_LINE_VOLTAGE = -12;
const int MAX_LINE_VOLTAGE = 12;

void public ToggleRTS(bool TurnOn);
void public TransmitHoldingReg();

// ...


}

class USBToRS232Converter: IWin32SerialAPI, IUSBStack, IRS232_Physical
{
public void TransmitHoldingReg()
{
// Get TX Holding Reg

// Send Start Bits

// Send Data Bits

// Send Stop Bits
}
}
 
steve said:
I think that being able to introduce constants into interfaces is very
important since some interfaces have a real-world analogy.

eg. // Contrived example

interface IRS232_Physical
{
const int MIN_LINE_VOLTAGE = -12;
const int MAX_LINE_VOLTAGE = 12;

void public ToggleRTS(bool TurnOn);
void public TransmitHoldingReg();

// ...

This looks awfully like the Java Constant Interface Antipattern
(Effective Java (http://java.sun.com/docs/books/effective/) Item 17).

-- Barry
 
The Java cons... what??? Me not speaka da language ; )

Never heard of it sorry. I'll put it in the to have a look at basket.

Ciao,

Steve
 
Back
Top