Last active
June 11, 2018 13:52
-
-
Save fukaoi/fdd7baaa78a91c724e11edb313a3988a to your computer and use it in GitHub Desktop.
Adapter pattern in crystal-lang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Demo | |
@obj: Coin | |
def initialize(name : String) | |
if name == "LTC" | |
@obj = LTC.new | |
elsif name == "ETH" | |
@obj = ETH.new | |
else | |
raise "Not found name" | |
end | |
end | |
def exec(name : String) | |
@obj.exec(name) | |
end | |
end | |
abstract class Coin | |
abstract def exec(name : String) | |
end | |
class LTC < Coin | |
def initialize | |
end | |
def exec(name : String) | |
puts "Im LTC: #{name}" | |
end | |
end | |
class ETH < Coin | |
def initialize | |
end | |
def exec(name : String) | |
puts "Im ETH: #{name}" | |
end | |
end | |
demo = Demo.new("ETH") | |
demo.exec("hello") | |
demo = Demo.new("LTC") | |
demo.exec("hello") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment