菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

软件设计-Tutorial13

复制代码
```mermaid
classDiagram
    class ChessPiece {
        <<abstract>>
        +String color
        +display(int x, int y)
    }

    class WhiteChessPiece {
        +display(int x, int y)
    }

    class BlackChessPiece {
        +display(int x, int y)
    }

    class ChessPieceFactory {
        -static ChessPieceFactory instance
        -Map<String, ChessPiece> chessPieceMap
        +static getInstance() ChessPieceFactory
        +getChessPiece(String color) ChessPiece
    }

    class ChessBoard {
        -List<Move> moves
        +placePiece(String color, int x, int y)
    }

    class Move {
        +String color
        +int x
        +int y
    }

    ChessPiece <|-- WhiteChessPiece
    ChessPiece <|-- BlackChessPiece
    ChessPieceFactory --> ChessPiece
    ChessBoard --> ChessPieceFactory
    ChessBoard --> Move
```
复制代码
复制代码
package Tutorial13;

import java.util.ArrayList;
import java.util.List;

class ChessBoard {
    private List<Move> moves = new ArrayList<>();

    public void placePiece(String color, int x, int y) {
        ChessPiece piece = ChessPieceFactory.getInstance().getChessPiece(color);
        piece.display(x, y);
        moves.add(new Move(color, x, y));
    }

    private class Move {
        String color;
        int x, y;

        Move(String color, int x, int y) {
            this.color = color;
            this.x = x;
            this.y = y;
        }
    }
}
复制代码
复制代码
package Tutorial13;

abstract class ChessPiece {
    protected String color;

    public abstract void display(int x, int y);
}

class WhiteChessPiece extends ChessPiece {
    public WhiteChessPiece() {
        this.color = "";
    }

    @Override
    public void display(int x, int y) {
        System.out.println("在位置 (" + x + ", " + y + ") 放置 " + color + " 棋子");
    }
}

class BlackChessPiece extends ChessPiece {
    public BlackChessPiece() {
        this.color = "";
    }

    @Override
    public void display(int x, int y) {
        System.out.println("在位置 (" + x + ", " + y + ") 放置 " + color + " 棋子");
    }
}
复制代码
复制代码
package Tutorial13;

import java.util.HashMap;
import java.util.Map;

class ChessPieceFactory {
    private static ChessPieceFactory instance;
    private Map<String, ChessPiece> chessPieceMap;

    private ChessPieceFactory() {
        chessPieceMap = new HashMap<>();
    }

    public static ChessPieceFactory getInstance() {
        if (instance == null) {
            instance = new ChessPieceFactory();
        }
        return instance;
    }

    public ChessPiece getChessPiece(String color) {
        ChessPiece chessPiece = chessPieceMap.get(color);
        if (chessPiece == null) {
            if (color.equalsIgnoreCase("")) {
                chessPiece = new WhiteChessPiece();
            } else if (color.equalsIgnoreCase("")) {
                chessPiece = new BlackChessPiece();
            }
            chessPieceMap.put(color, chessPiece);
        }
        return chessPiece;
    }
}
复制代码
复制代码
package Tutorial13;
public class GoGame {
    public static void main(String[] args) {
        ChessBoard board = new ChessBoard();
        board.placePiece("", 0, 0);
        board.placePiece("", 1, 1);
        board.placePiece("", 2, 2);
        board.placePiece("", 3, 3);
    }
}
复制代码

 

posted on   hhmzd233  阅读(4)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2023-11-11 11.10日补交 顺序表的实现
点击右上角即可分享
微信分享提示