寻找一种设计模式,它可以创建具有不同接口实现的 class 的不同实例
Looking for a Design pattern which can create different instances of a class with different interface implementations
我有一个 class,其中包含一个视图依赖项(所有接口)。 class 的行为基本上是通过这些接口的实现来定义的。我希望能够有一个 "builder",它可以使用接口(或其部分)的不同实现创建此 class 的实例。像这样:
public class API
{
private readonly ISomeInterface _someInterface;
private readonly ISomeOtherInterface _someOtherInterface;
private readonly ISomeAnotherInterface _someAnotherInterface;
API(ISomeInterface someInterface,ISomeOtherInterface someOtherInterface,ISomeAnotherInterface someAnotherInterface)
{*/implementation ommitted*/}
//Example method
public void DoSomethingWhichDependsOnOneOrMoreInterfaces()
{
//somecode
id(_someInterface != null)
_someInterface.SomeMethode();
}
public class MyApiBuilder()
{
// implementation ommitted
API CreateAPI(someEnum type)
{
switch(type)
{
case SpecificAPI32:
var speficImplementationOfSomeInterface = new ImplementsISomeInterface();
speficImplementationOfSomeInterface .Setup("someSetup");
var specificImplementationOfOtherInterface = new ImplementsISomeOtherInterface();
returns new API(speficImplementationOfSomeInterface,specificImplementationOfOtherInterface ,null);
}
}
}
最优雅的实现方式是什么(如果这完全有意义的话)?我首先想到的是构建器设计模式,但据我所知,它略有不同。
[编辑]
正如所指出的,我实现它的方式是一种工厂方法,但我对它并不完全满意。 API 可以包含各种不同的接口,这些接口可以完全相互独立,但有些可能依赖于其他接口。(但不是强制性的)我想给用户(使用这个 "API" 的开发人员) ) 尽可能自由地创建他想使用的 API。让我们试着解释一下我基本上在做什么:
假设我正在为游戏引擎开发一个插件,它可以 post 成就和其他内容到各种社交媒体渠道。所以基本上可以有一个接口来实现对 twitter、facebook、youtube 等等或一些自定义服务器的访问。此自定义服务器可能需要某种身份验证过程。用户应该能够在 API 开始时以一种很好的方式构建(嗯,流利很好..)。所以基本上是这样的:
var myTotallyForMyNeedsBuildAPI = API.CreateCustomApi().With(Api.Twitter).And(Api.Facebook).And(Api.Youtube).And(Api.CustomServer).With(Security.Authentification);
我真的不知道怎么说得流利,但像这样就好了。
您实施的是 Factory method 模式。
这对于您尝试做的事情来说非常好,但是您可以根据您的上下文以及您认为代码将如何发展来查看其他工厂模式(即 here)未来。
反正我也会考虑不要把这三个接口绑在一个工厂里。如果它们真的如此紧密地一起使用并一起构建,也许它们首先不应该是三个不同的接口,或者至少所有三个都由相同的 class 实现,因此您的工厂将构建适当的class 正确实施这些。
可能你要的是Decorator pattern。
在您的 API class 中,如果每个接口已提供给 API 实例,您将调用它们,这是 Decorator 模式的行为。
使用此模式,您可以获得模块化实现,允许您向 API.
添加多个行为
使用依赖注入 是一种很好的做法,因为您希望让程序员能够组合 具有所需配置的对象。
检查非常适合这项工作的 MEF
和 Unity
框架。
例如在Unity
中你可以这样写:
// Introducing an implementation for ISomeInterface
container.Register<ISomeInterface, SomeImplementation>();
// Introducing an implementation for ISomeOtherInterface
container.Register<ISomeOtherInterface, SomeOtherImplementation>();
// Introducing an implementation for ISomeAnotherInterface
container.Register<ISomeAnotherInterface, SomeAnotherImplemenation>();
container.Register<API, API>();
// and finally unity will compose it for you with desired configurations:
var api = container.Resolve<API>();
在这种情况下,api
将 组合 与所需的实现。
我有一个 class,其中包含一个视图依赖项(所有接口)。 class 的行为基本上是通过这些接口的实现来定义的。我希望能够有一个 "builder",它可以使用接口(或其部分)的不同实现创建此 class 的实例。像这样:
public class API
{
private readonly ISomeInterface _someInterface;
private readonly ISomeOtherInterface _someOtherInterface;
private readonly ISomeAnotherInterface _someAnotherInterface;
API(ISomeInterface someInterface,ISomeOtherInterface someOtherInterface,ISomeAnotherInterface someAnotherInterface)
{*/implementation ommitted*/}
//Example method
public void DoSomethingWhichDependsOnOneOrMoreInterfaces()
{
//somecode
id(_someInterface != null)
_someInterface.SomeMethode();
}
public class MyApiBuilder()
{
// implementation ommitted
API CreateAPI(someEnum type)
{
switch(type)
{
case SpecificAPI32:
var speficImplementationOfSomeInterface = new ImplementsISomeInterface();
speficImplementationOfSomeInterface .Setup("someSetup");
var specificImplementationOfOtherInterface = new ImplementsISomeOtherInterface();
returns new API(speficImplementationOfSomeInterface,specificImplementationOfOtherInterface ,null);
}
}
}
最优雅的实现方式是什么(如果这完全有意义的话)?我首先想到的是构建器设计模式,但据我所知,它略有不同。
[编辑] 正如所指出的,我实现它的方式是一种工厂方法,但我对它并不完全满意。 API 可以包含各种不同的接口,这些接口可以完全相互独立,但有些可能依赖于其他接口。(但不是强制性的)我想给用户(使用这个 "API" 的开发人员) ) 尽可能自由地创建他想使用的 API。让我们试着解释一下我基本上在做什么: 假设我正在为游戏引擎开发一个插件,它可以 post 成就和其他内容到各种社交媒体渠道。所以基本上可以有一个接口来实现对 twitter、facebook、youtube 等等或一些自定义服务器的访问。此自定义服务器可能需要某种身份验证过程。用户应该能够在 API 开始时以一种很好的方式构建(嗯,流利很好..)。所以基本上是这样的:
var myTotallyForMyNeedsBuildAPI = API.CreateCustomApi().With(Api.Twitter).And(Api.Facebook).And(Api.Youtube).And(Api.CustomServer).With(Security.Authentification);
我真的不知道怎么说得流利,但像这样就好了。
您实施的是 Factory method 模式。
这对于您尝试做的事情来说非常好,但是您可以根据您的上下文以及您认为代码将如何发展来查看其他工厂模式(即 here)未来。
反正我也会考虑不要把这三个接口绑在一个工厂里。如果它们真的如此紧密地一起使用并一起构建,也许它们首先不应该是三个不同的接口,或者至少所有三个都由相同的 class 实现,因此您的工厂将构建适当的class 正确实施这些。
可能你要的是Decorator pattern。 在您的 API class 中,如果每个接口已提供给 API 实例,您将调用它们,这是 Decorator 模式的行为。 使用此模式,您可以获得模块化实现,允许您向 API.
添加多个行为使用依赖注入 是一种很好的做法,因为您希望让程序员能够组合 具有所需配置的对象。
检查非常适合这项工作的 MEF
和 Unity
框架。
例如在Unity
中你可以这样写:
// Introducing an implementation for ISomeInterface
container.Register<ISomeInterface, SomeImplementation>();
// Introducing an implementation for ISomeOtherInterface
container.Register<ISomeOtherInterface, SomeOtherImplementation>();
// Introducing an implementation for ISomeAnotherInterface
container.Register<ISomeAnotherInterface, SomeAnotherImplemenation>();
container.Register<API, API>();
// and finally unity will compose it for you with desired configurations:
var api = container.Resolve<API>();
在这种情况下,api
将 组合 与所需的实现。