-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
・5.4 オブザーバとしてのコードブロック EFROR-62
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module DesignPattern | ||
module Observer | ||
class Employee3 | ||
include Subject3 | ||
attr_reader :name | ||
attr_accessor :title, :salary | ||
|
||
def initialize(name, title, salary) | ||
super() | ||
@name = name | ||
@title = title | ||
@salary = salary | ||
@observers = [] | ||
end | ||
|
||
def salary=(new_salary) | ||
@salary = new_salary | ||
notify_observers | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module DesignPattern | ||
module Observer | ||
module Subject3 | ||
def initialize | ||
@observers = [] | ||
end | ||
|
||
def add_observer(&observer) | ||
@observers << observer | ||
end | ||
|
||
def delete_observer(observer) | ||
@observers.delete(observer) | ||
end | ||
|
||
def notify_observers | ||
@observers.each do |observer| | ||
observer.call(self) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters