# Prototype

Prototype design pattern suggest to create a clone of a prototype rather than using `new` keyword and deep-copy all values to a clonned instance.

## Example - Simple Prototype

In order to implement prototype patern, we can simply implement Clonable interface from Java.

```
class HumanCell implements Cloneable {

    private String dna;

    public HumanCell(String dna) {
        this.dna = dna;
    }

    public String getDna() {
        return dna;
    }

    // all complex objects must be recreated (lists, maps, POJOs)
    public Object clone() throws CloneNotSupportedException {
        return new HumanCell(dna);
    }
}
```

Then we can clone human cells like this.

```
HumanCell cell = new HumanCell("DNA123");
HumanCell identical = (HumanCell) cell.clone();
```

## Example - Define own prototype interface

We can create our own interface that will define how a prototype should be copied into a new class.

```
interface Cell {
    Cell split();
    String getDna();
}

class SingleCellOrganism implements Cell {

    private String dna;

    public SingleCellOrganism(String dna) {
        this.dna = dna;
    }

    public String getDna() {
        return dna;
    }

    // all complex objects must be recreated (lists, maps, POJOs)
    public Cell split() {
        return new SingleCellOrganism(dna);
    }
}
```

Then we can use it as follows and we can exclude casting Object into a specific type.

```
Cell cell = new SingleCellOrganism("DNA123");
Cell identical = i1.split();
```


---

# 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/chapter1/prototype.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.
