# Component

A component is an encapsulated block of code that can be assembled together with a [base](https://polylith.gitbook.io/polylith/architecture/2.2.-base) (it's often just a single base) and a set of components and libraries into services, libraries or tools. Components achieve encapsulation and composability by separating their private implementation from their public interface:

<figure><img src="https://3095132514-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAhrWK1psIWk5h5zNLV%2Fuploads%2FyOiL26V7Rvvx8ykOOnNN%2Fcomponent.png?alt=media&#x26;token=298e216c-36ff-4dd2-9f6e-b0319c606ea1" alt=""><figcaption></figcaption></figure>

Each component lives in a separate directory under the `components` directory, and contains a `src`*,* `test` and `resources` directory:

```
▾ workspace
  ▾ components
    ▾ mycomponent
      ▸ src
      ▸ test
      ▸ resources
```

The `src` directory often contains at least two namespaces, one for the interface and one for the implementation:

```
▾ src
    interface.clj
    core.clj
```

A component's interface is a namespace that exposes a collection of functions for other components or bases to call. Each function in a component’s interface “passes-through” to an equivalent function in its private implementation (the `core` namespace in this example). This “pass-through” approach enables full code navigation and refactoring, whilst maintaining encapsulation. You are allowed to put the implementation directly in the interface, but most of the time you want to separate the two.

Code examples of components can be found in the [RealWorld example app](https://github.com/furkan3ayraktar/clojure-polylith-realworld-example-app) and in the [Polylith Tool](https://cljdoc.org/d/polylith/clj-poly/CURRENT/doc/readme).


---

# 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://polylith.gitbook.io/polylith/architecture/2.3.-component.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.
