Factory is used to integrate the creation of some related object into one responsory class:
class PondOrganismFactory
def new_animal(name)
Frog.new(name)
end
def new_plant(name)
Algae.new(name)
end
end
class JungleOrganismFactory
def new_animal(name)
Tiger.new(name)
end
def new_plant(name)
Tree.new(name)
end
end
In ruby the class is an object too, you can create factory by class name
class IOFactory
def initialize(format)
@reader_class = self.class.const_get("#{format}Reader")
@writer_class = self.class.const_get("#{format}Writer")
end
def new_reader
@reader_class.new
end
def new_writer
@writer_class.new
end
end
html_factory = IOFactory.new('HTML')
html_reader = html_factory.new_reader
pdf_factory = IOFactory.new('PDF')
pdf_writer = pdf_factory.new_writer
分享到:
相关推荐
Design Patterns in Ruby, Addison-wesley (2008). The Addison-Wesley Professional Ruby Series provides readers with practical, people-oriented, and in-depth information about applying the Ruby platform ...
Addison.Wesley.Design.Patterns.in.Ruby.Dec.2007 高清PDF英文版
Design Patterns in Ruby Dec 2007.rar
design pattern in C# language
Design Patterns in Modern C++.pdf
Introduction to Design Patterns in C With Qt4 Alan Ezust
Design Patterns in Modern C++,Design Patterns in Modern C++,Design Patterns in Modern C++
Too often design patterns are explained using tricky concepts, when in fact they are easy to use and can enrich your everyday development. Design Patterns in ...
Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....
Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...
An introduction to Design Patterns in C++ with Qt 2nd Edition 中文版 C++ qt 设计模式 第二版, 中英文版 合集 Master C++ “The Qt Way” with Modern Design Patterns and Efficient Reuse This fully ...
Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...
Implement design patterns in .NET using the latest versions of the C# and F# languages. This book provides a comprehensive overview of the field of design patterns as they are used in today’s ...
Design Patterns by Tutorials Learning design patterns in Swift 4.2, 2nd Edition.pdf
App Architecture: iOS Application Design Patterns in Swift 包含Source code 有钱请支持正版 没钱请默默学习 原书地址: https://www.objc.io/books/app-architecture 中文原书地址: ...
Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm
Design Patterns in .NET .net设计模式 2019年最新版