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

FactoryMethod その3

IDCardとIDCardFactoryは強い依存関係にある。 しかも型が無いため抽象クラスのようなものは必要ない。 それらを考慮するとこうしてもいいかも。 module FactoryMethod module Factory def create(*arg) product = new(*arg) puts product.owner product end…

言語の宗教戦争

なにやら最近盛り上がってますね。 結局のところ好き嫌いあるのが当たり前なんだし、 争うだけ無駄ですよね。でも、それぞれの意見を 聞いてみるのは楽しいと思う。 ただ、PHPしかできない人がPHPをバカにされて、 反論するのはどうかと思う。その他の技術も…

Factory Method その2

module FactoryMethod class IDCard def initialize(str) @owner = str end def use "#{@owner}のカード" end end module Factory def create(*arg) product(*arg) end end class IDCardFactory include Factory def product(str) IDCard.new(str) end end e…

Factory Method その1

単純移植 module FactoryMethod class IDCard def initialize(str) @owner = str end def use "#{@owner}のカード" end end class Factory def create(*arg) create_product(*arg) end end class IDCardFactory < Factory def create_product(str) IDCard.ne…

Template Method その5

Template Method その3のモジュールを使えば、 Enumerableモジュールと同じように、こんな事もできますね。 obj = Object.new def obj.open puts "open!" end def obj.close puts "close!" end def obj.temp [:open, :close] end obj.extend(TemplateMethod:…

Template Method その4

module TemplateMethod2 class Child def open print "<" end def disp(&block) open block.call close end def close puts ">" end end end TemplateMethod2::Child.new.disp { print "H" * 5 } 初めと意味が変わってしまいましたが、 これもTemplate Metho…

Template Method その3

module TemplateMethod module Display def disp temp.each { |sym| __send__(sym) } end end class Parent include Display def temp a = [] a << :open a << [:_print] * 5 a << :close a.flatten end end class Child < Parent def initialize(str) @str …

Template Method その2

module TemplateMethod class Display def self.disp(procs) procs[:open].call procs[:_print].call procs[:close].call end end end def char_procs(str) { :open => proc { print "<" }, :_print => proc { print str * 5 }, :close => proc { puts ">" }…

Template Method その1

1つめのサンプルを、単純に移植。 module TemplateMethod class AbstractDisplay def disp open 5.times { _print } close end end class CharDisplay < AbstractDisplay def initialize(str) @str = str end def open print "<" end def _print print @str …

Adapter その2

標準ライブラリを使ってみました。 require "forwardable" class PrintBanner extend Forwardable def initialize(str) @banner = Banner.new(str) end def_delegator :@banner, :show_with_paren, :print_weak def_delegator :@banner, :show_with_aster, :…

Adapter その1

所謂ラッパーですね。まずは単純に移植。継承の場合 module Adapter class Banner def initialize(str) @str = str end def show_with_paren puts "(#{@str})" end def show_with_aster puts "*#{@str}*" end end class PrintBanner < Banner def print_weak…

Iterator その2

Rubyっぽく、まず思いついたのはこんな感じです。 module Iterator2 class Book def initialize(name) @name = name end attr_reader :name end class BookShelf def initialize @books = [] end def append(book) @books << book end def each(&block) @boo…

Iterator その1

まずはIterator。サンプルプログラムを単純に移植してみます。 module Iterator class Book def initialize(name) @name = name end attr_reader :name end class BookShelf def initialize @books = [] @length = 0 end def [](i) @books[i] end def append…

ことはじめ

単純に移植するだけなら簡単だが、いかにRubyらしく、そもそも、 Rubyならもっと素敵に実装できるのかも?をテーマに、 Rubyなりのデザインパターン実装を考えてみようと思います。結城先生の「Java言語で学ぶデザインパターン入門」の サンプルプログラムを…

Rubyソースコード完全解説

を読んでいます。gc.c (obj->as.basic.flags & FL_MARK) こんなソースを見かけるが、どんな意味があるんだろう? flagsのままじゃダメなのかな? FL_MARK(0x3f)より上位ビットをどうにかするため? (0x40)で割った時の余り? どんな状況なんだろ?プログラム…