代码之家  ›  专栏  ›  技术社区  ›  FrostyStraw

嵌套类和接口

  •  2
  • FrostyStraw  · 技术社区  · 7 年前

    (如果有人想帮忙的话,我真的很难为这个问题找到一个好的标题。)

    所以我在设计一些东西时遇到了一个问题。本质上,我有一个类a,它由B类型的对象数组组成。我只希望a类的接口被公开,并且希望B类基本上对任何用户隐藏。我希望能够对类型B及其数据执行操作,但只能通过类A的接口/方法调用B实例的方法。困难之处在于,我想创建一个对类型B的成员执行操作的方法,但我想实现一个接口,然后有一个实现该接口的类,因为我希望我的用户能够创建自己的该方法实现。我想做一些事情,比如:

    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发生,这也是我将B嵌套在A中的原因。但是,既然类B是私有的,我是否可以将其用作类C方法的参数?实现的Interface1方法将影响B如何执行someMethodOfb的内部实现,这就是为什么我认为我需要传递它,以便能够保持B类的隐藏性质。是否有更好的方法来设计它,并能够实现我在第一段中设定的目标?

    2 回复  |  直到 7 年前
        1
  •  2
  •   NetMage    7 年前

    我建议您为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

        2
  •  1
  •   ylax    7 年前

    让我们用具体的例子:)

    比方说,我们有三个类: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
        }
    }