2020-05-01から1ヶ月間の記事一覧

rubyでデザインパターン simple_factoryパターン

module Door def getWidth end def getHeight end end class WoodenDoor include Door attr_reader :width, :height def initialize(width, height) @width = width @height = height end def name "木製のドアです。" end end class DoorFactory def self.m…

rubyでデザインパターン commandパターン

class Bulb def turnOn puts "電気がつきました" end def turnOff puts "真っ暗です" end end class Command def execute end def undo end def redo end end class TurnOn < Command def initialize(bulb) @bulb = bulb end def execute @bulb.turnOn end d…

オブジェクト指向

「人の認識に近い形で」というところが一般的に言われる「責務に応じた形で」と同じニュアンスです。

rubyでデザインパターン compositeパターン

あるオブジェクトの集合で構成されたオブジェクトも同一のインターフェースをもつパターンです。 class Employee attr_reader :salary, :name def initialize(name, salary) @name = name @salary = salary end end class Developer < Employee attr_reader …

rubyでデザインパターン adapterパターン

ライオンを狩るハンタークラスがあったのですが 急遽、野犬も狩るように言われてしまいました。 しかし、野犬はライオンとは別のインターフェイスを持っているので このままではうまく使えません。 そこで野犬クラスを包み込んだようなadapterクラスを用意し…

rubyでデザインパターン decoratorパターン

class Coffee #価格 def getCost end #商品説明 def getDescription end end class SimpleCoffee < Coffee def getCost 10 end def getDescription 'Simple coffee' end end class MilkCoffee < Coffee def initialize(coffee) @coffee = coffee end def get…

Rubyでデザインパターン proxyパターン

class Door def open puts 'open door!' end def close puts 'close door!' end end class SecurityDoor def initialize(door) @door = door end def open(password) if password == 'open goma!' @door.open else puts 'password fail' end end def close(p…

Rubyによるオブジェクト指向 8章 コードサンプル

attr_reader :size, :parts def initialize(args={}) @size = args[:size] @parts = args[:parts] end def spares parts.spares end end class Part attr_reader :name, :description, :needs_spares def initialize(args) @name = args[:name] @description…