# 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");
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/design-patterns-handbook/structural-patterns/facade.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
