「友達/内部が必要な場合はデザインが間違っている」などとよく言われますが、ChessPiece.Locationの内部を削除するために次のコードを再設計する方法を教えてもらえますか?
これは現在、ChessBoardにピースを追加すると、ChessPiece.Locationプロパティが一致するように設定されるように使用されています。明らかに、パブリックにすることは内部よりも悪く、プライベートにすることでChessBoardがロケーションを更新できなくなります。洞察をありがとう。
public struct Coord
{
public Coord(int x, int y) { this.X = x; this.Y = y; }
public int X { get; private set; }
public int Y { get; private set; }
}
public class ChessBoard
{
public ChessBoard() { /*[...]*/ }
public ChessPiece this[int x, int y]
{
get
{
// Return ChessPiece at this position (or null)
}
set
{
// Add ChessPiece at this position and set its Location property
}
}
public class ChessPiece
{
public ChessPiece() { /*[...]*/ }
public Coord Location { get; internal set; }
}