Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用OOP设计模式更好地实现基类属性扩展机制_C#_Oop_Design Patterns_Extensibility - Fatal编程技术网

C# 如何用OOP设计模式更好地实现基类属性扩展机制

C# 如何用OOP设计模式更好地实现基类属性扩展机制,c#,oop,design-patterns,extensibility,C#,Oop,Design Patterns,Extensibility,在我的项目中,我有实现自定义属性的类的层次结构。这里更接近我想要的控制台应用程序版本 class Property { string key; object value; public Property(string key, object value) { this.key = key; this.value = value; } public override string ToString() {

在我的项目中,我有实现自定义属性的类的层次结构。这里更接近我想要的控制台应用程序版本

class Property
{
    string key;
    object value;

    public Property(string key, object value)
    {
        this.key = key;
        this.value = value;
    }

    public override string ToString()
    {
        return "(key=" + key + ": value=" + value + ")";
    }
}

public struct PropertyConfig
{
    public string key;
    public object defaultValue;
}

abstract class BaseClass
{
    Dictionary<string, Property> properties = new Dictionary<string, Property>();

    Dictionary<string, PropertyConfig> mergedConfigs = new Dictionary<string, PropertyConfig>();

    public BaseClass()
    {
        MergeWithInheritedConfigsAndCreateInstances(
            new PropertyConfig[]
            {
                new PropertyConfig() { key = "p1",  defaultValue = "v1" },
                new PropertyConfig() { key = "p2",  defaultValue = "v2" }
            }, 
            true);
    }

    protected void MergeWithInheritedConfigsAndCreateInstances(PropertyConfig[] configs = null, bool IsBaseClass = false)
    {
        configs = configs ?? new PropertyConfig[] { };

        foreach (PropertyConfig config in configs)
        {
            mergedConfigs[config.key] = config;
        }

        if (!IsBaseClass)
        {
            CreatePropertyInstancesAfterMerge();
        }
    }

    private void CreatePropertyInstancesAfterMerge()
    {
        foreach (KeyValuePair<string, PropertyConfig> kvp in mergedConfigs)
        {
            PropertyConfig config = kvp.Value;

            properties.Add(config.key, new Property(config.key, config.defaultValue));
        }
    }

    public override string ToString()
    {
        return GetType().Name + ".Properties: " + string.Join(",", properties.Select(kvp => kvp.Value.ToString()).ToArray());
    }
}

class DerivedClassA : BaseClass
{
    public DerivedClassA(): base()
    {
        MergeWithInheritedConfigsAndCreateInstances();
    }
}

class DerivedClassB : BaseClass
{
    public DerivedClassB() : base()
    {
        MergeWithInheritedConfigsAndCreateInstances(new PropertyConfig[]
        {
            new PropertyConfig() { key = "p2",  defaultValue = true },
            new PropertyConfig() { key = "p3",  defaultValue = "v3" }
        });
    }
}

class DerivedClassC : BaseClass
{
    public DerivedClassC() : base()
    {
        MergeWithInheritedConfigsAndCreateInstances(new PropertyConfig[]
        {
            new PropertyConfig() { key = "p2",  defaultValue = false },
            new PropertyConfig() { key = "p4",  defaultValue = "v4" }
        });
    }
}

class Program
{
    static void Main(string[] args)
    {
        DerivedClassA derivedA = new DerivedClassA();
        DerivedClassB derivedB = new DerivedClassB();
        DerivedClassC derivedC = new DerivedClassC();

        Console.WriteLine(derivedA.ToString());
        Console.WriteLine(derivedB.ToString());
        Console.WriteLine(derivedC.ToString());


        Console.ReadLine();
    }
}
这正是我所需要的,但是这个解决方案有一些我不喜欢的缺点:

1) 必须在每个构造函数中调用
mergewithInheritedConfigandCreateInstances
。第二个参数必须在抽象类构造函数中提供

我希望有一个解决方案,其中所有合并/实例化机制都是在基类中实现和调用的。并且能够将派生类特定的属性配置定义为成员字段/属性(可能是静态的?),而不是方法参数

2) 合并过程在每次实例化类时完成

我宁愿只做一次。(放置在静态构造函数中?)


UPD:重写的示例代码更好地演示了预期的想法。

我认为你把事情复杂化了。即使对于复杂的设置,这也应足够:

class BaseClass
{
    private readonly Dictionary<string, string> properties = new Dictionary<string, string>();
    protected string this[string key]
    {
        get { string value; return properties.TryGetValue(key, out value) ? value : null; }
        set { if (value == null) properties.Remove(key); else properties[key] = value; }
    }

    public BaseClass()
    {
        this["p1"] = "v1";
        this["p2"] = "v2";
    }

    public override string ToString()
    {
        return GetType().Name + ".Properties: " + string.Join(",", properties.Select(kvp => $"{kvp.Key}:{kvp.Value}"));
    }
}

class DerivedClass : BaseClass
{
    public DerivedClass() : base()
    {
        this["p2"] = "update";
        this["p3"] = "v3";
    }
}

我觉得你把事情复杂化了。即使对于复杂的设置,这也应足够:

class BaseClass
{
    private readonly Dictionary<string, string> properties = new Dictionary<string, string>();
    protected string this[string key]
    {
        get { string value; return properties.TryGetValue(key, out value) ? value : null; }
        set { if (value == null) properties.Remove(key); else properties[key] = value; }
    }

    public BaseClass()
    {
        this["p1"] = "v1";
        this["p2"] = "v2";
    }

    public override string ToString()
    {
        return GetType().Name + ".Properties: " + string.Join(",", properties.Select(kvp => $"{kvp.Key}:{kvp.Value}"));
    }
}

class DerivedClass : BaseClass
{
    public DerivedClass() : base()
    {
        this["p2"] = "update";
        this["p3"] = "v3";
    }
}

谢谢当然,我过于简化了代码。任务是实现某种依赖属性系统。每个属性类都有其逻辑。基类为不同类型的属性值实现pub/sub、getter/setter。相互参照。可以动态添加/删除属性。对于这个问题,我需要为属性系统的一部分找到解决方案,该部分提供基类中定义的属性的初始化,并允许继承/重写/合并它们与派生类中定义的属性集。如果它过于简单化,请询问,我将重新编写它。谢谢!,当然,我过于简化了代码。任务是实现某种依赖属性系统。每个属性类都有其逻辑。基类为不同类型的属性值实现pub/sub、getter/setter。相互参照。可以动态添加/删除属性。对于这个问题,我需要为属性系统的一部分找到解决方案,该部分提供基类中定义的属性的初始化,并允许继承/重写/合并它们与派生类中定义的属性集。如果它过于简单,请询问,我将重新格式化它。仅供参考:这就是XAML/WPF在具有依赖属性的情况下的工作方式……在我的代码中,我不能依赖WPF结构。虽然需求看起来很相似,但wpf依赖属性系统的内部实现对其域非常具体,实现细节不能在这里按原样重用。这不是重用它们的具体类,而是它背后的思想…:/仅供参考:这就是XAML/WPF在具有依赖性属性的情况下的工作方式……在我的代码中,我不能依赖WPF结构。虽然需求看起来很相似,但wpf依赖属性系统的内部实现对其域非常具体,实现细节不能在这里按原样重用。这不是重用它们的具体类,而是它背后的思想…:/
class BaseClass
{
    public string P1 {get;set;}
    public string P2 { get; set; }
    public BaseClass()
    {
        P1 = "v1";
        P2 = "v2";
    }
    public override string ToString()
    {
        return GetType().Name + ".Properties: " + string.Join(",", GetType().GetProperties(
            BindingFlags.Public | BindingFlags.Instance).Select(p => $"{p.Name}:{p.GetValue(this)}"));
    }
}

class DerivedClass : BaseClass
{
    public string P3 { get; set; }
    public DerivedClass() : base()
    {
        P2 = "update";
        P3 = "v3";
    }
}