Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page

Was this helpful?

  1. Structural Patterns

Facade

Facade purpose is to hide complex libraries API and provide simple class or set of classes that are meant to be used by users of a library.

Example - Document reader

We are going to use Facade pattern to provide API for complex readers that can read various document types, like MS Word or Text files.

These are the readers that should be hidden from users.

class TextReader {

    public TextReader(String document) {
    }

    public String getText() {
        return "Content of a simple text fiel";
    }
}

class WordReader {

    public WordReader(String document) {
    }

    public String getText() {
        return "Content of a MS Word document.";
    }
}

We create a facade class called DocumentReader to provide easy API for library users. We usually put facade classes into library root, so it is the first thing a library user sees.

class DocumentReader {

    public String read(String document) {
        if (document.endsWith(".docx")) {
            return new WordReader(document).getText();
        } else if (document.endsWith(".txt")) {
            return new TextReader(document).getText();
        }

        return "";
    }
}

Here is how users of our library would use the facade.

DocumentReader reader = new DocumentReader();

String wordText = reader.read("resume.docx");
String text = reader.read("resume.txt");
PreviousFlyweightNextBridge

Last updated 5 years ago

Was this helpful?