tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Design Patterns > Java Design Patterns > Facade : Compiler Example

Facade : Compiler Example 

Facade pattern comes under Structural design pattern. It provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Behaviour & Advantages

  • Simplifies the subsystem usage.
  • Acts as entry point to the subsystem.
  • Shields clients from subsystem components.
  • The interaction and communication among subsystems is hidden from client.
  • Facade
    Provides a simplified API to interact with subsystems. It knows how to handle a request and manages request processing with subsystems.
  • Subsystem classes
    Complex set of classes designed to perform a specific task.
  • Client
    One who uses Facade to interact with subsystems.

This example shows a Compiler Facade which shield the users from directly interacting with Subsystems such as Scanner, Parser, Assembler and Linker.

Compiler and subsystem classes are shown below,

File Name  :  
com/bethecoder/tutorials/dp/facade3/Compiler.java 
   
package com.bethecoder.tutorials.dp.facade3;

public class Compiler {

  Parser parser = new Parser();
  Assembler assembler = new Assembler();
  Linker linker = new Linker();
  
  public void compile(String sourceFile) {
    String orgSrcFile = sourceFile;
    parser.parse(sourceFile);
    sourceFile = compileInternal(sourceFile);
    sourceFile = assembler.assemble(sourceFile);
    sourceFile = linker.link(sourceFile);
    
    System.out.println();
    System.out.println("Successfully compiled " + orgSrcFile);
    System.out.println("Final executable is " + sourceFile);
  }
  
  private String compileInternal(String sourceFile) {
    sourceFile = sourceFile.toLowerCase().replaceAll(".cpp"".asm");
    System.out.println("Compiled to assembly " + sourceFile);
    return sourceFile;
  }
}
   

Scanner subsystem.

File Name  :  
com/bethecoder/tutorials/dp/facade3/Scanner.java 
   
package com.bethecoder.tutorials.dp.facade3;

public class Scanner {

  public void scan(String sourceFile) {
    System.out.println("Started scanning " + sourceFile);
  }
}
   

Parser subsystem.

File Name  :  
com/bethecoder/tutorials/dp/facade3/Parser.java 
   
package com.bethecoder.tutorials.dp.facade3;

public class Parser {

  private Scanner scanner = new Scanner();
  
  public void parse(String sourceFile) {
    scanner.scan(sourceFile);
    System.out.println("Started parsing " + sourceFile);
  }
}
   

Assembler subsystem.

File Name  :  
com/bethecoder/tutorials/dp/facade3/Assembler.java 
   
package com.bethecoder.tutorials.dp.facade3;

public class Assembler {

  public String assemble(String sourceFile) {
    sourceFile = sourceFile.toLowerCase().replaceAll(".asm"".obj");
    System.out.println("Translated to binary object code " + sourceFile);
    return sourceFile;
  }
}
   

Linker subsystem.

File Name  :  
com/bethecoder/tutorials/dp/facade3/Linker.java 
   
package com.bethecoder.tutorials.dp.facade3;

public class Linker {

  public String link(String sourceFile) {
    sourceFile = sourceFile.toLowerCase().replaceAll(".obj"".exe");
    System.out.println("Linked to executable " + sourceFile);
    return sourceFile;
  }
}
   

Facade pattern usage is shown below,

File Name  :  
com/bethecoder/tutorials/dp/facade3/Test.java 
   
package com.bethecoder.tutorials.dp.facade3;

public class Test {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    /**
     * The client deals with Compiler
     * which generates the final executable.
     
     * He does'nt need to know how
     * Scanner, Parser, Assembler and
     * Linker interact.
     */
    Compiler compiler = new Compiler();
    compiler.compile("C:\\random.cpp");
  }

}
   

It gives the following output,
Started scanning C:\random.cpp
Started parsing C:\random.cpp
Compiled to assembly c:\random.asm
Translated to binary object code c:\random.obj
Linked to executable c:\random.exe

Successfully compiled C:\random.cpp
Final executable is c:\random.exe


                    Print

   



Books Recommendation

 
 
bl  br