Odkazy
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Poskytuje/zavádí rozhraní/interface pro tvorbu rodiny souvisejících nebo závislých objektů bez specifikace jejich konkrétních tříd.
Ruby automatically implements
Příklad 59.1. Abstract Factory Pattern
class Foo; end
class Bar; end
# Here is the use of the Abstract Factory pattern
def create_something( factory )
new_object = factory.new
puts "created a new #{new_object.type} with a factory"
end
# Here we select a factory to use
create_something( Foo )
create_something( Bar )
Příklad 59.2. Abstract Factory Pattern
def create_something_with_block
new_object = yield
puts "created a new #{new_object.type} with a block"
end
def create_something_with_proc( &proc )
new_object = proc.call
puts "created a #{new_object.type} with a proc"
end
create_something_with_block { Foo.new }
create_something_with_block { Bar.new }
create_something_with_proc { Foo.new }
create_something_with_proc { Bar.new }
