抽象工厂(Abstract factories)

抽象工厂(abstract factory)模式看起来很像前面我们看到的那些factory对象,只不过它有多个而不是一个factory方法。每一个factory 方法创建一个不同类型的对象。基本思想是:在创建工厂对象的地方,由你来决定如何使用该工厂对象创建的那些对象。《设计模式》里给出的例子实现了在不同用 户图形界面(GUIs)之间的可移植性:你根据自己使用的GUI来创建一个与之对应的factory对象,在这以后,当你需要用到菜单,按钮,滚动条这些 东西的时候,它会根据你使用的GUI自动创建合适的对象。这样,你就可以把实现不同GUI之间切换的代码分离出来,使它集中在一个地方。
作为另外一个例子,假设你要创建一个通用的游戏环境,而且你还想要支持不同类型的游戏。下面的例子用抽象工厂给出了它的一种可能的实现。
//: factory:Games.java


// An example of the Abstract Factory pattern.
package factory;
import junit.framework.*;
interface Obstacle {
void action();
}
interface Player {
void interactWith(Obstacle o);
}
class Kitty implements Player {
public void interactWith(Obstacle ob) {
System.out.print("Kitty has encountered a ");
ob.action();
}
}
class KungFuGuy implements Player {
public void interactWith(Obstacle ob) {
System.out.print("KungFuGuy now battles a ");
ob.action();
}
}
class Puzzle implements Obstacle {
public void action() {System.out.println("Puzzle");
}
}
class NastyWeapon implements Obstacle {
public void action() {
System.out.println("NastyWeapon");
}
}
// The Abstract Factory:
interface GameElementFactory {
Player makePlayer();
Obstacle makeObstacle();
}
// Concrete factories:
class KittiesAndPuzzles implements GameElementFactory {
public Player makePlayer() {
return new Kitty();
}
public Obstacle makeObstacle() {
return new Puzzle();
}
}
class KillAndDismember implements GameElementFactory {
public Player makePlayer() {
return new KungFuGuy();
}
public Obstacle makeObstacle() {
return new NastyWeapon();
}
}
class GameEnvironment {
private GameElementFactory gef;
private Player p;
private Obstacle ob;
public GameEnvironment( GameElementFactory factory) {
gef = factory;
p = factory.makePlayer();
ob = factory.makeObstacle();
}
public void play() { p.interactWith(ob); }
}
public class Games extends TestCase {
GameElementFactory
kp = new KittiesAndPuzzles(),
kd = new KillAndDismember();
GameEnvironment
g1 = new GameEnvironment(kp),
g2 = new GameEnvironment(kd);
// These just ensure no exceptions are thrown:
public void test1() { g1.play(); }
public void test2() { g2.play(); }
public static void main(String args[]) {
junit.textui.TestRunner.run(Games.class);
}
}
在 上面的游戏环境里,Player对象与Obstale对象交互,根据你所选择的游戏类型,player和obstacle的各自类型也会不同。你通过选择 某个特定的GameElementFactory来决定游戏的类型,然后GameElementFactory会控制初始化和游戏的进行。在上面的例子 里,初始化和游戏的进行都非常简单,但那些活动(初始条件和状态变化)可以在很大程度上决定游戏的结局。这里,GameEnvironment不是用来给 其它类继承的 ,尽管那么做很可能也说的通。
上面的代码也包含了双重分派(Double Dispatching)和工厂方法(Factory Method)两种模式,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Laravel 的抽象工厂设计模式代码看起来大致像这样: ``` <?php namespace App\Factories; use App\Contracts\Shape; interface ShapeFactory { public function makeCircle(): Shape; public function makeRectangle(): Shape; public function makeSquare(): Shape; } class CircleFactory implements ShapeFactory { public function makeCircle(): Shape { return new Circle; } public function makeRectangle(): Shape { throw new \Exception('This factory can only make circles.'); } public function makeSquare(): Shape { throw new \Exception('This factory can only make circles.'); } } class RectangleFactory implements ShapeFactory { public function makeCircle(): Shape { throw new \Exception('This factory can only make rectangles.'); } public function makeRectangle(): Shape { return new Rectangle; } public function makeSquare(): Shape { throw new \Exception('This factory can only make rectangles.'); } } class SquareFactory implements ShapeFactory { public function makeCircle(): Shape { throw new \Exception('This factory can only make squares.'); } public function makeRectangle(): Shape { throw new \Exception('This factory can only make squares.'); } public function makeSquare(): Shape { return new Square; } } ``` 在这个例子中,我们定义了一个 `ShapeFactory` 接口和三个具体的工厂类,分别是 `CircleFactory`、`RectangleFactory` 和 `SquareFactory`。这三个工厂类都实现了 `ShapeFactory` 接口,并定义了如何创建圆形、矩形和正方形。 使用这些工厂类的方式如下: ``` $circleFactory = new CircleFactory; $circle = $circleFactory->makeCircle(); $rectangleFactory = new RectangleFactory; $rectangle = $rectangleFactory->makeRectangle(); $squareFactory = new SquareFactory; $square = $squareFactory->makeSquare(); ``` 抽象工厂模式是一种创建型设计模式,它通过定义一个抽象工厂接口来创建一系列相关的产品,而不需要指定它

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值