8章で実施したこと
- ファクトリメソッドパターンを適用し、サブクラスへの参照を減少させた
money.rb
#!/usr/bin/ruby
class Money
attr_accessor :amount
protected :amount
def ==(other)
return @amount == other.amount && self.class == other.class
end
def self.franc(amount)
return Franc.new(amount)
end
def self.dollar(amount)
return Dollar.new(amount)
end
end
class Dollar < Money
def initialize(amount)
@amount = amount
end
def *(multiplier)
return Dollar.new(@amount * multiplier)
end
end
class Franc < Money
def initialize(amount)
@amount = amount
end
def *(multiplier)
return Franc.new(@amount * multiplier)
end
end
money_test.rb
#!/usr/bin/ruby
class Money
attr_accessor :amount
protected :amount
def ==(other)
return @amount == other.amount && self.class == other.class
end
def self.franc(amount)
return Franc.new(amount)
end
def self.dollar(amount)
return Dollar.new(amount)
end
end
class Dollar < Money
def initialize(amount)
@amount = amount
end
def *(multiplier)
return Dollar.new(@amount * multiplier)
end
end
class Franc < Money
def initialize(amount)
@amount = amount
end
def *(multiplier)
return Franc.new(@amount * multiplier)
end
end