This discussion is archived
9 Replies Latest reply: Mar 1, 2009 2:01 AM by 843785 RSS

does a one method Interface indicate bad design?

843785 Newbie
Currently Being Moderated
as said in the subject:

does a one method interface indicate a bad design?

if i were in an open-source java project, and offered the idea of having a one method interface, would it be
(1) accepted as common practice?
(2) noted as unusual, but sometimes appropriate?
(3) considered to be a completely bad design?

i read that having more classes with fewer public methods is the most agile OOP design.
still.... a one method interface appears pretty extreme? thanks for any guidance.

(as i don't know C++ or C#, i hope this counts as a java question)
  • 1. Re: does a one method Interface indicate bad design?
    843785 Newbie
    Currently Being Moderated
    ActionListener, which is probably the one interface that I use most commonly, is a one-method interface. I wouldn't listen to anyone who gives any hard and fast rules stating that this is always good design or bad. Instead I would concentrate on using the best tool for the job at hand. If that means using a one-method interface, then so be it, and move on.
  • 2. Re: does a one method Interface indicate bad design?
    843785 Newbie
    Currently Being Moderated
    The question is, what does this type do? (the type you're defining, i mean)

    If it does only one thing, then a single method is appropriate.

    If you have two objects that have to constantly talk to each other to accomplish their most basic tasks, then those two object probably ought to be the same object.

    If you have a single object that does two entirely different unrelated things, then that object probably ought to be two objects.

    A type with a single exposed method is perfectly fine if that method is what the type does.

    In addition to ActionListener, consider an iterator type -- a type that has a method, say, like this:
    <T> next();
    all this type does is return the next item in a (possibly virtual) list of items that it represents. That's all it should do, so having a single method is wholly appropriate.
  • 3. Re: does a one method Interface indicate bad design?
    843785 Newbie
    Currently Being Moderated
    i did not know about iterator or ActionListener. but i did forget about the
    Runnable interface. now i feel fine about writing a one method interface if the design clearly calls for one.
    thanks for setting me straight!
  • 4. Re: does a one method Interface indicate bad design?
    800308 Newbie
    Currently Being Moderated
    Closeable sticks out in my mind.

    The below Streams/Readers/Writers/Sockets/Channels/etc,etc,etc are all Closable... and may therefore all be closed (and any IOExceptions handled) by one generic helper method.
    AbstractInterruptibleChannel, AbstractSelectableChannel, AudioInputStream, BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter, ByteArrayInputStream, ByteArrayOutputStream, CharArrayReader, CharArrayWriter, CheckedInputStream, CheckedOutputStream, CipherInputStream, CipherOutputStream, DatagramChannel, DataInputStream, DataOutputStream, DeflaterInputStream, DeflaterOutputStream, DigestInputStream, DigestOutputStream, FileChannel, FileInputStream, FileOutputStream, FileReader, FileWriter, FilterInputStream, FilterOutputStream, FilterReader, FilterWriter, Formatter, ForwardingJavaFileManager, GZIPInputStream, GZIPOutputStream, InflaterInputStream, InflaterOutputStream, InputStream, InputStream, InputStream, InputStreamReader, JarInputStream, JarOutputStream, LineNumberInputStream, LineNumberReader, LogStream, ObjectInputStream, ObjectOutputStream, OutputStream, OutputStream, OutputStream, OutputStreamWriter, Pipe.SinkChannel, Pipe.SourceChannel, PipedInputStream, PipedOutputStream, PipedReader, PipedWriter, PrintStream, PrintWriter, ProgressMonitorInputStream, PushbackInputStream, PushbackReader, RandomAccessFile, Reader, RMIConnectionImpl, RMIConnectionImpl_Stub, RMIConnector, RMIIIOPServerImpl, RMIJRMPServerImpl, RMIServerImpl, SelectableChannel, SequenceInputStream, ServerSocketChannel, SocketChannel, StringBufferInputStream, StringReader, StringWriter, Writer, ZipInputStream, ZipOutputStream
      /**
       * close these "streams"
       * @param Closeable... "streams" to close.
       */
      public static void close(Closeable... streams) {
        Exception x = null;
        for(Closeable stream : streams) {
          if(stream==null) continue;
          try {
            stream.close();
          } catch (Exception e) {
            if(x!=null)x.printStackTrace();
            x = e;
          }
        }
        if(x!=null) throw new RuntimeIOException(x.getMessage(), x);
      }
    I like to think of an interface as defining an aspect of a type, as apposed to a complete type.... Allowing the "consumer" to deal with only the aspects of the type which interest them... This (IMHO) leads to more "generic" code, increasing code-reuse, hopefully leading to less and more maintainable code.

    The downside is that "thin" types (especially collections of them) leads to reliance on instanceof and typecasting (to get at the other interesting aspects of the collected objects... and to be honest I still don't have a "good" solution to this inherent problem... so (for the moment) I "bundle" instanceof and typecasting into a collection-container class... not good.

    Cheers. Keith.
  • 5. Re: does a one method Interface indicate bad design?
    843785 Newbie
    Currently Being Moderated
    corlettk wrote:
    Closeable sticks out in my mind.
    or Cloneable
  • 6. Re: does a one method Interface indicate bad design?
    843785 Newbie
    Currently Being Moderated
    tschodt wrote:
    corlettk wrote:
    Closeable sticks out in my mind.
    or Cloneable
    I don't know why, though. It has no methods at all!
  • 7. Re: does a one method Interface indicate bad design?
    843785 Newbie
    Currently Being Moderated
    rksj wrote:
    as said in the subject:

    does a one method interface indicate a bad design?
    Nope
    if i were in an open-source java project, and offered the idea of having a one method interface, would it be
    Why would OSS be any different?
    (1) accepted as common practice?
    If it made sense that the interface have only one method, yes
    (2) noted as unusual, but sometimes appropriate?
    Not unusual at all
    (3) considered to be a completely bad design?
    Not at all
    i read that having more classes with fewer public methods is the most agile OOP design.
    still.... a one method interface appears pretty extreme? thanks for any guidance.
    The methods should be cohesive, that's all. There are plenty of examples of one-method interfaces in the JDK, as other posters have noted
    (as i don't know C++ or C#, i hope this counts as a java question)
    It does
  • 8. Re: does a one method Interface indicate bad design?
    843785 Newbie
    Currently Being Moderated
    georgemc wrote:
    tschodt wrote:
    corlettk wrote:
    Closeable sticks out in my mind.
    or Cloneable
    I don't know why, though. It has no methods at all!
    Since OP was asking about interfaces with no more than a single method
    I was thinking one or more of the marker interfaces
    java.lang.Cloneable
    java.io.Serializable
    java.util.EventListener
    java.rmi.Remote
    javax.servlet.SingleThreadModel
    might drive home a point.
    If we limit ourselves to talking about interfaces with exactly one method I would probably point to Runnable.
  • 9. Re: does a one method Interface indicate bad design?
    843785 Newbie
    Currently Being Moderated
    In package javax.swing.event, 8 of 22 listener interfaces had a single method. Hmmm...