当然可以。
您所分享的示例对我来说并不是很详细,我无法理解
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);
}