# 抽象ファクトリーインターフェース
class UIFactory
def create_button
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
def create_checkbox
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
end
# 具体的なファクトリー
class WinFactory < UIFactory
def create_button
WinButton.new
end
def create_checkbox
WinCheckbox.new
end
end
class MacFactory < UIFactory
def create_button
MacButton.new
end
def create_checkbox
MacCheckbox.new
end
end
# 抽象製品
class Button
def paint
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
end
class Checkbox
def paint
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
end
# 具体的な製品
class WinButton < Button
def paint
puts 'Render a button in Windows style.'
end
end
class MacButton < Button
def paint
puts 'Render a button in macOS style.'
end
end
class WinCheckbox < Checkbox
def paint
puts 'Render a checkbox in Windows style.'
end
end
class MacCheckbox < Checkbox
def paint
puts 'Render a checkbox in macOS style.'
end
end
# クライアントコード
def create_ui(factory)
button = factory.create_button
checkbox = factory.create_checkbox
button.paint
checkbox.paint
end
# 使用例
windows_factory = WinFactory.new
mac_factory = MacFactory.new
create_ui(windows_factory)
create_ui(mac_factory)