11.20

实验13的黑白五子棋结果如下
1.+----------------+ +---------------------+ +-------------------+
| IgoChessman | | IgoChessmanFactory | | Client |
+----------------+ +---------------------+ +-------------------+
| + getColor() | | - blackChess: | | |
| + display(pos) | | BlackIgoChessman | | |
+----------------+ | - whiteChess: | | |
^ | WhiteIgoChessman | | |
| | + getIgoChessman() | | |
| +---------------------+ +-------------------+
+----------------+ ^
| BlackIgoChessman| |
+----------------+ |
| + getColor() | |
| + display(pos) | |
+----------------+ Simple Factory Pattern
^ &
+----------------+ Singleton Pattern
| WhiteIgoChessman|
+----------------+
| + getColor() |
| + display(pos) |
+----------------+
2.
// 享元接口
public interface IgoChessman {
String getColor();
void display(Position position);
}

// 具体享元类 - 黑棋
public class BlackIgoChessman implements IgoChessman {
@Override
public String getColor() {
return "黑色";
}

@Override
public void display(Position position) {
    System.out.println("黑棋落在位置:(" + position.getX() + ", " + position.getY() + ")");
}

}

// 具体享元类 - 白棋
public class WhiteIgoChessman implements IgoChessman {
@Override
public String getColor() {
return "白色";
}

@Override
public void display(Position position) {
    System.out.println("白棋落在位置:(" + position.getX() + ", " + position.getY() + ")");
}

}

// 外部状态 - 位置信息
public class Position {
private int x;
private int y;

public Position(int x, int y) {
    this.x = x;
    this.y = y;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

@Override
public String toString() {
    return "(" + x + ", " + y + ")";
}

}

// 享元工厂类(使用简单工厂模式和单例模式)
public class IgoChessmanFactory {
// 单例实例
private static IgoChessmanFactory instance = new IgoChessmanFactory();

// 享元对象池
private static final Map<String, IgoChessman> chessmanMap = new HashMap<>();

// 私有构造函数
private IgoChessmanFactory() {
    // 初始化时创建黑白棋子对象
    chessmanMap.put("black", new BlackIgoChessman());
    chessmanMap.put("white", new WhiteIgoChessman());
    System.out.println("享元工厂初始化完成,创建了黑白棋子对象");
}

// 获取单例实例
public static IgoChessmanFactory getInstance() {
    return instance;
}

// 简单工厂方法 - 获取棋子对象
public IgoChessman getIgoChessman(String color) {
    IgoChessman chessman = chessmanMap.get(color.toLowerCase());
    if (chessman == null) {
        throw new IllegalArgumentException("不支持的棋子颜色: " + color);
    }
    return chessman;
}

// 获取棋子池大小
public int getChessmanPoolSize() {
    return chessmanMap.size();
}

}

// 测试客户端
public class GoGame {
public static void main(String[] args) {
System.out.println("=== 围棋软件 - 享元模式演示 ===\n");

    // 获取享元工厂单例
    IgoChessmanFactory factory = IgoChessmanFactory.getInstance();
    
    // 显示棋子池信息
    System.out.println("棋子池中的对象数量: " + factory.getChessmanPoolSize());
    
    // 获取黑白棋子对象(享元对象)
    IgoChessman black1, black2, black3;
    IgoChessman white1, white2;
    
    black1 = factory.getIgoChessman("black");
    black2 = factory.getIgoChessman("black");
    black3 = factory.getIgoChessman("black");
    
    white1 = factory.getIgoChessman("white");
    white2 = factory.getIgoChessman("white");
    
    // 验证享元模式:相同颜色的棋子是同一个对象
    System.out.println("\n=== 对象验证 ===");
    System.out.println("black1 和 black2 是同一个对象: " + (black1 == black2));
    System.out.println("black2 和 black3 是同一个对象: " + (black2 == black3));
    System.out.println("white1 和 white2 是同一个对象: " + (white1 == white2));
    System.out.println("black1 和 white1 是同一个对象: " + (black1 == white1));
    
    // 模拟下棋过程
    System.out.println("\n=== 下棋过程模拟 ===");
    
    // 黑棋落子
    black1.display(new Position(3, 4));
    black2.display(new Position(15, 15));
    black3.display(new Position(10, 10));
    
    // 白棋落子
    white1.display(new Position(3, 3));
    white2.display(new Position(16, 16));
    
    // 再获取一次黑棋,验证仍然是同一个对象
    IgoChessman black4 = factory.getIgoChessman("black");
    black4.display(new Position(5, 5));
    System.out.println("black1 和 black4 是同一个对象: " + (black1 == black4));
    
    System.out.println("\n=== 最终统计 ===");
    System.out.println("总共使用了 " + (3 + 2 + 1) + " 个棋子位置");
    System.out.println("但实际只创建了 " + factory.getChessmanPoolSize() + " 个棋子对象");
    System.out.println("节省了 " + ((3 + 2 + 1) - factory.getChessmanPoolSize()) + " 个对象创建");
}

}

posted @ 2025-11-20 22:40  山蚯  阅读(0)  评论(0)    收藏  举报