아마도 "동적"으로 설정할 필요는 없습니다.
Abstract Factory 패턴을 확인 했습니까? 기본적으로 필요한 것은 장치 을 기반으로 각 인터페이스에 대한 구체적인 구현을 만드는 것입니다.
많은 인터페이스를 구현하는 단일 클래스를 가질 필요는 없지만 코드에서 요청할 때 특정 인터페이스를 적절히 구현하면 충분합니다.
추상 팩토리를 구현할 때마다 디바이스 유형에 따라 여러 인터페이스 구현을 생성 할 수 있습니다.
는
예 :
public interface IDeviceFactory
{
ISomething GetSomeInterface();
ISomethingElse GetSomeOtherInterface();
}
한 다음 각 장치에 대한 특정 공장 구현 :
public class SimpleDeviceFactory : IDeviceFactory
{
public virtual ISomething GetSomeInterface()
{ return Something.Empty; }
public virtual ISomethingElse GetSomeOtherInterface()
{ return new SomeSimpleConreteImplementation(); }
}
아니면 : 다음
public class ComplexDeviceFactory : IDeviceFactory
{
public virtual ISomething GetSomeInterface()
{ return new ComplexStuff(); }
public virtual ISomethingElse GetSomeOtherInterface()
{ return new EvenMoreComplexStuff(); }
}
그리고를, 마지막으로, 당신은 바로를 만들 기기 용 공장 :
public class DeviceFactory
{
public static IDeviceFactory CreateForDevice(IDevice device)
{
DeviceType type = device.Type; // or something like this
switch (type)
{
case DeviceType.Simple:
return new SimpleDeviceFactory();
case DeviceType.Complex:
return new ComplexDeviceFactory();
default:
throw new NotImplementedException();
}
}
}
IDeviceFactory 메서드 구현을 virtual
으로 표시 했으므로 특정 장치의 특정 인터페이스를 쉽게 다시 사용하거나 재정의 할 수 있습니다.
첫째, 귀하의 의견과 제안에 대해 모두 감사드립니다. 대단히 감사합니다! 좋아, 내가 직면하고있는 문제에 대해 좀 더 자세히 설명해야한다고 생각한다. 특정 기능을 보유하고 있습니다 (예 : CanMakeCoffe, CanCleanKitchen 등 대부분의 기기에서 구현되는 몇 가지 기능이 있습니다 (예 : CanMakeCoffee. 내가 피할 싶은 것이 는 CDeviceOne에서 이러한 장치에 대한 소스 코드 클래스 어딘가에 정의하는 것입니다 : ICanMakeCoffee, ICanCleanKitchen CDeviceTwo : ICanMakeCoffee –
차라리 그들이 무엇을 할 수 있는지에 연결하면 장치가 말해 거라고 그리고 나는 그들의 능력과 닮은 대상을 창조 할 것입니다. 의미가 있습니까? –