首页 > 解决方案 > 如何覆盖方法保存模板方法模式

问题描述

我确实有一个抽象类,在构造函数上我正在调用在同一个类上声明的抽象方法,我这样做是因为我确实想强制所有派生类不仅实现我的抽象方法而且调用它(我认为这就是模板方法模式的用武之地)。

顺便说一句:我知道这不是确保所有内容都正确编写的最佳方法,但由于外部问题,我想尽量减少我未来因实施不当而出现的问题。片段如下:

public abstract class Generic
{
    public Generic()
    {
        Console.WriteLine("This is the generic");

        this.SetRules();
    }

    protected abstract void SetRules();
}

好吧,在我的一个派生类上需要在声明上设置一个私有变量,我想在重写方法中使用这个变量。由于我不知道我的每个派生类需要多少种可能性,我想根据需要在我的每个派生类中声明它。下面的一个例子:

public class Derivated2 : Generic
{
    private int _parameter;

    public Derivated2(int parameter)
    {
        this._parameter = parameter;
    }

    protected override void SetRules()
    {
        int x = 0;
        x = 1 + 3;

        Console.WriteLine(String.Concat("This is the derivated2 with x: ", x, "; _parameter: ", _parameter));
    }
}

现在来解决问题;

当我调用我的 derivated2 类时,我无法使用/查看声明中指定的变量值:

public static void Main()
{
    Derivated2 _class1 = new Derivated2(2);
}

我的输出是:

This is the generic
This is the derivated2 one with x: 4; _parameter: 0

如何在覆盖的 asbtract 方法上查看/使用我的变量?如果我不能,那么无论开发人员做什么并且还可以在他的派生类中使用私有变量,实现我的目标的最佳方法是什么,确保 SetRules 方法将被调用?

这是我完成的完整代码:

在抽象泛型类上调用 SetRules

在派生类上调用 SetRules

标签: c#asp.nettemplatesdesign-patterns

解决方案


问题是您正在调用 Generic 构造函数,它将在调用 Derivated2 构造函数之前调用 SetRules,这意味着您的参数尚未设置。你可以试试这个。

public abstract class Generic
{
    public Generic(int parameter)
    {
        Console.WriteLine("This is the generic");

        this.SetRules(parameter);
    }

    protected abstract void SetRules(int paramter);
}

public class Derivated2 : Generic
{

    public Derivated2(int parameter) : base(parameter){ }

    protected override void SetRules(int parameter)
    {
        int x = 0;
        x = 1 + 3;

        Console.WriteLine(String.Concat("This is the derivated2 with x: ", x, "; _parameter: ", parameter));
    }
}
public static void Main()
{
    Derivated2 _class1 = new Derivated2(2);
}

正如您所说,每次要创建新类时都必须调用 SetRules 并不好,所以这意味着您不必(快速将其放在一起,因此您需要修复它)

public abstract class Generic
{
    public Generic()
    {
        Console.WriteLine("This is the generic");
    }

    public abstract void SetRules();
}

public sealed class Derivated2 : Generic
{
    int _param;

    public Derivated2(RulesWithParameters rules) {
        _param = rules.Parameter;
    }

    public override void SetRules()
    {
        int x = 0;
        x = 1 + 3;

        Console.WriteLine(String.Concat("This is the derivated2 with x: ", x, "; _parameter: ", _param));
    }
}
public void Main()
{
    var rules = new RulesWithParameters{
        Parameter = 5
    };
    var _class1 = FactoryMethod<Derivated2>(rules);

    var _class2 = FactoryMethod<Derivated1>(null);
}

public class Derivated1 : Generic
{
    public Derivated1(Rules rules)
    {

    }
    public override void SetRules()
    {
        Console.WriteLine("This is the derivated1");
    }
}
public class Rules
{
    public string RuleName {get; set;}
}

public class RulesWithParameters : Rules{
    public int Parameter { get; set;}
}

public Generic FactoryMethod<T>(Rules rules) where T : Generic
{
    T instance = (T)Activator.CreateInstance(typeof(T), rules);;
    instance.SetRules();
    return instance;
}

推荐阅读