Search code examples
.netfriendset-difference

How to obtain difference of sets like behaviour with OOP


I need a behaviour similar to the set-theory's difference operator \.
I am trying to get something of a friend class behaviour since i have the following situation:

    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();
}

As you can see i have 3 interfaces (IRead,IWrite,IFull) where the third one is derived from the first 2 and has another method.

I have two classes Consumer and Admin where:

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

Since both my concrete classes delegate the implementations to an inner field the problem lies in the inner field for the Consumer class since it needs something that implements IFull\IWrite

Normally in C++ i would've solved this with a friend class that has full access to the class while using a Read for all others.Here i can't do this.

What are my alternatives ?
I need an interface ISomething: {IFull\IWrite} for the Consumer class

P.S IFull\IWrite=IRead + [methods defined only in IFull]


Solution

  • A quick and simple solution would be to break up the hierarchy from IFull interface and have 3 separate interfaces, which can be derived according to use need for either Consumer or Admin classes