Java vs. Ruby: Adapters

Here’s Java to adapt an OpenCloseDevice to an OnOffDevice:

public class OnOffAdapter implements OnOffDevice {
  private OpenCloseDevice myDevice;
  public OnOffAdapter(OpenCloseDevice adaptedDevice) {
    myDevice = adaptedDevice;
  public void on() {
  public void off() {;
// OnOffAdapter usage
OnOffDevice valveAdapter = new OnOffAdapter(reliefValve);

Ruby equivalent:

valve_adapter =,
  :on  => :close,
  :off => :open)

Reference: Thinking in Ruby

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply