Skip to content

Instantly share code, notes, and snippets.

@wcc526
Created September 26, 2015 06:59
Show Gist options
  • Save wcc526/8dd8b233b274656b19ce to your computer and use it in GitHub Desktop.
Save wcc526/8dd8b233b274656b19ce to your computer and use it in GitHub Desktop.
ruby adapter

Adapter Design Pattern

Abstract Duck

class Duck def quack end

def fly end end

Abstrack Turkey

class Turkey def gobble end

def fly end end

Weird Duck

class WeirdDuck < Duck# Adaptee

def quack "quack" end

def fly "I'm flying" end end

Wild Turkey

class WildTurkey < Turkey

def gobble "Gobble Gobble" end

def fly "I'm flying a short distance" end end

Turkey to Duck Adapter

class TurkeyToDuckAdapter # < Duck

attr_reader :turkey

def initialize(turkey) @turkey = turkey end

def quack @turkey.gobble end

def fly @turkey.fly end end

wildturkey = WildTurkey.new

duck_like_turkey = TurkeyToDuckAdapter.new(wildturkey)

puts duck_like_turkey.quack

puts duck_like_turkey.fly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment