예 A :플랫폼 간 구현을 위해 인터페이스 또는 팩토리 (및 인터페이스)를 사용해야합니까?
// pseudo code
interface IFoo {
void bar();
}
class FooPlatformA : IFoo {
void bar() { /* ... */ }
}
class FooPlatformB : IFoo {
void bar() { /* ... */ }
}
class Foo : IFoo {
IFoo m_foo;
public Foo() {
if (detectPlatformA()} {
m_foo = new FooPlatformA();
} else {
m_foo = new FooPlatformB();
}
}
// wrapper function - downside is we'd have to create one
// of these for each function, which doesn't seem right.
void bar() {
m_foo.bar();
}
}
Main() {
Foo foo = new Foo();
foo.bar();
}
예 B : A는, B는, 둘, 또는 "그것은 의존하지"
더 나은 옵션입니다// pseudo code
interface IFoo {
void bar();
}
class FooPlatformA : IFoo {
void bar() { /* ... */ }
}
class FooPlatformB : IFoo {
void bar() { /* ... */ }
}
class FooFactory {
IFoo newFoo() {
if (detectPlatformA()} {
return new FooPlatformA();
} else {
return new FooPlatformB();
}
}
}
Main() {
FooFactory factory = new FooFactory();
IFoo foo = factory.newFoo();
foo.bar();
}
, 예?
재미있는 대답, 그러나 나는 C#을 사용하고 있습니다 만, 공장은 여전히 참으로 옵션입니다. 이 시나리오에서 공장을 사용하는 데 명확한 단점이 있는지 궁금합니다. –