简体   繁体   中英

How to better implement base class property extencibility mechanism with OOP design patterns

In my project I have hierarchy of classes which implements custom properties. Here is more close to what I want version for the console application.

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();
    }
}

Base abstract class defines a configuration of its property objects which are intended to be inherited in derived classes.

In the constructor configuration array is passed to MergeWithInheritedConfigsAndCreateInstances method call with second parameter set to true indicating that the instantiation of property objects have to be postponed.

Current state of the merged property configuration is stored in the mergedConfigs Dictionary .

Derived classes pass local property configuration to merge/override with base class configuration calling MergeWithInheritedConfigsAndCreateInstances method in its constructor with second parameter set to its default value false indicating that now property instances have to be created after merge.

The resulting output is below.

DerivedClassA.Properties: (key=p1: value=v1),(key=p2: value=v2)
DerivedClassB.Properties: (key=p1: value=v1),(key=p2: value=True),(key=p3: value=v3)
DerivedClassC.Properties: (key=p1: value=v1),(key=p2: value=False),(key=p4: value=v4)

And it is what I needed, but there are some disadvantages of this solution which I do not like:

1) It is necessary to call MergeWithInheritedConfigsAndCreateInstances in each constructor. Second parameter have to be provided in the abstract class constructor.

I'd like to have a solution where all the merge/instantiate mechanism was implemented and invoked in the base class. And to be able to define derived class specific property configuration not as a method parameter but rather as member field/property (maybe of static?).

2) The merging process is done each time class is instantiated.

I'd prefer to have it done only once. (placing in static constructor?)

UPD: Rewritten example code which is better demonstrates the intended idea.

I think you're over-complicating things. Even for a complicated setup, this should suffice:

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";
    }
}

But frankly, this is simpler and more obvious:

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";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM