SHARE
TWEET

Untitled

a guest Jun 19th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # State
  2. module State
  3.   def do_use(context)
  4.     raise NotImplementedError.new("#{self.class}##{__method__} が実装されていません")
  5.   end
  6.  
  7.   def do_alarm(context)
  8.     raise NotImplementedError.new("#{self.class}##{__method__} が実装されていません")
  9.   end
  10.  
  11.   def do_phone(context)
  12.     raise NotImplementedError.new("#{self.class}##{__method__} が実装されていません")
  13.   end
  14. end
  15.  
  16. # Context
  17. module Context
  18.   def record_log(message)
  19.     raise NotImplementedError.new("#{self.class}##{__method__} が実装されていません")
  20.   end
  21.  
  22.   def call_security_center(message)
  23.     raise NotImplementedError.new("#{self.class}##{__method__} が実装されていません")
  24.   end
  25. end
  26.  
  27. # ConcreteState
  28. class DayState
  29.   include State
  30.  
  31.   @singleton = self.new
  32.  
  33.   class << self
  34.     def get_instance
  35.       @singleton
  36.     end
  37.   end
  38.  
  39.   def do_use(context)
  40.     context.record_log('金庫使用(昼間)')
  41.   end
  42.  
  43.   def do_alarm(context)
  44.     context.call_security_center('非常ベル(昼間)')
  45.   end
  46.  
  47.   def do_phone(context)
  48.     context.call_security_center('通話(昼間)')
  49.   end
  50. end
  51.  
  52. # ConcreteState
  53. class NightState
  54.   include State
  55.  
  56.   @singleton = self.new
  57.  
  58.   class << self
  59.     def get_instance
  60.       @singleton
  61.     end
  62.   end
  63.  
  64.   def do_use(context)
  65.     context.call_security_center('非常ベル(夜間)')
  66.   end
  67.  
  68.   def do_alarm(context)
  69.     context.call_security_center('非常ベル(夜間)')
  70.   end
  71.  
  72.   def do_phone(context)
  73.     context.call_security_center('通話(夜間)')
  74.   end
  75. end
  76.  
  77. # ConcreteContext
  78. class SafeguardSystem
  79.   include Context
  80.  
  81.   def initialize(hour)
  82.     @state = change_state(hour)
  83.   end
  84.  
  85.   def set_clock(hour)
  86.     change_state(hour)
  87.   end
  88.  
  89.   def switch_use
  90.     @state.do_use(self)
  91.   end
  92.  
  93.   def switch_alarm
  94.     @state.do_alarm(self)
  95.   end
  96.  
  97.   def switch_phone
  98.     @state.do_phone(self)
  99.   end
  100.  
  101.   # mediator pattern
  102.   def change_state(hour)
  103.     if hour >= 9 && hour < 17
  104.       @state = DayState.get_instance
  105.     else
  106.       @state = NightState.get_instance
  107.     end
  108.   end
  109.  
  110.   def record_log(message)
  111.     puts "record_log: #{message}"
  112.   end
  113.  
  114.   def call_security_center(message)
  115.     puts "call_security_center: #{message}"
  116.   end
  117. end
  118.  
  119. # main
  120. safeguard_system = SafeguardSystem.new(0)
  121.  
  122. 24.times do |hour|
  123.   safeguard_system.set_clock(hour)
  124.   safeguard_system.switch_use
  125.   safeguard_system.switch_alarm
  126.   safeguard_system.switch_phone
  127. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top