嵌套类和接口

时间:2018-04-19 21:50:40

标签: c# interface inner-classes

(如果有人想帮助解决这个问题,我真的很难为这个问题找到一个好头衔。)

所以我在设计方面遇到了问题。基本上我有一个类A,它由类型B的对象数组组成。我只希望暴露类A的接口,并且希望将类B基本上隐藏到任何用户。我希望能够对类型B及其数据执行操作,但只能通过类A的接口/方法调用B实例的方法。它变得棘手的部分是我想创建一个对成员执行操作的方法类型B,但我想实现一个接口,然后有一个实现该接口的类,因为我希望我的用户能够创建自己的这个方法的实现。我正在考虑做像somtehing:

public class A 
{
    B[] arr;
    C c;

    public A(C c) 
    { 
        arr = new B[100];
        this.c = c;
    }


    public void method1() 
    {
        var b = new B();
        b.someMethodofb(c);    // pass c to the method of b
    }

    private class B 
    {
        someMethodOfb(C c) 
        {
        }
    }
}

public class C : Interface1 
{
    public void method(B b) 
    {    
        //interface method we have implemented
    }
}

我将B类设为私有,因为我只希望A类公开,所以B类发生的任何事情都发生在A类,这也是我在A中嵌套B的原因。但是因为B类是私有的,我会能够将它用作我的C类方法的参数吗? Interface1实现的方法将影响B执行someMethodOfb的内部实现,这就是为什么我认为我需要传递它以便能够维护B类的隐藏性质。对我来说有更好的方法设计这个并且能够实现我在​​第一段中提出的目标吗?

2 个答案:

答案 0 :(得分:2)

我建议你为B的公共已知方添加另一个接口,让B实现该接口并让C的方法使用该接口。

public interface IC {
    void method(IB b);
}

public interface IB {
    int Priority { get; set; }
    int Urgency { get; set; }
}

public class A {
    B[] arr;
    IC c;

    public A(C c) {
        arr = new B[100];
        this.c = c;
    }


    public void method1() {
        var r = (new Random()).Next(100);
        arr[r].someMethodOfB(c);    // pass c to the method of b
    }

    private class B : IB {
        public int Priority { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public int Urgency { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        internal void someMethodOfB(IC aC) {
            aC.method(this);
            throw new NotImplementedException();
        }
    }
}

public class C : IC { // user implements
    public void method(IB b) {
        if (b.Priority > 10 || b.Urgency > 10)
            ; // do something with BI using b
        throw new NotImplementedException();
    }
}

现在,类的用户需要知道IC,以便他们可以创建C并且他们需要知道IB,以便他们可以在C中编写方法的主体},但他们不需要知道所有B或有权访问B

答案 1 :(得分:1)

让我们使用具体的例子:)

说,我们有三个类:Customer,Order和OrderProcessor。客户和订单分别是代表客户和订单的实体,而OrderProcessor将处理订单:

public interface IOrderProcessor
{
    void ProcessOrder(IOrder order);
}

public interface IOrder
{
    void FinalizeSelf(IOrderProcessor oProc);
    int CustomerId {get; set;}
}

public class Customer
{
    List<IOrder> _orders;
    IOrderProcessor _oProc;
    int _id;

    public Customer(IOrderProcessor oProc, int CustId)
    {
        _oProc = oProc;
        _orders = new List<IOrder>();
        _id = CustId;
    }

    public void CreateNewOrder()
    {
        IOrder _order = new Order() { CustomerId = _id };
        _order.FinalizeSelf(_oProc);
        _orders.Add(_order);
    }

    private class Order : IOrder
    {
        public int CustomerId {get; set;}
        public void FinalizeSelf(IOrderProcessor oProcessor)
        {
            oProcessor.ProcessOrder(this);
        }
    }
}
public class ConcreteProcessor : IOrderProcessor
{
    public void ProcessOrder(IOrder order)
    {
        //Do something
    }
}
相关问题