views:

133

answers:

2

I'm creating a base class Node that essentially wraps instances of another class from a program I am writing a plugin for, BaseAppObject. Properties of Node and any derivation of Node store their properties in the BaseAppObject through two methods BaseAppObject.SetUserString(key,value) and BaseAppObject.GetUserString(key,value).

There are two situations where I need to instantiate a Node. One where the BaseAppObject is clean, and has no user strings. In this case the constructor needs to be passed the values of all the properties to initiate. The second is when I have a BaseAppObject that already has user strings defined and I simply just want to wrap it with a Node object. In that case I only need to run the initialization constructor that sets the _baseObject field. All other properties of the object will then simply read from user strings set in the BaseAppObject.

Here's a simplified version of what my classes look like.

public abstract class Node
{
    BaseAppObject _baseObject;

    //reinitializing constructor
    public Node(BaseAppObject baseObject)
    {
         this._baseObject = baseObject;
    }

    //initializing constructor
    public Node(BaseAppObject baseObject, string name)
    {
        this._baseObject = baseObject;
        this.Name = name;
    } 

    public string Name
    {
        get { 
                string name = "";
                _baseObject.GetUserString("CPName", ref name);
                return name;             
            }

        set {
                _baseObject.SetUserString("CPName", value);
            }
    }
}

Other classes derived from Node may add additional properties like this.

public CustomClass:Node
{

    //reinitializing constructor
    public CustomClass(BaseAppObject baseObj):base(baseObj)
    {
    }


    //initializing constructor
    public CustomClass(BaseAppObject baseObj,string name, string color):base(baseObj,name)
    {

        //here's an additional property added to CustomClass
        public string Color
        {
            get { 
                    string name = "";
                    this.BaseObject.GetUserString("Color", ref name);
                    return name;             
                }
            set {
                    this.BaseObject.SetUserString("Color", value);
                }
        }
}

This works, but later on if I (or someone else) wants to make another class derived from Node they have to understand that the derived class needs to have two constructors. One for initialization and another for reinitialization.

Is there some way to make this implementation intrinsic to the Node class? Or is thorough documentation the only option?

Also is there a way to avoid placing the same single argument constructor (below) in every class derived from node.

//reinitializing constructor
public ClassDerivedFromNode(BaseAppObject baseObject)
{
     this._baseObject = baseObject;
}
A: 

There's no way to force subclasses to create two constructors. Documentation and code reviews are useful, but not foolproof. Otherwise, you might have to add checks in the constructor to ensure that the "CPName" property is set if the reinitialize constructor is called and throw an exception if it's not. Alternately, you can do a Debug.Assert instead of throwing an exception.

Michael Meadows
+1  A: 

You might want to consider Factory Methods as a solution to your problem.

Mark