# Flyweight

Flyweight is inteded to use sharing to support large numbers of fine-grained objects efficiently.

Good example to explain usage Flyweight is UIKit by Apple. When we create TableView that is can display milions rows to the user, we know that user can see only few at the time. What Apple engineers did that they are reusing components that are on the table view to avoid any performance issues.

## Example - UI component factory

When our UI table should display a button, it will use ButtonFactory to get a button (keeping track of what buttons are not used and visible is omitted). Number that is the parameter in `get` method could be an ID of a button in a table row (but it is not really important in this example).

This example intent is to show that Flyweight is some kind of cash for objects that are memory-heavy or difficult to create.

```
class Button {
}

class ButtonFactory {

    private Map<Integer, Button> cache = new HashMap<>();

    public Button get(Integer number) {
        if (cache.containsKey(number)) {
            return cache.get(number);
        } else {
            Button button = new Button();
            cache.put(number, button);
            return button;
        }
    }
}
```

Then we would use the factory whenever we need an instance of a button that can be reused.

```
ButtonFactory buttonFactory = new ButtonFactory();

Button button1a = buttonFactory.get(1);
Button button1b = buttonFactory.get(1);
```


---

# 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/flyweight.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.
