# 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: 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/anti-patterns/generic-interface-over-function.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.
