Skip to main content
Tweeted twitter.com/StackSoftEng/status/1201380603779780608
Question Protected by gnat
english typo
Source Link
ianmandarini
  • 2.8k
  • 15
  • 26

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. With that in mind, I asked myself: Is there any way to enforce a behavior relationship between interface methods?

I thought about defining this behavior via inheritance (by defining a common implementation). But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, I wanted the following relationship shouldto be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

In that example, the behavior constraint that I would want to enforce is:

  • When implementing Enable(), the implementer should ensure that IsEnabled() returns true
  • When implementing Disable(), the implementer should ensure that IsEnabled() returns false

Is there a way to enforce this implementation constraint? Or, the fact that I am thinking about enforcing this kind of constraint is itself a sign that there is a flaw in the design?

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. With that in mind, I asked myself: Is there any way to enforce a behavior relationship between interface methods?

I thought about defining this behavior via inheritance (by defining a common implementation). But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, I wanted following relationship should be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

In that example, the behavior constraint that I would want to enforce is:

  • When implementing Enable(), the implementer should ensure that IsEnabled() returns true
  • When implementing Disable(), the implementer should ensure that IsEnabled() returns false

Is there a way to enforce this implementation constraint? Or, the fact that I am thinking about enforcing this kind of constraint is itself a sign that there is a flaw in the design?

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. With that in mind, I asked myself: Is there any way to enforce a behavior relationship between interface methods?

I thought about defining this behavior via inheritance (by defining a common implementation). But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, I wanted the following relationship to be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

In that example, the behavior constraint that I would want to enforce is:

  • When implementing Enable(), the implementer should ensure that IsEnabled() returns true
  • When implementing Disable(), the implementer should ensure that IsEnabled() returns false

Is there a way to enforce this implementation constraint? Or, the fact that I am thinking about enforcing this kind of constraint is itself a sign that there is a flaw in the design?

Became Hot Network Question
better phrasing the question
Source Link
ianmandarini
  • 2.8k
  • 15
  • 26

In C#, is there a way to enforce behavior coupling in interface methods? Is this question or is the fact that I am trying to do that a design smell?

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. With that in mind, I asked myself: Is there any way to enforce thisa behavior relationship between interface methods?

Of course, one could defineI thought about defining this behavior asvia inheritance (by defining a common implementation). But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, theI wanted following relationship should be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

For clarification:In that example, the behavior constraint that I would want to enforce the constraint thatis:

  • When implementing Enable(), the implementer should ensure that IsEnabled() returns true
  • When implementing Disable(), the implementer should ensure that IsEnabled() returns false

HowIs there a way to enforce this implementation constraint? IsOr, the fact that I am thinking about enforcing this kind of constraint is itself a sign (a smell) that this designthere is flaweda flaw in some waythe design?

In C#, is there a way to enforce behavior coupling in interface methods? Is this question a design smell?

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. Is there any way to enforce this relationship?

Of course, one could define this behavior as inheritance. But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, the following relationship should be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

For clarification: I want to enforce the constraint that:

  • When implementing Enable(), the implementer should ensure that IsEnabled() returns true
  • When implementing Disable(), the implementer should ensure that IsEnabled() returns false

How to enforce this implementation constraint? Is this a sign (a smell) that this design is flawed in some way?

In C#, is there a way to enforce behavior coupling in interface methods or is the fact that I am trying to do that a design smell?

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. With that in mind, I asked myself: Is there any way to enforce a behavior relationship between interface methods?

I thought about defining this behavior via inheritance (by defining a common implementation). But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, I wanted following relationship should be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

In that example, the behavior constraint that I would want to enforce is:

  • When implementing Enable(), the implementer should ensure that IsEnabled() returns true
  • When implementing Disable(), the implementer should ensure that IsEnabled() returns false

Is there a way to enforce this implementation constraint? Or, the fact that I am thinking about enforcing this kind of constraint is itself a sign that there is a flaw in the design?

clarifying the question being asked
Source Link
ianmandarini
  • 2.8k
  • 15
  • 26

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. Is there any way to enforce this relationship?

Of course, one could define this behavior as inheritance. But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, the following relationship should be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

For clarification: I want to enforce the constraint that:

  • When implementing Enable(), the implementer should ensure that IsEnabled() returns true
  • When implementing Disable(), the implementer should ensure that IsEnabled() returns false

How to enforce this implementation constraint? Is this a sign (a smell) that this design is flawed in some way?

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. Is there any way to enforce this relationship?

Of course, one could define this behavior as inheritance. But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, the following relationship should be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

How to enforce this implementation constraint? Is this a sign (a smell) that this design is flawed in some way?

Many times I want to define an interface with some methods that maintain a behavior relationship between them.

However, I feel that many times this relationship is implicit. Is there any way to enforce this relationship?

Of course, one could define this behavior as inheritance. But since C# does not allow multiple inheritance, I believe that many times an interface would be more advisable and that inheritance is not flexible enough.


For example:

public interface IComponent { void Enable(); void Disable(); bool IsEnabled(); } 

For this interface, the following relationship should be fulfilled:

  • If Enable() is called, IsEnabled() should return true.
  • If Disable() is called, IsEnabled() should return false.

For clarification: I want to enforce the constraint that:

  • When implementing Enable(), the implementer should ensure that IsEnabled() returns true
  • When implementing Disable(), the implementer should ensure that IsEnabled() returns false

How to enforce this implementation constraint? Is this a sign (a smell) that this design is flawed in some way?

edited tags
Link
Martin Maat
  • 18.6k
  • 3
  • 33
  • 59
Loading
Source Link
ianmandarini
  • 2.8k
  • 15
  • 26
Loading