抽象工厂模式(Abstract Factory)(对象创建型模式)

时间:2022-09-08 20:14:56

参考文档:

1.设计模式书籍

2.http://blog.csdn.NET/eson_15/article/details/51253161(【Java设计模式】之 抽象工厂(Abstract Factory)模式)

3.http://blog.csdn.net/hguisu/article/details/7505909(设计模式(一)工厂模式Factory(创建型))

4.http://blog.csdn.Net/zhshulin/article/details/38349211(设计模式——抽象工厂模式(Abstract Factory))


理论方面的知识,可以查看上面的  参考文档中的内容。


我们来看看Abstract Factory模式的结构:

抽象工厂模式(Abstract Factory)(对象创建型模式)

就长上面这样,看起来还是比较简单的。。。

下面,我们来看看书上说的迷宫的案例。

类图:

抽象工厂模式(Abstract Factory)(对象创建型模式)

实现之后,大概就是这个样子的吧。。。

代码实现:

MazeFactory.java:
package com.rick.designpattern.abstract_factory2;

/**
* Created by MyPC on 2017/6/20.
*/
public class MazeFactory {

public MazeFactory() {
}

public Maze makeMaxe() {
return new Maze();
}

public Wall makeWall() {
return new Wall();
}

public Room makeRoom(int n) {
return new Room(n);
}

public Door makeDoor(Room r1, Room r2) {
return new Door(r1, r2);
}
}
Maze.java:
package com.rick.designpattern.abstract_factory2;import java.util.ArrayList;import java.util.List;/** * 迷宫 * <p> * Created by MyPC on 2017/6/20. */public class Maze {    private List<Room> rooms;    public Maze() {        this.rooms = new ArrayList<Room>();    }    public void addRoom(Room room) {        rooms.add(room);    }}
Wall.java:
package com.rick.designpattern.abstract_factory2;/** * 墙 * * Created by MyPC on 2017/6/20. */public class Wall {}
Room.java:
package com.rick.designpattern.abstract_factory2;import java.util.HashMap;import java.util.Map;/** * 房间 * <p> * Created by MyPC on 2017/6/20. */public class Room {    private int n;    private Map<String, Object> wallAndRoomMap = new HashMap<String, Object>();    public Room(int n) {        this.n = n;    }    public void setSide(String direction, Object o) {        wallAndRoomMap.put(direction, o);    }}
Door.java:
package com.rick.designpattern.abstract_factory2;/** * 门 * <p> * Created by MyPC on 2017/6/20. */public class Door {    private Room r1, r2;    public Door(Room r1, Room r2) {        this.r1 = r1;        this.r2 = r2;    }}
EnchantedMazeFactory.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class EnchantedMazeFactory extends MazeFactory {    public EnchantedMazeFactory() {    }    @Override    public Room makeRoom(int n) {        return new EnchantedRoom(n, castSpell());    }    @Override    public Door makeDoor(Room r1, Room r2) {        return new DoorNeedingSpell(r1, r2);    }    private Spell castSpell() {        return new Spell();    }}
EnchantedRoom.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class EnchantedRoom extends Room {    private Spell spell;    public EnchantedRoom(int n, Spell spell) {        super(n);        this.spell = spell;    }}
Spell.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class Spell {}
DoorNeedingSpell.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class DoorNeedingSpell extends Door {    public DoorNeedingSpell(Room r1, Room r2) {        super(r1, r2);    }}
BombedMazeFactory.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class BombedMazeFactory extends MazeFactory {    @Override    public Wall makeWall() {        return new BombedWall();    }    @Override    public Room makeRoom(int n) {        return new RoomWithABomb(n);    }}
BombedWall.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class BombedWall extends Wall {}
RoomWithABomb.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class RoomWithABomb extends Room {    public RoomWithABomb(int n) {        super(n);    }}
MazeGame.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class MazeGame {    public Maze createMaze(MazeFactory factory) {        Maze maze = factory.makeMaxe();        Room r1 = factory.makeRoom(1);        Room r2 = factory.makeRoom(2);        Door door = factory.makeDoor(r1, r2);        maze.addRoom(r1);        maze.addRoom(r2);        r1.setSide("north", factory.makeWall());        r1.setSide("east", door);        r1.setSide("south", factory.makeWall());        r1.setSide("west", factory.makeWall());        r2.setSide("north", factory.makeWall());        r2.setSide("east", factory.makeWall());        r2.setSide("south", factory.makeWall());        r2.setSide("west", door);        return maze;    }}
Client.java:
package com.rick.designpattern.abstract_factory2;/** * Created by MyPC on 2017/6/20. */public class Client {    public static void main(String[] args) {        MazeGame mazeGame = new MazeGame();        BombedMazeFactory bombedMazeFactory = new BombedMazeFactory();        mazeGame.createMaze(bombedMazeFactory);    }}