欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

设计模式之工厂模式

程序员文章站 2024-01-21 14:54:16
...

问题:写一个程序来创建一个迷宫,需要有房间、墙壁、门等。
1.写一个由两个房间和它们之间的门组成的小迷宫。
(1) 一个枚举类 描述方向

public enum Direction {
    NORTH,SOUTH,EAST,WEST;
}

(2) 相关类

public  abstract class  MapSite {
    abstract void enter();
}

房间

public class Room  extends MapSite{

    private int roomNo;
    private int [] sides=new int[4];
    
    public Room(int roomNo) {
        super();
        this.setRoomNo(roomNo);
    }

    @Override
    public void enter() {}

    public MapSite getSide(Direction direction) {
        return null;
        
    }
    public void setSide(Direction direction,MapSite mapSite) {
        
    }

    public int getRoomNo() {
        return roomNo;
    }

    public void setRoomNo(int roomNo) {
        this.roomNo = roomNo;
    }

    public int [] getSides() {
        return sides;
    }

    public void setSides(int [] sides) {
        this.sides = sides;
    }
    
}

public class Wall  extends  MapSite{
    
    @Override
    void enter() {
        
    }

}

public class Door  extends MapSite{

    private Room room1;
    private Room room2;
    private boolean isOpened;
    
    public Door(Room room1, Room room2) {
        super();
        this.setRoom1(room1);
        this.setRoom2(room2);
    }

    @Override
    void enter() {
        
    }
    
    public Room otherSideFrom(Room room) {
        return room;
    }

    public boolean isOpened() {
        return isOpened;
    }

    public void setOpened(boolean isOpened) {
        this.isOpened = isOpened;
    }

    public Room getRoom1() {
        return room1;
    }

    public void setRoom1(Room room1) {
        this.room1 = room1;
    }

    public Room getRoom2() {
        return room2;
    }

    public void setRoom2(Room room2) {
        this.room2 = room2;
    }

}

迷宫

public class Maze {
    private List<Room> rooms;

    public void addRoom(Room room) {
        rooms.add(room);
    }

}

(3)生成迷宫

public class MazeGame  {
    //生成一个迷宫
    public Maze createMaze() {
        Maze maze=new Maze();
        Room r1=new Room(0);
        Room r2=new Room(1);
        Door door=new Door(r1,r2);
        
        r1.setSide(Direction.NORTH, new Wall());
        r1.setSide(Direction.SOUTH, door);
        r1.setSide(Direction.EAST, new Wall());
        r1.setSide(Direction.WEST, new Wall());
        
        r2.setSide(Direction.NORTH, new Wall());
        r2.setSide(Direction.SOUTH, new Wall());
        r2.setSide(Direction.EAST, new Wall());
        r2.setSide(Direction.WEST, door);
        
        maze.addRoom(r1);
        maze.addRoom(r2);
        return maze;
    }
    
}

有个工厂模式之后

public class MazeFactory {
    public Maze makeMaze() {
        return new Maze();
    }
    
    public Wall makeWall() {
        return new Wall();
    }
    
    public Room makeRoom(int no) {
        return new Room(no);
    }
    
    public Door makeDoor(Room r1,Room r2) {
        return new Door(r1, r2);
    }
}
public class MazeGame {
  //生成一个迷宫
    public Maze createMaze(MazeFactory factory) {
        Maze maze=factory.makeMaze();
        Room r1=factory.makeRoom(0);
        Room r2=factory.makeRoom(1);
        Door door=factory.makeDoor(r1, r2);
        
        r1.setSide(Direction.NORTH, factory.makeWall());
        r1.setSide(Direction.SOUTH, door);
        r1.setSide(Direction.EAST, factory.makeWall());
        r1.setSide(Direction.WEST, factory.makeWall());
        
        r2.setSide(Direction.NORTH, factory.makeWall());
        r2.setSide(Direction.SOUTH, factory.makeWall());
        r2.setSide(Direction.EAST, factory.makeWall());
        r2.setSide(Direction.WEST, door);
        
        maze.addRoom(r1);
        maze.addRoom(r2);
        return maze;
    }
}

原先创建迷宫是一种硬编码,缺乏灵活性,试想你想写一个稍微复杂一点的迷宫,对原先的代码就要做很多的编码,改了这里改那里,写很多重复的代码。有了工厂模式之后,流水线生产,简单易操作。