I am using JRuby
.
In my Java
code, I have a class called Texture
, capable of doing some graphic manipulation stuff.
In my Ruby
code, I will usually need to draw things, so though I should simply call Java
's Texture
class to do the drawing for me.
However, I ended up making a Ruby
class called Texture
, which wraps an instance of Java
's Texture
. Somehow like this (just an example, but rather accurate):
class Texture
def initialize
@reference = getMeAnInstanceOfAJavaTexture
end
def draw
@reference.draw
end
def rotate
@reference.rotate
end
def clear
@reference.clear
end
end
As you can see, all this Texture
class does is... well, tell a Java
Texture
to do the job for it. Just that.
Subconsciously, I think I wanted to do it this way because it looks pretty (lol).
Now, getting a bit more technical, a possible advantage I see is that this way I don't have to interact much with the Java
-side of the project. Most of the work I will do will be in Ruby
, and maintaining a constant interaction between Ruby
and Java
can be confusing. If I make a Ruby
class that handles this stuff for me, I might feel more comfortable in the future when I have to make like a hundred textures, but instead of interacting a hundred times with Java
, I do it with Ruby
, my main environment.
Does my reasoning make sense or is it a lame excuse to do something that looks pretty? Or perhaps there is indeed a good reason to do what I just did?
-
In the Ruby community "looks pretty" always had very high value. Your code will be more readable for sure. I don't know how important it is from a technical sense, since I don't know Java and the environment you use.thorsten müller– thorsten müller2013年01月22日 08:48:08 +00:00Commented Jan 22, 2013 at 8:48
-
1this is a valid approach and (as a bonus) allows you to substitute the Texture with a custom ruby class later (if you ever feel the need to...)ratchet freak– ratchet freak2013年01月22日 09:15:15 +00:00Commented Jan 22, 2013 at 9:15
1 Answer 1
Yes, this probably makes sense.
Without such a class, you will need to remember the details of calling out to the Java class at each place where you use this, and get those details right at each place -- and even if this is a simple enough use of Java to make this not a burden, those lines of code will "feel" more in the Java idiom than the Ruby idiom, breaking the flow of your program when reading the code. As you find yourself repeating this usage in more places, the value in fixing this grows (i.e. one use of a Java class probably does not justify a wrapepr).
Bottom line: creating a class or method to wrap something you find yourself doing over and over again in a simple interface which feels right in the context of your program can make your code easier to read and maintain.
-
1This can also aid in encapsulation. If the java object has functionality which is unused, excluding it from your interface can simplify your coding. This is not specific to cross-language wrappers; providing intra-language interfaces to a subset of an object's functionality can also aid encapsulation.Brian– Brian2013年01月22日 22:50:07 +00:00Commented Jan 22, 2013 at 22:50
-