2016-12-11 58 views
0

我想知道如果我有一个门面类,我可以创建一个接口,并有多个类的实现。我可以在立面设计模式中使用界面吗?

如:

interface IUserDetailFacade{} 

public class UserDetailsFacade implements IUserDetailFacade{} 

public class UserDetailsLdapFacade implements IUserDetailFacade{} 
+0

没有太多的工作在这里,你能提供一个你的意图的例子。 Facade模式的目的是抽象出多个相关的类层次体系的复杂性。所以,我可能会想念你的问题;然而,虽然外观本身当然是一种抽象,但是它对于您正在尝试讨论的特定子系统具有讽刺意味。在这方面,你的问题没有意义,因为它就像是问'如果我有解决问题的方法,我可以使用相同的解决方案解决同一问题的另一种解决方案'。循环逻辑是你的,我只是把它指出来。 –

回答

1

当然可以。

您已经共享的例子是不是很详细,我听不懂的抽象(在interface要创建的)的又一个层中如何适应。

但让我给你举个例子,这将合理。

假设你正在创建测试不同的C++编译器如何高效地编译相同的源代码的应用程序。

您可以创建CppCompiler作为interface到不同的外墙,每个类型CppCompiler每个类型一个。

public interface CppCompiler { 
    void compile(String sourceFile); 
} 

TurboCppCompilerBorlandCppCompilerGccCppCompiler等都是它做的像解析编译不同的步骤类,组装一个子系统的外墙,链接等。例如,TurboCppCompiler实施将是这个样子。

public class TurboCppCompiler implements CppCompiler { 

    // .. private variables 

    public TurboCppCompiler(TurboParser parser, TurboAssembler assembler, TurboLinker linker) { 
     this.parser = parser; 
     this.assembler = assembler; 
     this.linker = linker; 
    } 

    public void compile(String sourceFile) { 
     /* Compile the code Borland Cpp style using the subsystems Parser, Assembler, Linker */ 
    } 
} 

使用

您可以创建得到一个编译器工厂方法(注意如何CppCompiler用作这里return型)

public static CppCompiler createCppCompiler(CompilerType type) { 
    switch (type) { 
     case TURBO: 
      return new TurboCppCompiler(new TurboParser(), new TurboAssembler(), new TurboLinker()); 
     case BORLAND: 
      return new BorlandCppCompiler(new BorlandParser(), new BorlandAssembler(), new BorlandLinker()); 
     case GCC: 
      return new GccCppCompiler(new GccParser(), new GccAssembler(), new GccLinker()); 
    } 
    throw new AssertionError("unknown compiler type:" + type); 
} 

希望这有助于。

1

你在正确的轨道,因为你的系统的其他部分将与抽象(即你的外观接口)连接,而你能保证你的系统将与多个给定的工作整个外观界面的实现。