Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define optional implementation methods in Interface?

Tags:

c#

.net-3.5

Is it possible to define an Interface with optional implementation methods? For example I have the following interface definition as IDataReader in my core library:

public interface IDataReader<T> {
  void StartRead(T data);
  void Stop();
}

However, in my current implementations, the Stop() method has never been used or implemented. In all my implementation classes, this method has to be implemented with throw NotImplementedExcetion() as default:

class MyDataReader : IDataReader<MyData> {
   ...
   public void Stop()
   {
     // this none implementaion looks like uncompleted codes
     throw NotImplementedException();
   }

Of course, I can remove the throw exception code and leave it empty.

When I designed this data reader interface, I thought it should provide a way to stop the reading process. Maybe we will use Stop() sometime in the future.

Anyway, not sure if it is possible to make this Stop() method as an optional implementation method? The only way I can think is to either to define two interfaces one with stop and another without such as IDataReader and IDataReader2. Another option is to break this one into to interfaces like this:

 interface IDataReader<T> {
    void StartRead(T data);
 }

 interface IStop {
    void Stop();
 }

In my implementation cases, I have to cast or use as IStop to check if my implementation supports Stop() method:

 reader.StartRead(myData);
 ....
 // some where when I need to stop reader
 IStop stoppable = reader as IStop;
 if (stoppable != null ) stoppable.Stop();
 ...

Still I have to write those codes. Any suggestions? Not sure if there is any way to define optional implementation methods in an interface in .Net or C#?

like image 492
David.Chu.ca Avatar asked Jul 24 '09 05:07

David.Chu.ca


People also ask

CAN interface have optional methods?

The optional methods in the Collection interface mean that the implementation of the method is allowed to throw an exception, but it has to be implemented anyway. As specified in the docs: Some collection implementations have restrictions on the elements that they may contain.

What is an optional interface?

Optional is a container object used to contain not-null objects. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as 'available' or 'not available' instead of checking null values.

How is optional implemented in Java?

Optional is implemented as a single immutable concrete class that internally handles two cases; one with an element and one without.

Can you define a method in the interface?

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body). Interfaces specify what a class must do and not how. It is the blueprint of the class.


1 Answers

Interesting. I'll have to quote you here:

However, in my current implementations, the Stop() method has never been used or implemented. In all my implementation classes, this method has to be implemented with throw NotImplementedExcetion() as default:

If this is the case, then you have two options:

  1. Remove the Stop() method from the interface. If it isn't used by every implementor of the interface, it clearly does not belong there.
    • Instead of an interface, convert your interface to an abstract base class. This way there is no need to override an empty Stop() method until you need to.

Update The only way I think methods can be made optional is to assign a method to a variable (of a delegate type similar to the method's signature) and then evaluating if the method is null before attempting to call it anywhere.

This is usually done for event handlers, wherein the handler may or may not be present, and can be considered optional.

like image 76
Jon Limjap Avatar answered Nov 02 '22 00:11

Jon Limjap