- 简单工厂:简单工厂的特点是基类提供静态的工厂函数。
基类提供抽象函数,实体类继承基类并重写抽象函数。
调用时,通过类名调用静态工厂类,通过形参决定调用哪一个实体类。 缺点是必须预先规划好实体类。
- 动态工厂:与简单工厂不同的地方是我们需要将工厂类单独分离出来。并且可以动态添加实体类对应的工厂类。
interface Shape {
void draw();
void erase();
}
工厂类
abstract class ShapeFactory {
//模板方法
protected abstract Shape create();
private static Map factories = new HashMap();
public static void addFactory(String id, ShapeFactory f) {
factories.put(id, f);
}
public static final Shape createShape(String id) {
if(!factories.containsKey(id)) {
try {
// Load dynamically
Class.forName("factory.shapefact2." + id);
} catch(ClassNotFoundException e) {
throw new RuntimeException(
"Bad shape creation: " + id);
}
// See if it was put in:
if(!factories.containsKey(id))
throw new RuntimeException(
"Bad shape creation: " + id);
}
return
((ShapeFactory)factories.get(id)).create();
}
}
square类
class Square implements Shape {
private Square() {}
public void draw() {
System.out.println("Square.draw");
}
public void erase() {
System.out.println("Square.erase");
}
private static class Factory
extends ShapeFactory {
protected Shape create() {
return new Square();
}
}
static {
ShapeFactory.addFactory(
"Square", new Factory());
}
}
ShapeFactory.createShape(“Square”));
- 抽象工厂
GameEnvironment通过构造函数形参判断实体类类型。
GameEnvironment是Player和obstacle的代理类。GameEnvironment调用player中的interactWith函数动态决定obstacle
接口
interface Obstacle {
void action();
}
interface Player {
void interactWith(Obstacle o);
}
player实体类
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();
}
}
Obstacle实体类
class Puzzle implements Obstacle {
public void action() {
System.out.println("Puzzle");
}
}
class NastyWeapon implements Obstacle {
public void action() {
System.out.println("NastyWeapon");
}
}
工厂类
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);
}
}