My goal is to define a Service class which process method accepts a RequestType argument that is defined by the contained ServiceType and returns an object of a type defined by ServiceType. By doing this I'm able to derive Services that can process a given Request that is compatible with the type of the Service , same for the Response.

I really don't know if it is possible and the following diagram is obviously wrong but depicts the result I'm struggling to achieve.

enter image description here

1 Answer 1


Many programming languages support generic types (C#), generics (Java) or class templates (C++). All three terms mean the same thing, but I honestly like the C++ terminology best. The term "class template" is good, because it implies that the class is not the actual class, but a template used by the compiler to create real classes. The intention is to solve the exact problem you are running into. I'll use C#, since that is the language I am most fluent with, but many OOP languages support this:

public abstract class Service<TRequest, TResponse>
    public virtual TResponse process(TRequest)
        // common logic goes here

The identifiers inside the angle brackets TRequest and TResponse are just placeholders for real classes that will be specified when initializing a new service object:

Service<GetBlogRequest, GetBlogReponse> service = new Service<GetBlogRequest, GetBlogReponse>();
GetBlogRequestrequest request = new GetBlogRequest(1200);
GetBlogReponseresponse response = service.process(request);

UML diagrams usually depict class templates or generics using the angle bracket notation as well:

|               <<abstract>>               |
| Service<TRequest, TResponse>             |
| +process(request : TRequest) : TResponse |
  • thank you for your suggestion, the problem here is that Service accepts any derived type of TRequest and TResponse, I need to tied them under a common type so that Service can only accept a TRequest correlated with the specific TResponse May 6, 2021 at 19:20
  • TRequest is not a class. It is a placeholder for a real class that is specified later when other code creates a new instance of the service. Some languages allow you to restrict which types can be specified in generics. For instance in C# you would do class Service<TRequest, ...> where TRequest : RequestType. May 6, 2021 at 19:29
  • I wrongly expressed myself. My goal design is not to define a template because each different Service that accepts a different correlated couple of <TRequest, TResponse> must define a different process logic in advance May 6, 2021 at 19:46
  • Ok. So override process in the child class, or make process an abstract method. May 6, 2021 at 19:47
  • The language you're using feels like the Mediator pattern. @GregBurghardt is using a c# example - here is a good github repo that implements a more detailed version of what Greg was doing: github.com/jbogard/MediatR/wiki
    – Patrick
    May 6, 2021 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.