抽象工厂模式

意图

抽象工厂模式提供一系列相关或者相互依赖的对象的接口,而无需指定它们具体的类。客户类不需要直接构建对象,它会调用该接口提供的方法。

实现

下面是一个具体的实现。

class AbstractMazeFactory
  def make_maze
    raise NotImplementedError, "You should implement this method"
  end

  def make_wall
    raise NotImplementedError, "You should implement this method"
  end

  def make_room
    raise NotImplementedError, "You should implement this method"
  end
end

class MazeFactory < AbstractMazeFactory
   def make_maze
     Maze.new
   end

   def make_wall wall
     wall.camelize.constantlize.new
   end

   def make_room
     Room.new
   end
end

class Room
  def room_number
    p "i am room number 1"
  end
end

class Maze
  def maze
    p "maze builded complete"
  end
end

class AbstractWall
  def feature
    raise NotImplementedError, "You should implement this method"
  end
end

class IronWall < AbstractWall
  def feature
    p "I am an Iron Wall"
  end
end

class BrickWall < AbstractWall
  def feature
    p "I am a Brick Wall"
  end
end

class ClayWall < AbatractWall
  def feature
    p "I am a Clay Wall"
  end
end

class Client
  def make_maze
    maze_factory = MazeFactory.new
    room = maze_factory.make_room
    room.room_number
    iron_wall = maze_factory.make_wall "iron_wall"
    iron_wall.feature
    brick_wall = maze_factory.make_wall "brick_wall"
    brick_wall.feature
    clay_wall =  maze_factory.make_wall "clay_wall"
    clay_wall.feature
    maze = maze_factory.make_maze
    maze.maze
  end
end

在上面的实现中,我们首先中创建一个AbstractMazeFactory,其中定义了必须被子类复写的方法。然后创建一个MazeFactory类,
复写父类所有的方法,并返回Room,MazeWall子类的实例。最后利用Client类构造出一个迷宫。

适用性

  • 一个系统要独立于它的产品的创建、组合或者实现时。
  • 一个系统要由多个产品系列中的一个来配置时。
  • 当你强调一系列相关的产品的对象的设计以便进行联合使用时。
  • 当你提供一个产品类库,而只想显示它的接口而不是实现时。

优缺点

  • 它分离了具体的类。
  • 它使得易于交换产品序列。
  • 它有利于产品的一致性。
  • 它难以支持新种类的产品

参考文献: