-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-adapter.rb
49 lines (39 loc) · 926 Bytes
/
06-adapter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 06. Adapter (using composition)
# Target
class Person
attr_accessor :name, :lastname
def initialize(name, lastname)
@name = name
@lastname = lastname
end
def say_hello
puts "Hello, my name is #{ [self.name, self.lastname].join(' ') }"
end
end
# Adaptee
class FrenchPerson
attr_accessor :prenom, :nom, :age
def initialize(prenom, nom, age)
@prenom = prenom
@nom = nom
@age = age
end
def say_bon_jour
puts "Bon jour, je m'appelle #{ [self.prenom, self.nom].join(' ') }"
end
end
# Adapter (adapts FrenchPerson -> Person)
class FrenchPersonAdapter
def initialize(frenchPerson)
@frenchPerson = frenchPerson
end
def say_hello
@frenchPerson.say_bon_jour
end
end
# Client
french_person = FrenchPersonAdapter.new(FrenchPerson.new('Chuck', 'Norris', 35))
person = Person.new('Britney', 'Spears')
[french_person, person].each do |person|
person.say_hello
end