代码之家  ›  专栏  ›  技术社区  ›  Mohan Seth

我可以在外观设计模式中使用接口吗?

  •  0
  • Mohan Seth  · 技术社区  · 9 年前

    例如。:

    interface IUserDetailFacade{}
    
    public class UserDetailsFacade implements IUserDetailFacade{}
    
    public class UserDetailsLdapFacade implements IUserDetailFacade{}
    
    2 回复  |  直到 9 年前
        1
  •  2
  •   Pravin Sonawane    9 年前

    当然可以。

    您所分享的示例对我来说并不是很详细,我无法理解 interface 你想要创建)适合。

    但让我举一个例子,说明这是有意义的。

    实例

    您可以创建 CppCompiler 作为 界面 不同立面,每种类型各一个 CPP编译器 .

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

    TurboCppCompiler , BorlandCppCompiler , GccCppCompiler 正面 类的子系统,这些子系统在编译中执行不同的步骤,如解析、组装、链接等。例如, TurboCPP编译器 实现看起来像这样。

    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 */
        }
    }
    

    您可以创建一个获取编译器的工厂方法(注意 CPP编译器 用作 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);
    }
    

        2
  •  1
  •   Matías Fidemraizer    9 年前

    您走在正确的轨道上,因为您的系统的其他部分将与抽象(即您的外观接口)耦合,同时您可以保证您的系统将与整个外观接口的多个给定实现一起工作。

    推荐文章