jeudi 5 octobre 2023

OOP - Is there a name for the design pattern of using class properties or lambdas to wrap static methods (of the same name)?

I'm working with a C# class at the moment that exposes some lambda [instance] methods which serve as wrappers for static methods of the same name.

Here is an example snippet:

public class UserUI : DataUI
{
  // FYI: the `Context` object here is inherited from the `DataUI` base class
  public string GetUserId() => GetUserId(Context);

  public static string GetUserId(IDataUIContext context)
  {
    // ... logic here for pulling id from `context` ...
    return userId;
  }
}

I'm wondering: is there a name for such a design pattern as this?


Additionally, would it be possible to accomplish the same functionality using a class property here rather than an instance method / lambda?

Like so:

public class UserUI : DataUI
{
  public string GetUserId => GetUserId(Context);

  public static string GetUserId(IDataUIContext context)
  {
    // ... logic here for pulling id from `context` ...
    return userId;
  }
}

And, if so, is there any discernible difference btw using a property / getter instead? (other than the method having to be invoked as opposed to being accessed like a field, obviously)

Aucun commentaire:

Enregistrer un commentaire