我是靠谱客的博主 舒服航空,最近开发中收集的这篇文章主要介绍抽象工厂模式(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]

最后

以上就是舒服航空为你收集整理的抽象工厂模式(Abstract Factory)的全部内容,希望文章能够帮你解决抽象工厂模式(Abstract Factory)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部