> For the complete documentation index, see [llms.txt](https://ondrej-kvasnovsky-2.gitbook.io/design-patterns-handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ondrej-kvasnovsky-2.gitbook.io/design-patterns-handbook/anti-patterns/generic-interface-over-function.md).

# Generic Interface over Function

We should create generic interfaces only if they are adding value and increasing readability of the code.

```
export interface MessageTranslator<A, B> {
  to(from: A): B;
}
```

```
export class Processor {
  private detectIntentRequestTranslator: MessageTranslator<DetectIntentRequestDF, DetectIntentRequest>;
  private detectIntentResponseTranslator: MessageTranslator<DetectIntentResponse, DetectIntentResponseDF>;

  constructor() {
    this.detectIntentRequestTranslator = new DetectIntentRequestTranslator();
    this.detectIntentResponseTranslator = new DetectIntentResponseTranslator();
  }

  doSomething(): number {
    const request: DetectIntentRequest = this.detectIntentRequestTranslator.to(call.request);
    const resposne: DetectIntentResponse = this.detectIntentResponseTranslator.to(call.response);
    // do something
    return 1;
  }
}
```

The code above could look like this. We could just create the transformation functions and use them directly, because they are pure functions.

```
function toIntentRequest(request): DetectIntentRequest {
  return new DetectIntentRequest();
}

function toIntentResponse(request): DetectIntentResponse {
  return new DetectIntentResponse();
}

export class Processor {

  doSomething(): number {
    const request = toIntentRequest(call.request);
    const response = toIntentResponse(call.response);
    // do something
    return 1;
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/design-patterns-handbook/anti-patterns/generic-interface-over-function.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
