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()
{...}
}
我想将Interface公开给我的表示层。
如何设计上面三个类(2个类和1个接口)以将所有方法公开到我的表示层。
所有方法均指:
请问我是否需要更多解释
表示层可能就像
void Main()
{
ISaveData saveFiles = new GoogleCloudSaveFile(); // This is just example. I will inject this via Dependency Injection framework
saveFiles.Delete();
}
ChangeBucket()
和AssignPermission()
只是示例方法。我想说,我们的子类可以有不同的方法,比如这两个。
一个解决方案是我可以在界面中定义这两个方法,并且可以将方法体留空一个方法,但我认为这不是一个好方法
答案 0 :(得分:1)
据我所知,根据您提供的信息而不了解哪种方法在哪个界面中存在的细节,这是我能想到的最简单的方法:
ISaveData x = new GoogleCloudSaveFile();
x.DeleteFile();
(x as IPermission).AssignPermission();
您可以按如下方式使用这些:
if(x is IPermission)
(x as IPermission).AssignPermission();
您还可以在类型转换之前检查您创建的对象是否为类型:
public interface IGoogleCloudSaveFile : ISaveData, IPermission { }
public interface IAzureSaveData : ISaveData, IBucketOperation { }
我不确定您是否愿意采取以下方法,但我认为这会更好:
throw new NotImplementedException();
除非您想忽略设计主体并将所有内容放在一个接口中,否则您很难使用通用接口并期望它具有基于实现的不同类型对象的不同方法。在这种情况下,只需将所有内容放在一个界面中,并在类中实现它时,只需执行
即可{{1}}