假设我有一个抽象对象,可以由多个单独的插件作者实现。(例如,一个错误数据库连接)我不希望我的位的消费者必须处理每个特定的插件类型。
我还想将解析配置文件的过程与实际初始化数据库插件和其他类似事情的过程分开。
为此,我想出了这样的事情:
public interface IConfiguration
{
// No members
}
public interface IConnection
{
// Members go in here
void Create();
void Update();
void Delete();
}
public interface IConnectionProvider
{
// Try to interpret file as a configuration, otherwise return null
IConfiguration ParseConfiguration(Stream configurationContents);
IConnection Connect(IConfiguration settings);
}
public class ThingyRepository
{
// Lets say there is a constructor that initializes this with something
List<IConnectionProvider> providers;
// Insulates people from the actual connection provider
KeyValuePair<IConfiguration, IConnectionProvider> Parse(string filename)
{
IConnection result = null;
IConnectionProvider resultProvider = null;
foreach (var provider in this.providers)
{
using (Stream fs = OpenTheFileReadonly(filename))
{
IConnection curResult = provider.ParseConfiguration(fs);
if (curResult == null)
{
continue;
}
else
{
if (result == null)
{
result = curResult;
resultProvider = provider;
}
else
{
throw new Exception ("ambguity!");
}
}
}
}
if (result == null)
{
throw new Exception ("can't parse!");
}
return new KeyValuePair<IConfiguration, IConnectionProvider>(
result, resultProvider);
}
}
我的问题是,我有这个空界面,它应该用作从指定文件加载的任何设置的不透明句柄。IConnectionProvider 的特定实现者知道它需要从文件加载的配置中的哪些位,但是该库的用户应该与该信息隔离。
但是有一个空的界面对我来说似乎很奇怪。这种事情有意义还是我做错了什么?