4

让我们使用这个简单的例子:

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];
    }
}

盒子.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 }
    }
}

有没有一个好的设计模式可以让它更优雅?

4

4 回答 4

4

您可以使用多种策略。

对接口进行编程通常很有用。下面的 IBox 界面不允许人们编辑该框(不将其转换为 a 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};
}
于 2013-05-07T17:13:51.873 回答
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.
   }
于 2013-05-07T17:10:22.390 回答
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

您也许可以使用显式接口实现来实现这一点?创建一个界面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;
    }
}

现在,为了 mutate Box,您需要将其转换为IMutableBox.

var box = new Box();
var mutableBox = box as IMutableBox;
mutableBox.SetEmpty(true);
于 2013-05-07T17:12:17.267 回答
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]);
    }
}
于 2013-05-07T18:42:02.860 回答