抽象工厂模式(Abstract Factory)

名称: 抽象工厂模式(abstract factory)
别名: Kit
意图: 提供一个[b]创建一系列相关或相互依赖对象的接口[/b], 而无需指定它们具体的类.
动机: 考虑一个支持多种视感(look-and-feel)标准的用户界面工具包.
不同的视感风格为诸如滚动条、窗口和按钮等用户界面"窗口组件"定义不同的外观和行为.
为保证视感风格标准间的可移植性, 一个应用不应该为一个特定的视感外观硬编码它的窗口组件.
在整个应用中实例化特定视感风格的窗口组件类将使得以后很难改变视感风格.


class AbstractTable
def render
raise 'abstract method'
end
end

class SquareCornerTable < AbstractTable
def initialize ; end

def render
puts '创建方角桌'
end
end

class RoundCornerTable < AbstractTable
def initialize ; end

def render
puts '创建圆角桌'
end
end

# ------------------------------------

class AbstractFoot
end

class SimpleFoot < AbstractFoot
def initialize ; end

def render
puts '创建单脚'
end
end

class EightShapeFoot < AbstractFoot
def initialize ; end

def render
puts '创建八字脚'
end
end

# ------------------------------------

class AbstractFactory
def create_table
raise 'abstract method'
end

def create_foot
raise 'abstract method'
end
end

class AFactory < AbstractFactory
def create_table
SquareCornerTable.new.render
end

def create_foot
SimpleFoot.new.render
end
end

class BFactory < AbstractFactory
def create_table
RoundCornerTable.new.render
end

def create_foot
EightShapeFoot.new.render
end
end

# ------------------------------------

class Client
def self.run
puts 'A 工厂'
a_factory = AFactory.new
a_factory.create_table
a_factory.create_foot

puts 'B 工厂'
b_factory = BFactory.new
b_factory.create_table
b_factory.create_foot
end
end

Client.run


输出结果:

A 工厂
创建方角桌
创建单脚
B 工厂
创建圆角桌
创建八字脚


[img]http://dl.iteye.com/upload/attachment/189006/d45d9d04-66a9-378f-92bd-0286e7e861a8.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值