在没有单例的情况下优化我的代码逻辑

Optimize my Code logic without singletion

 public interface IHandler
    {
        List<string> Run();
    }

public class Base 
{

    void methodA();
}

public Class Der1 : Base , IHandler
{
     List<string> Run()
     { //Generate huge records
     }
}

public Class Der2 : Base , IHandler
{
     List<string> Run()
     {//Generate huge records
     }
}

public Class Der3 : Base , IHandler
{
     List<string> Run()
     {//Generate huge records
     }
}

目前 运行() 正在所有派生的 class 中执行并生成相同的记录集。我要优化它。

将 运行() 内的 RecordGeneration 进程移动到公共 class/function 并执行一次并准备必要的记录。所有派生 class 将使用此 "RecordGeneration" 来获取已生成的记录。

注意:我无法实现单例模式。

您可以使用 Lazy<T>:

private Lazy<List<string>> l;

public Der1
{
    l = new Lazy<List<string>>(() => Run());
}

public List<string> ResultOfRun
{
    get
    {
         return l.Value();
    }
}

为了扩展我最初的回答,如果该方法在所有方法中具有相同的输出,您可以这样做:

public class Base
{
    private Lazy<List<string>> l =  new Lazy<List<string>>(() => RunStatic());

    private static List<string> RunStatic()
    {
        //
    }

    public List<string> ResultOfRun
    {
        get
        {
             return l.Value();
        }
    }

    void methodA();
}

然后你只需要在 Run 中调用它,如果它实现接口

,它可以在基础 class 中
public Class Der1 : Base , IHandler
{
     List<string> Run()
     {
         return this.ResultOfRun;
     }
}