让我们使用这个简单的例子:
Connect4Board.cs:
public class Connect4Board
{
private Box[,] _boxes = new Box[7, 6];
public void DropPieceAt(int column, bool redPiece)
{
//Safe modifications to box colors.
}
public Box GetBoxAt(int x, int y)
{
return _boxes[x, y];
}
}
Box.cs :
public class Box
{
public bool IsRed { get; private set; }
public bool IsEmpty { get; private set; }
}
我希望GetBoxAt()
返回一个包含只读属性的框。但是我希望我的Connect4Board
能够更改框颜色。
我们假设我根本不想使用internal
修饰符。
我的解决方案(非常难看):
public class Connect4Board
{
private Box.MutableBox[,] _mutableBoxes = new Box.MutableBox[7, 6];
public Connect4Board()
{
for (int y = 0; y < 6; y++)
{
for (int x = 0; x < 7; x++)
{
_mutableBoxes[x, y] = new Box.MutableBox();
}
}
}
public void DropPieceAt(int column, bool isRed)
{
//Safe modifications to box colors.
}
public Box GetBoxAt(int x, int y)
{
return _mutableBoxes[x, y].Box;
}
}
public class Box
{
public bool IsRed { get; private set; }
public bool IsEmpty { get; private set; }
private Box()
{
}
public class MutableBox
{
public Box Box { get; private set; }
public MutableBox()
{
Box = new Box();
}
public void MakeRed() { //I can modify Box here }
public void MakeYellow() { //I can modify Box here }
public void MakeEmpty() { //I can modify Box here }
}
}
是否有一个好的设计模式可以让它更优雅?
答案 0 :(得分:4)
您可以使用多种策略。
编程接口通常很有用。下面的IBox界面不允许人们编辑框(不将其转换为Box
),但仍然可以使代码简单。
public class Connect4Board
{
private Box[,] _boxes = new Box[7, 6];
public void DropPieceAt(int column, bool redPiece)
{
//Safe modifications to box colors.
}
public IBox GetBoxAt(int x, int y)
{
return _boxes[x, y];
}
}
public interface IBox
{
bool IsRed { get; }
bool IsEmpty { get; }
}
public class Box : IBox
{
public bool IsRed { get; set; }
public bool IsEmpty { get; set; }
}
另一种方法是使框永远不变(如字符串),而不是修改框的状态,只需修改哪个框位于数组中的哪个位置:
public class Connect4Board
{
private Box[,] _boxes = new Box[7, 6];
public Connect4Board()
{
for(int i = 0; i<7; i++)
{
for(int j = 0; j<6; j++)
{
// Notice how you're not changing a color, but assigning the location
_boxes[i,j] = Box.Empty;
}
}
}
public void DropPieceAt(int column, bool redPiece)
{
// Modifications to the top empty location in the given column.
}
public Box GetBoxAt(int x, int y)
{
return _boxes[x, y];
}
}
public class Box
{
public bool IsRed { get; private set; }
public bool IsBlack { get; private set; }
public bool IsEmpty { get; private set; }
private Box() {}
public static readonly Box Red = new Box{IsRed = true};
public static readonly Box Black = new Box{IsBlack = true};
public static readonly Box Empty = new Box{IsEmpty = true};
}
答案 1 :(得分:3)
为你做这项工作? 使用静态工厂使Box不可变,并添加返回具有各种颜色的新框的静态属性
public class Box
{
private Box() {}
private Box(Color color) { Color = color; }
public static Box Make(Color color) { return new Box(color); }
public static Box RedBox { get { return new Box(Color.Red); } }
public static Box GreenBox { get { return new Box(Color.Green); } }
public static Box BlueBox { get { return new Box(Color.Blue); } }
// ... etc.
}
答案 2 :(得分:1)
解决方案1
您可以在Box
周围创建一个不可变的包装器。 Connect4Board
会在内部使用MutableBox
类,但会向消费者公开ImmutableBox
。
public interface IBox
{
bool IsRed { get; }
bool IsEmpty { get; }
}
public class MutableBox : IBox
{
public bool IsRed { get; set; }
public bool IsEmpty {get; set; }
public IBox MakeImmutable()
{
return new ImmutableBox(this);
}
}
public class ImmutableBox : IBox
{
private IBox innerBox;
public ImmutableBox(IBox innerBox) { this.innerBox = innerBox; }
public bool IsRed { get { return innerBox.IsRed; } }
public bool IsEmpty { get { return innerBox.IsEmpty; } }
}
public class Connect4Board
{
private MutableBox[,] boxes = new MutableBox[7, 6];
public void DropPieceAt(int column, bool redPiece)
{
// perform modifications
}
public IBox GetBoxAt(int x, int y)
{
return boxes[x,y].MakeImmutable();
}
}
解决方案2
您可以使用explicit interface implementation来实现这一目标吗?
创建一个界面IMutableBox
。
public interface IMutableBox
{
void SetIsRed(bool isRed);
void SetIsEmpty(bool isEmpty);
}
public class Box : IMutableBox
{
private bool isRed;
private bool isEmpty;
public bool IsRed { get { return isRed; } }
public bool IsEmpty { get { return isEmpty; } }
void IMutableBox.SetIsRed(bool isRed)
{
this.isRed = isRed;
}
void IMutableBox.SetIsEmpty(bool isEmpty)
{
this.isEmpty = isEmpty;
}
}
现在,为了变异Box
,您需要将其转换为IMutableBox
。
var box = new Box();
var mutableBox = box as IMutableBox;
mutableBox.SetEmpty(true);
答案 3 :(得分:1)
您可以为ReadOnlyBox
制作一个Box
,就像ReadOnlyCollection
一样。
[Flags]
public enum BoxState
{
Empty = 0,
Red = 1 << 0,
Black = 1 << 1
}
[Flags]
public enum BoardColor
{
Red = 1 << 0,
Black = 1 << 1
}
public interface IBox
{
BoxState State { get; }
}
public class Box : IBox
{
public BoxState State { get; set; }
}
public class ReadOnlyBox : IBox
{
private readonly IBox _box;
public ReadOnlyBox(IBox box)
{
_box = box;
}
public BoxState State { get { return _box.State; } }
}
public class Connect4Board
{
private const int _boardWidth = 7;
private const int _boardHeight = 6;
private Box[,] _boxes = new Box[_boardWidth, _boardHeight];
public void DropPieceAt(int column, BoardColor color)
{
for(int height = 0; height < _boardHeight; height++)
{
if(_boxes[column, height].State != BoxState.Empty) continue;
_boxes[column, height].State = (BoxState)color;
break;
}
}
public IBox GetBoxAt(int x, int y)
{
return new ReadOnlyBox(_boxes[x, y]);
}
}