我想创建 Facade 类来处理对两个服务的少量操作。为了联系这些服务,我有代理类。
每个服务都有抽象代理和派生代理 - 我如何创建这部分架构,以避免在 Facade 中解析?
class Facade
{
private Proxy proxy1;
private Proxy proxy2;
public Facade()
{
//I don't like this part
proxy1 = ...Resolve<Proxy1Type>();
proxy2 = ...Resolve<Proxy2Type>();
}
public void Do()
{
proxy1.Call();
proxy2.Call();
}
}
abstract class Proxy {public void Call();}
class Proxy1Type : Proxy {public void override Call(){}}
class Proxy2Type : Proxy {public void override Call(){}}
我应该使用什么设计模式来解决这种情况?
编辑
(可选)我应该使用这个解决方案,但仍然不太喜欢它
class Facade
{
private IProxy proxy1;
private IProxy proxy2;
//I feel it's still wrong
public Facade(IProxy1Type p1, IProxy2Type p2)
{
proxy1 = p1;
proxy2 = p2;
}
public void Do()
{
proxy1.Call();
proxy2.Call();
}
}
interface IProxy { void Call();}
interface IProxy1Type : IProxy{}
interface IProxy2Type : IProxy {}
class Proxy1Type : IProxy1Type { public void Call() { } }
class Proxy2Type : IProxy2Type { public void Call() { } }