c# - 通过通用接口(interface)封装的正确方法

标签 c# oop generics interface encapsulation

我的应用程序由服务器和客户端组成,它们是独立的。它们通过服务器创建和修改的对象进行通信。为客户端提供该对象的只读接口(interface)。据我所知,这是在 OOP 中保持封装的正确方法。请参阅示例:

// Client-side

interface IBox<T> where T : ITool
{
    IEnumerable<T> Tools { get; }
}

interface ITool
{
    void Use();
}

// Server-side

class Box : IBox<Tool>
{
    public List<Tool> ToolList = new List<Tool>();

    public IEnumerable<ITool> Tools
    {
        get { return ToolList; }
    }
}

class Tool : ITool
{
    string _msg = "default msg";
    public string Msg
    {
        get { return _msg; }
        set { _msg = value; }
    }

    public void Use()
    {
        Console.WriteLine("Tool used! Msg: {0}", _msg);
    }
}

如您所见,我必须使用泛型,因为我的对象形成了层次结构。

看起来不错,直到我决定添加 Room带接口(interface)的类IRoom ,这不仅必须概括 IBox ,但是ITool也是:

interface IRoom<B, T>
    where B : IBox<T>
    where T : ITool
{
    IEnumerable<B> Boxes { get; }
}

class Room : IRoom<Box, Tool>
{
    public List<Box> BoxList = new List<Box>();

    public IEnumerable<Box> Boxes
    {
        get { return BoxList; }
    }
}

现在,假设我们有一个 Room不仅由盒子组成。我需要至少 3 个完全不同的东西的集合,它们也是多种类型的集合。所以,一定有一棵巨大的树,我的根类变成了这样: Room : IRoom<Box, Tool1, Tool2, Tool3, Wardrobe, Coat, Jeans, Hat, Table, Computer, Book, Pen>

我不确定,这是对的。所以,我想问,实现我的任务的真正的 OOP 方式是什么? (没有反射、破坏封装、类型转换或其他糟糕的技巧)

最佳答案

从 .NET Framework 4 和 C# 4 开始,您可以使用 IEnumerable 的协变性并避免使用泛型。

// Client-side

interface IBox
{
    IEnumerable<ITool> Tools { get; }
}

interface ITool
{
    void Use();
}

// Server-side

class Box : IBox
{
    public List<Tool> ToolList = new List<Tool>();

    public IEnumerable<ITool> Tools
    {
        get { return ToolList; } // With .NET 3.5 and earlier cast here is neccessary to compile
        // Cast to interfaces shouldn't be so much of a performance penalty, I believe.
    }
}

class Tool : ITool
{
    string _msg = "default msg";
    public string Msg
    {
        get { return _msg; }
        set { _msg = value; }
    }

    public void Use()
    {
        Console.WriteLine("Tool used! Msg: {0}", _msg);
    }
}


interface IRoom
{
    IEnumerable<IBox> Boxes { get; }
}

class Room : IRoom
{
    public List<Box> BoxList = new List<Box>();

    public IEnumerable<IBox> Boxes
    {
        get { return BoxList; } // and here...
    }
}

此处描述的泛型中的协变和逆变:http://msdn.microsoft.com/en-us/library/dd799517.aspx

关于c# - 通过通用接口(interface)封装的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16360453/

相关文章:

c# - ToString() 没有返回预期的字符串

c# - 如何连接MySQL数据库?

php - 这个 PHP 库中的 '\' 是什么意思?

oop - 为什么 oop 语言不支持单独的读写访问修饰符?

c# - 如何强制子类实现一个方法

c# - 替代使用 InStr

c# - 我应该如何确保处理可能是一次性的元素?

c# - 使用仅在执行时已知的类型参数调用泛型方法

java - 用泛型实例化抽象类的对象

Java泛型返回类型