[英]Create a board made of Cells able host a Generic value
我正在做一個項目(一個游戲),它將有一個可以玩的棋盤。
我想讓這個板盡可能通用,以便能夠將代碼重新用於可能的其他游戲。
public class Board {
private int rows;
private int cols;
private Box[][] board;
public Board(int rows, int cols){
Box[][] board = new Box[rows][cols];
for (int row = 0; row < this.rows; row++){
for (int col = 0; col < this.cols; col++){
board[row][col] = new Box();
}
}
}
public int getCols(){return this.cols;}
public int getRows(){return this.rows;}
public Piece getContentAtPos(int row, int col){
return board[row][col].getContent();
}
}
這是Board
class。這里的問題是我必須使用getContentAtPos()
方法返回一個Piece
object,因為Box
( Cell ) class 是這樣的:
public class Box {
private boolean empty;
private Piece content;
public Box(){
empty = true;
}
public Box(Piece piece){
empty = false;
this.content = piece;
}
public boolean isEmpty(){
return empty;
}
public Piece getContent(){
if (!empty) {
return content;
} else {
return null;
}
}
}
然而,對我來說理想的是 class Box
能夠托管任何類型的 object 並通過 Box getContent()
方法返回這個通用的 object 和getContentAtPos()
。
我的通用Box
class。
public class Box<T>{
private boolean empty;
private T content;
public Box(){
empty = true;
}
public Box(T content){
this.content = content;
empty = false;
}
public boolean isEmpty(){
return empty;
}
public T getContent(){
if (!isEmpty()){
return content;
}
return null;
}
public T setContent(Object content){
this.content = content;
}
}
但是我需要在 Board class 中更改什么?
我應該把什么返回值放在那里?
您應該創建您將來返回的每個 class 將實現的接口。 這樣您就可以確保返回類型將實現接口並具有定義的行為。
public interface ReturnableBox {
public boolean isEmpty();
}
public class Board {
private int rows;
private int cols;
private ReturnableBox [][] board;
}
public class Box implements ReturnableBox {
//...
}
對我來說理想的是 class
Box
能夠承載任何類型的 object 並通過Box
getContent
方法返回帶有getContentAtPos
的通用 object
我想您希望您的游戲片段聲明某種行為。 如果是這樣, Box
不需要存儲任意的 object,而是存儲類型為 object 的合約,該合約包含游戲塊預期具有的所有方法。
換句話說,您可以定義一個接口,比方說Piece
,它將有幾個實現:
public interface Piece {
// behavior of game pieces
}
public class MyGamePiece implements Piece {
// implementations
}
您不需要將Box
class 設為通用,而是它可以包含Piece
類型的字段:
public class Box {
private boolean empty;
private Piece content;
// constractor and methods
public void setContent(Object content){
this.content = content;
}
public Piece getContent(){
return content;
}
}
這使您可以自由地通過Box
class 的構造函數或 true the setter 提供Peice
的任何實現。 這意味着您可以設計僅具有一個實現MyGamePiece
的 gave 的核心部分,然后再添加幾個類,如NewGamePeace
(如果簡潔明了,名稱應反映 class 的目的,這只是虛擬示例)。
關鍵是您可以從多態性中受益,而不必更改以前設計和測試的代碼以使其與新類一起使用(稱為后期綁定):
Box box = new Box();
box.setContent(new MyGamePiece());
box.setContent(new NewGamePeace()); // in order to make this line compile only need to introde the `NewGamePeace` class itself
方法getContentAtPos()
也將返回類型為Piece
的 object。
我想讓這個板盡可能通用,以便能夠將代碼重新用於可能的其他游戲。
這是一個值得稱贊的目標。
為了可重用,您的類必須是松散耦合和狹窄的重點。 即每個 class 都應該是一組范圍狹窄且定義明確的功能,並且只知道那些對其工作至關重要的 object(減少耦合)。
並且你可以通過引入如上所示的接口來使必要的耦合變得松散(高耦合不好,但是類完全沒有耦合就無法交互,你需要保持平衡)。
即使最終您不會在其他項目中使用此代碼,保持松散耦合也是有益的,因為這樣可以更輕松地維護和擴展代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.