如何通过OOP获得行为集的差异

问题描述 投票:0回答:1

我需要一个类似于set-theory的差异运算符\的行为。 我试图得到一些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接口(IReadIWriteIFull),其中第三个来自第一个2,并有另一种方法。

我有两个类ConsumerAdmin其中:

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

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

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

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

P.Sazxswpoi

.net friend set-difference
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.