vendredi 16 juillet 2021

Is wrapping a method a kind of design pattern?

Can anyone tell me if this code is a design pattern or not?

Imagine your codebase has hundreds of calls to a method GetJsonAsync. For example:

var request = new HttpRequest(url);

return request.GetJsonAsync<T>();

You want to add a header to every request, so you create a wrapper method like:

public static Task<T> GetJsonWithHeader<T>(this HttpRequest request)
{
    var requestWithHeader = AddHeaders(request);

    return requestWithHeader.GetJsonAsync<T>();
}

Which means those first two lines of code can be changed to:

var request = new HttpRequest(url);

return request.GetJsonWithRequestId<T>();

I called this class a "Wrapper". But is it actually an example of the Decorator pattern? Or some other design pattern?

Aucun commentaire:

Enregistrer un commentaire