Ruby's equivalent to Java's "interface"

Please forgive this rather naive question, but it’s a query that’s
difficult to search for on google. In java, you can program to
interface. IE, in java, you can do:

public interface QuakeBehavior {
public void quack();
}

public class Quack implements QuckBehavior {
public void quack() {
System.out.println(“Quack”);
}
}

public class Squeek implements QuackBehavior {
public void quack() {
System.out.println(“Squeek”);
}
}

public class MuteQuack implements QuackBehavior {
public void quack() {
System.out.println("<< Silence >>");
}
}

I am learning about Design Patterns (which is a wonderful concept), but
I want to know the Ruby equivalent.

  • steve

Steve,

The Ruby equivalent of Java’s interface is module.

Deepak

Steve Q. [email protected] wrote:
Please forgive this rather naive question, but it’s a query that’s
difficult to search for on google. In java, you can program to
interface. IE, in java, you can do:

public interface QuakeBehavior {
public void quack();
}

public class Quack implements QuckBehavior {
public void quack() {
System.out.println(“Quack”);
}
}

public class Squeek implements QuackBehavior {
public void quack() {
System.out.println(“Squeek”);
}
}

public class MuteQuack implements QuackBehavior {
public void quack() {
System.out.println("<< Silence >>");
}
}

I am learning about Design Patterns (which is a wonderful concept), but
I want to know the Ruby equivalent.

On Sun, 19 Aug 2007 11:17:07 +0900, Olivier R. wrote:

Note that the method ‘quack’ from class ‘Squeek’ replaces the definition
of the method ‘quack’ that was first included from ‘QuakeBehavior’. If
you use this technique, you will be warned by a NotImplementedError (so,
at runtime), if you forgot to define a method in classes that include
QuakeBehavior. I don’t think it is a good design, but maybe it can help.

You get two differences when you do this: one is that
NotImplementedError rather than a NameError – that’s probably not too
important. The other is that you can use is_a? and case statements to
match QuakeBehavior.

–Ken

Ruby modules also allow implementation details and I’m not sure if this
is
also true for java interfaces.

public void quack() {
public class MuteQuack implements QuackBehavior {
public void quack() {
System.out.println("<< Silence >>");
}
}

I am learning about Design Patterns (which is a wonderful concept), but
I want to know the Ruby equivalent.

  • steve

Le samedi 18 août 2007 23:59, Deepak Vohra a écrit :

Steve,

The Ruby equivalent of Java’s interface is module.

Deepak

I disagree : the equivalent of java’s interface is implicit interface.
That
is, two objects implement an interface if they can respond to a common
set of
methods. There is no need to explicitly declare, like in java with
the “interface” construction, this set of methods. This is possible
because
of the dynamic typing of Ruby. You can search the keywords “duck
typing”, for
more details.

It has the advantage of being more concise, faster to write, and avoids
repetitions of method declarations . The disadvantage is that there is
not
dedicated places to formally define the interfaces.

Modules are constructs that allow to define a bunch of methods to be
“copied”
inside a class. Of course, every classes that include the same module
share
the same interface, since they copied the same methods. If you want to
compare to java, modules are more similar to java’s abstract classes.
The
difference is that java allows a class to inherit from only one abstract
class, when a ruby class can include many modules.

If you really want to have a place to explicitly define an interface,
you can
declare fake methods in a module :

module QuakeBehavior
def quack
raise NotImplementedError.new
end
end

class Squeek
include QuakeBehavior
def quack
puts ‘squeek’
end
end

Note that the method ‘quack’ from class ‘Squeek’ replaces the definition
of
the method ‘quack’ that was first included from ‘QuakeBehavior’.
If you use this technique, you will be warned by a NotImplementedError
(so, at
runtime), if you forgot to define a method in classes that include
QuakeBehavior. I don’t think it is a good design, but maybe it can help.

Robert K. wrote:

2007/8/19, david karapetyan [email protected]:

Ruby modules also allow implementation details and I’m not sure if this is
also true for java interfaces.

You can only define constants in a Java interface, no methods. A Ruby
module is by far not the equivalent to a Java interface. The short
answer is: there is no equivalent thing in Ruby. It is not needed
because of Ruby’s dynamic nature (see Olivier’s explanation).

Kind regards

robert

I second that…

2007/8/19, david karapetyan [email protected]:

Ruby modules also allow implementation details and I’m not sure if this is
also true for java interfaces.

You can only define constants in a Java interface, no methods. A Ruby
module is by far not the equivalent to a Java interface. The short
answer is: there is no equivalent thing in Ruby. It is not needed
because of Ruby’s dynamic nature (see Olivier’s explanation).

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs