0
0

More than 5 years have passed since last update.

ruby オブジェクト設計

Posted at

単一責任

変更に対応できるように


- wheelの切り出し

単一責任の為に

- initailizeの while=nil

変動に対応する

class Gear 
  attr_reader :chainring, :cog, :wheel

  def initialize(chainring, cog, wheel=nil)
    @chainring = chainring
    @cog = cog 
    @wheel = wheel
  end

  def ratio
    chainring / cog.to_f
  end

  def gear_inches
    ratio * wheel.diameter
  end
end

class Wheel
  attr_reader :rim, :tire

  def initialize(rim, tire)
    @rim = rim
    @tire = tire
  end

  def diameter
    rim + (tire * 2)
  end

  def circumference
    diameter * Math::PI
  end
end

@wheel = Wheel.new(26, 1.5)

p @wheel.circumference

gear2 = Gear.new(11,11,@wheel)


p gear2.gear_inches

 引数のコントロール

  • ハッシュで格納

arges = ハッシュ

  • 引数の格納順番への依存はなくなる
class Gear

  attr_reader :chinring, :cog, :wheel

  def initialize(arges)
    @chinring = arges[:chinring]
    @cog = arges[:cog]
    @wheel = arges[:wheel]
  end

  def fog
    chinring + cog + wheel
  end
end

class Wheel
  attr_reader :ring

  def initialize(ring)
    @ring = ring
  end

  def ee
   ring
  end

end

p Gear.new(chinring: 52, cog: 11, wheel: Wheel.new(11).ee ).fog


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0