【问题标题】:How to obtain difference of sets like behaviour with OOP如何使用 OOP 获得类似行为的集合差异
【发布时间】:2019-04-17 08:29:28
【问题描述】:

我需要类似于集合论的差分运算符\ 的行为。
我正在尝试获得 friend 类行为,因为我有以下情况:

    public interface IRead
    {
        List<string> GetInnerResource();
    } 
    public interface IWrite()
    { 
        bool SetInnerResource();
    }
    public interface IFull:IRead,IWrite{
        void SomeMethod();
    }


public Consumer:IFull  // IFull \ IWrite
{
   private IFull access; // IFull\IWrite 

   public List<string> GetInnerResource()=>this.access.GetInnerResource();
   public void SomeMethod()=>this.acces.SomeMethod();  // i need this 
   public bool SetInnerResource(){...}//--i do not want this here !
}
public Admin:IFull
{
   private IFull access;
   public List<string> GetInnerResource()=>this.access.GetInnerResource();
   public bool SetInnerResource()=>this.access.SetInnerResource();
   public void SomeMethod()=>this.access.SomeMethod();
}

如您所见,我有3 接口(@98​​7654325@,IWrite,IFull),其中第三个接口派生自第一个2 并有另一种方法。

我有两个班级 ConsumerAdmin 其中:

 -Consumer needs to implement IFull \ IWrite
 -Admin  needs IFull

由于我的两个具体类都将实现委托给内部字段 问题在于Consumer 类的内部字段,因为它需要实现IFull\IWritesomething

通常在C++ 中,我会使用friend 类来解决这个问题,该类可以完全访问该类,而对所有其他人使用Read。我不能这样做。

我的选择是什么?
我需要Consumer 类的接口ISomething: {IFull\IWrite}

附言 IFull\IWrite=IRead + [methods defined only in IFull]

【问题讨论】:

    标签: .net friend set-difference


    【解决方案1】:

    一个快速简单的解决方案是从 IFull 接口中分解层次结构,并拥有 3 个独立的接口,可以根据 Consumer 或 Admin 类的使用需求派生出来

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-17
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多