c# - C#中的对象健美操一级集合规则示例?

标签 c# oop design-patterns solid-principles

我正在玩对象健美操规则,我在使用 C# 时遇到了一些麻烦,不知道什么时候使用第一类集合。

我的意思是我几乎看不到什么时候应该使用它,例如很难将该规则应用于 EF DbContext

比方说,我们设计了一个 Board 类。

public class Board
{
    public IList<BoardRow> Rows { get; }
    public IList<BoardColumn> Columns { get; }

    public Board()
    {
        Rows = new List<BoardRow>();
        Columns = new List<BoardColumn>();
    }
}

所以根据那个规则,我们必须把上面的代码变成:

// Is it really that better than just using List<BoardRow>?
public class BoardRowCollection : IEnumerable<BoardRow>
{
    public void Add(BoardRow row) { /*...*/ }
    public void Remove(BoardRow row) { /*...*/ }

    // IEnumerable<BoardRow> Impl goes here...
}

// Is it really that better than just using List<BoardColumn>?
public class BoardColumnCollection : IEnumerable<BoardColumn>
{
    public void Add(BoardColumn column) { /*...*/ }
    public void Remove(BoardColumn column) { /*...*/ }

    // IEnumerable<BoardRow> Impl goes here...
}

public class Board
{
    public BoardRowCollection Rows { get; }
    public BoardColumnCollection Column { get; }

    // Rest of the impl, ctor, etc.
}

当您已经拥有可用于实现您的目标的基类时,我不确定是否理解这条规则的要点。

也许上面的代码不是最好的,但我想看一个示例,它可以阐明该规则的目的。

最佳答案

背景

假设您有一个类 Foo 并且出于任何原因它需要 Board 中的 Rows

现在 Foo 需要在 Rows 中找到第 5 个项目。几天后,您需要一个 Buu 类,它应该在 Rows 中找到第 8 个项目。

FooBuu 都有自己的关于如何在 Rows 中查找项目的实现。

// ...
Foo foo = new Foo(board.getRows());
Buu buu = new Buu(foo.getRows());

BoardRow row5 = foo.find5thRow();
BoardRow row8 = buu.find8thRow();

只有集合本身应该知道如何对其进行操作

来自Objects Calisthenics :

Application of this rule is simple: any class that contains a collection should contain no other member variables. Each collection gets wrapped in its own class, so now behaviors related to the collection have a home. You may find that filters become a part of this new class. Also, your new class can handle activities like joining two groups together or applying a rule to each element of the group.

如果我们要为 Rows 创建一个 First Class Collection,我们可以将它的一个实例传递给 FooBoo 并在其上调用一个方法:

class Foo {
    RowCollection rowCollection;

    // constructor and more ...

    public BoardRow find5thRow() {
        rowCollection.findByIndex(5);
    }
}

总结

First Class Collection 应该涵盖创建、读取、更新、删除、过滤、合并等操作。传递一个 First Class Collection 的实例而不是它自己的集合。优点是您只需将方法委托(delegate)给 First Class Collection,而不用编写新的操作副本。

关于c# - C#中的对象健美操一级集合规则示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53535573/

相关文章:

c# - Asp.Net CheckBoxList ListItem CssClass 由 aspNetDisabled 覆盖

c# - JWT 如何添加自定义声明和解码声明

php - Laravel 容器和共享实例

java - 没有通用接口(interface)的步骤序列模式

java - 使用 arraylists 得到错误的输出

c# - 无法从 DoubleClick 事件切换 TabItem

c# - 如何检测用户是否正在拖动 slider 拇指

php - 如何在 PHP 中使用多个类

java - 'protected' 和 'protected-static' 变量有什么区别?

c# - 如果条件取决于另一个变量,如何在不同的条件之间进行选择