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 |
+------------------------------------------+