redcapri
redcapri

Reputation: 11

Looking for approach to design two classes and one interface

public interface ISaveData
{
     void DeleteFile(); // this is common method
     //void ChangeBucket(); I don't need this method in GoogleCloudSaveFile. Should I remove this from here
     // void AssignPermission(); // I don't need of this method in AzureSaveData. Should I remove this from here?
}

public class AzureSaveData : ISaveData
{
     void ChangeBucket()
     {...}

     void DeleteFile()
     {...}
}

public class GoogleCloudSaveFile() : ISaveData
{
     void AssignPermission()
     {...}
     void DeleteFile()
     {...}
}

I want to expose Interface to my presentation layer.

How can I design above three classes (2 classes and 1 interface) to expose all methods to my presentation layer.

All methods means:

Please ask me if you need more explanation

Presentation layer could be like

void Main()
{
    ISaveData saveFiles = new GoogleCloudSaveFile(); // This is just example. I will inject this via Dependency Injection framework

    saveFiles.Delete(); 
}

ChangeBucket() and AssignPermission() are just example methods. I wanted to say, our child classes could have different methods like these two.

One solution is I can define these two methods in interface and can leave method body empty of one method but I don't think it will be good approach

Upvotes: 1

Views: 60

Answers (1)

Shakti Prakash Singh
Shakti Prakash Singh

Reputation: 2533

As far as I can think based on the information provided by you without getting into the nitty-gritty of what method would lie in which interface, this is the easiest I can think of:

public interface ISaveData
{
    void DeleteFile(); // this is common method
}

public interface IPermission
{
    void AssignPermission();
}

public interface IBucketOperation //or something else
{
    void ChangeBucket();
}

public class AzureSaveData : ISaveData, IBucketOperation 
{
    public void ChangeBucket()
    {
        Console.WriteLine("AzureSaveData ChangeBucket");
    }

    public void DeleteFile()
    {
        Console.WriteLine("AzureSaveData DeleteFile");
    }
}

public class GoogleCloudSaveFile : ISaveData, IPermission
{
    public void AssignPermission()
    {
        Console.WriteLine("GoogleCloudSaveFile AssignPermission");
    }

    public void DeleteFile()
    {
        Console.WriteLine("GoogleCloudSaveFile DeleteFile");
    }
}

You can use these as follows:

    ISaveData x = new GoogleCloudSaveFile();
    x.DeleteFile();
    (x as IPermission).AssignPermission();

You can also check if the object you create is of the type before typecasting:

if(x is IPermission)
    (x as IPermission).AssignPermission();

I am not sure if you are willing to take the following approach but I think this would be better:

public interface IGoogleCloudSaveFile : ISaveData, IPermission { }
public interface IAzureSaveData : ISaveData, IBucketOperation { }

It would be difficult for you to use a common interface and expect it to have different methods available for different type of objects based on the implementation unless you want to ignore design principals and put everything into one interface. In that case, just put everything in one interface, and while implementing it in the classes, just do a

throw new NotImplementedException();

Upvotes: 1

Related Questions