-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mori Zyun
committed
Dec 9, 2012
0 parents
commit f93a45a
Showing
28 changed files
with
25,330 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,5 @@ | ||
./data* | ||
file* | ||
./chap* | ||
./chap15/* | ||
chap15/* |
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,68 @@ | ||
class Report | ||
def initialize | ||
@title = "html report title" | ||
@text = ["report line 1", "report line 2", "report line 3"] | ||
end | ||
|
||
def output_report | ||
output_start | ||
output_body | ||
output_end | ||
end | ||
|
||
def output_start | ||
end | ||
|
||
def output_body | ||
@text.each do |line| | ||
output_line(line) | ||
end | ||
end | ||
|
||
def output_line(line) | ||
raise 'Called abstract method !!' | ||
end | ||
|
||
def output_end | ||
end | ||
end | ||
|
||
class HTMLReport < Report | ||
def output_start | ||
puts "<html><head><title>#{@title}</title></head><body>" | ||
end | ||
|
||
def output_line(line) | ||
puts "<p>#{line}</p>" | ||
end | ||
|
||
def output_end | ||
puts '</body></html>' | ||
end | ||
end | ||
|
||
class PlaneTextReport < Report | ||
def output_start | ||
puts "**** #{@title} ****" | ||
end | ||
|
||
def output_line(line) | ||
puts line | ||
end | ||
end | ||
|
||
# =========================================== | ||
html_report = HTMLReport.new | ||
html_report.output_report | ||
#<html><head><title>html report title</title></head><body> | ||
#<p>report line 1</p> | ||
#<p>report line 2</p> | ||
#<p>report line 3</p> | ||
#</body></html> | ||
|
||
plane_text_report = PlaneTextReport.new | ||
plane_text_report.output_report | ||
#**** html report title **** | ||
#report line 1 | ||
#report line 2 | ||
#report line 3 |
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,51 @@ | ||
class Report | ||
attr_reader :title, :text | ||
attr_accessor :formatter | ||
|
||
def initialize(formatter) | ||
@title = 'report title' | ||
@text = %w(text1 text2 text3) | ||
@formatter = formatter | ||
end | ||
|
||
def output_report | ||
@formatter.output_report(self) | ||
end | ||
end | ||
|
||
class Formatter | ||
def output_report(title, text) | ||
raise 'Called abstract method !!' | ||
end | ||
end | ||
|
||
class HTMLFormatter < Formatter | ||
def output_report(report) | ||
puts "<html><head><title>#{report.title}</title></head><body>" | ||
report.text.each { |line| puts "<p>#{line}</p>" } | ||
puts '</body></html>' | ||
end | ||
end | ||
|
||
class PlaneTextFormatter < Formatter | ||
def output_report(report) | ||
puts "***** #{report.title} *****" | ||
report.text.each { |line| puts(line) } | ||
end | ||
end | ||
|
||
# =========================================== | ||
report = Report.new(HTMLFormatter.new) | ||
report.output_report | ||
#<html><head><title>report title</title></head><body> | ||
#<p>text1</p> | ||
#<p>text2</p> | ||
#<p>text3</p> | ||
#</body></html> | ||
|
||
report.formatter = PlaneTextFormatter.new | ||
report.output_report | ||
#***** report title ***** | ||
#text1 | ||
#text2 | ||
#text3 |
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,41 @@ | ||
class Report | ||
attr_reader :title, :text | ||
attr_accessor :formatter | ||
|
||
def initialize(&formatter) | ||
@title = 'report title' | ||
@text = %w(text1 text2 text3) | ||
@formatter = formatter | ||
end | ||
|
||
def output_report | ||
@formatter.call(self) | ||
end | ||
end | ||
|
||
HTML_FORMATTER = lambda do |context| | ||
puts "<html><head><title>#{context.title}</title></head><body>" | ||
context.text.each { |line| puts "<p>#{line}</p>" } | ||
puts '</body></html>' | ||
end | ||
|
||
PLANE_TEXT_FORMATTER = lambda do |context| | ||
puts "***** #{context.title} *****" | ||
context.text.each { |line| puts(line) } | ||
end | ||
|
||
# =========================================== | ||
report = Report.new(&HTML_FORMATTER) | ||
report.output_report | ||
#<html><head><title>report title</title></head><body> | ||
#<p>text1</p> | ||
#<p>text2</p> | ||
#<p>text3</p> | ||
#</body></html> | ||
|
||
report.formatter = PLANE_TEXT_FORMATTER | ||
report.output_report | ||
#***** report title ***** | ||
#text1 | ||
#text2 | ||
#text3 |
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,42 @@ | ||
# -*- coding: utf-8 -*- | ||
require 'observer' | ||
class Employee | ||
include Observable | ||
|
||
attr_reader :name, :address, :salary | ||
|
||
def initialize(name, title, salary) | ||
@title = name | ||
@title = title | ||
@salary = salary | ||
add_observer(Payroll.new) | ||
add_observer(TaxMan.new) | ||
end | ||
|
||
def salary=(new_salary) | ||
@salary = new_salary | ||
changed | ||
notify_observers(self) | ||
end | ||
end | ||
|
||
class Payroll | ||
def update(changed_employee) | ||
puts "彼の給料は#{changed_employee.salary}になりました!#{changed_employee.title}のために新しい小切手を切ります。" | ||
end | ||
end | ||
|
||
class TaxMan | ||
def update(changed_employee) | ||
puts "#{changed_employee.title}に新しい税金の請求書を送ります" | ||
end | ||
end | ||
|
||
# =========================================== | ||
john = Employee.new('John', 'Senior Vice President', 5000) | ||
john.salary = 6000 | ||
john.salary = 7000 | ||
#彼の給料は6000になりました!Johnのために新しい小切手を切ります。 | ||
#Johnに新しい税金の請求書を送ります | ||
#彼の給料は7000になりました!Johnのために新しい小切手を切ります。 | ||
#Johnに新しい税金の請求書を送ります |
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,78 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Component | ||
class Entry | ||
def get_name; end | ||
|
||
def ls_entry(prefix) end | ||
|
||
def remove; end | ||
end | ||
|
||
# Leaf (中身) | ||
class FileEntry < Entry | ||
def initialize(name) | ||
@title = name | ||
end | ||
|
||
def get_name | ||
@title | ||
end | ||
|
||
def ls_entry(prefix) | ||
puts(prefix + "/" + get_name) | ||
end | ||
|
||
def remove | ||
puts @title + "を削除しました" | ||
end | ||
end | ||
|
||
# Composite | ||
class DirEntry < Entry | ||
def initialize(name) | ||
@title = name | ||
@directory = Array.new | ||
end | ||
|
||
def get_name | ||
@title | ||
end | ||
|
||
def add(entry) | ||
@directory.push(entry) | ||
end | ||
|
||
def ls_entry(prefix) | ||
puts(prefix + "/" + get_name) | ||
@directory.each do |e| | ||
e.ls_entry(prefix + "/" + @title) | ||
end | ||
end | ||
|
||
def remove | ||
@directory.each do |i| | ||
i.remove | ||
end | ||
puts @title + "を削除しました" | ||
end | ||
end | ||
|
||
# =========================================== | ||
root = DirEntry.new("root") | ||
tmp = DirEntry.new("tmp") | ||
tmp.add(FileEntry.new("conf")) | ||
tmp.add(FileEntry.new("data")) | ||
root.add(tmp) | ||
|
||
root.ls_entry("") | ||
|
||
root.remove | ||
#/root | ||
#/root/tmp | ||
#/root/tmp/conf | ||
#/root/tmp/data | ||
#confを削除しました | ||
#dataを削除しました | ||
#tmpを削除しました | ||
#rootを削除しました |
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,68 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# 集約オブジェクト中の要素 | ||
class Article | ||
def initialize(title) | ||
@title = title | ||
end | ||
|
||
attr_reader :title | ||
end | ||
|
||
# 集約オブジェクト | ||
class Blog | ||
def initialize | ||
@articles = [] | ||
end | ||
|
||
def get_article_at(index) | ||
@articles[index] | ||
end | ||
|
||
def add_article(article) | ||
@articles << article | ||
end | ||
|
||
def length | ||
@articles.length | ||
end | ||
|
||
def iterator | ||
BlogIterator.new(self) | ||
end | ||
end | ||
|
||
# 外部イーテレータ | ||
class BlogIterator | ||
def initialize(blog) | ||
@blog = blog | ||
@index = 0 | ||
end | ||
|
||
def has_next? | ||
@index < @blog.length | ||
end | ||
|
||
def next_article | ||
article = self.has_next? ? @blog.get_article_at(@index) : nil | ||
@index = @index + 1 | ||
article | ||
end | ||
end | ||
|
||
# =========================================== | ||
blog = Blog.new | ||
blog.add_article(Article.new("デザインパターン1")) | ||
blog.add_article(Article.new("デザインパターン2")) | ||
blog.add_article(Article.new("デザインパターン3")) | ||
blog.add_article(Article.new("デザインパターン4")) | ||
|
||
iterator = blog.iterator | ||
while iterator.has_next? | ||
article = iterator.next_article | ||
puts article.title | ||
end | ||
#デザインパターン1 | ||
#デザインパターン2 | ||
#デザインパターン3 | ||
#デザインパターン4 |
Oops, something went wrong.