samedi 16 octobre 2021

name of pattern for setting values for an object instance

Is there a pattern or naming convention for a class/function which sets properties for an existing instance of a class? I believe the word "factory" doesn't apply here because the desired function we already have an instance of the class. The function I need to develop would accept the instance as an argument then set default values. Please ignore the C# implementation detail (and the extension methods solution) because the naming convention will be used for more than C# programming. In the example below, I already have an instance of CacheProvider and want to set global values to it. The instance is provided by a third-party library and I don't have control over where it's instantiated.

public interface ICacheProvider {
  //...
}

public class CacheProvider : ICacheProvider {
  public string Name {get;set;}
  public ICacheProvider DefaultCacheProvider {get;set;}
  public ICacheProvider FallbackCacheProvider {get;set;}
}

// What should this class be named?  Is factory still correct here?
public static class GlobalCacheProvider {
  public static ICacheProvider SetDefaults(ICacheProvider cacheProvider){
    cacheProvider.DefaultCacheProvider = ...;
    cacheProvider.FallbackCacheProvider = ...;

    return cacheProvider;
  }
}

// Example usage
var cacheProvider = GlobalCacheProvider.SetDefaults(
  new CacheProvider{
    name = "Some Cache Provider"
  )
};

Aucun commentaire:

Enregistrer un commentaire