AweSIM
AweSIM

Reputation: 1703

Categorizing class functions in groups

Imagine a class as follows.. It's a class provided to me to work with.. I cannot change its source..

public class MyClass
{
    object _Object { get; set; }

    public void FuncA1() { _Object = new object(); }
    public void FuncA2() { _Object = new List<object>(); }

    public int FuncB1() { _Object = 0; return 0; }
    public int FuncB2() { _Object = 123; return 123; }

    public string FuncC1() { _Object = null; return null; }
    public string FuncC2() { _Object = "Hello"; return "Hello"; }
}

Im trying to create a wrapper for this class, such that I can group its many functions into categories..

MyWrapper.Voids.FuncA1();
MyWrapper.Voids.FuncA2();
MyWrapper.Integers.FuncB1();
MyWrapper.Integers.FuncB2();
MyWrapper.Strings.FuncC1();
MyWrapper.Strings.FuncC2();

The only solution I can think of for this scenario is to design the wrapper like this:

public class MyWrapper
{
    MyClass _Instance { get; set; }
    public _Void Voids { get; private set; }
    public _Integer Integers { get; private set; }
    public _String Strings { get; private set; }

    public class _Void
    {
        MyWrapper _Parent { get; set; }

        public void FuncA1() { _Parent._Instance.FuncA1(); }
        public int FuncA2() { return _Parent._Instance.FuncA2(); }
    }
    public class _Integer
    {
        ...
    }
    public class _String
    {
        ...
    }

    public MyWrapper()
    {
        _Instance = new MyClass();
        Voids = new _Voids(this);
        Integers = new _Integer(this);
        Strings = new _String(this);
    }
}

This solution works, but has a number of problems: - The inner classes are forced to be public, which allows them to be instantiated by the user.. - I am forced to maintain a reference of the parent object in the child classes..

Is there a better way of doing this?

EDIT: The code posted initially was a bit confusing, in the sense that it was diverting attention away from the core issue and more into the issues of whether a function would cause exceptions or not if they all work on the same object..

NOTE: This is not actual code.. I hacked together this example to show what I'm trying to do.. CREATE A WRAPPER AROUND AN OBJECT (I cannot change the original object's code) AND GROUP FUNCTIONS INTO CATEGORIES..

FINAL EDIT: following suggestion by Juharr.. here's what ive done to accomplish what i wanted.. for the betterment of others..

public interface IVoid
{
    void FuncA1();
    void FuncA2();
}
public interface IInteger
{
    int FuncB1();
    int FuncB2();
}
public class MyWrapper
{
    public MyClass Instance { get; private set; }
    public IVoid Voids { get; private set; }
    public IInteger Integers { get; private set; }

    private abstract class MyBase
    {
        protected MyWrapper Parent { get; set; }
        protected MyClass Instance { get { return Parent.Instance; } }
        public MyBase(MyWrapper oParent) { Parent = oParent; }
    }
    private class MyVoid : MyBase, IVoid
    {
        public MyVoids (MyWrapper oParent) : base(oParent) { }
        public void FuncA1() { Instance.FuncA1(); }
        public void FuncA2() { Instance.FuncA2(); }
    }
    private class MyInteger : MyBase, IInteger
    {
        public MyInteger (MyWrapper oParent) : base(oParent) { }
        public int FuncB1() { return Instance.FuncB1(); }
        public int FuncB2() { return Instance.FuncB2(); }
    }

    public MyWrapper()
    {
        Instance = new MyClass();
        Voids = new MyVoid(this);
        Integers = new MyInteger(this);
    }
}

Upvotes: 2

Views: 1569

Answers (1)

juharr
juharr

Reputation: 32286

You could write public interfaces instead. Then your inner classes don't have to be public. So something like this.

public interface IIntger
{
    void Set(int iValue);
    int Get();
}

public class MyWrapper
{
    MyClass _Instance { get; set; }
    public IInteger Integer { get; private set; }

    private class _Integer : IInteger
    {
        MyWrapper _Parent { get; set; }

        public void Set(int iValue) { _Parent._Instance.IntegerSet(iValue); }
        public int Get() { return _Parent._Instance.IntegerGet(); }
    }

    public MyWrapper()
    {
        _Instance = new MyClass();
        Integer = new _Integer(this);
    }
}

EDIT:

To answer the second part of your question you will either need the reference to the parent class or a reference to the class you are wrapping. So you could have this instead.

public class MyWrapper
{
    public IInteger Integer { get; private set; }

    private class _Integer : IInteger
    {
        MyClass _Instance { get; set; }
        public _Integer(MyClass myClass) { _Instance = myClass; }
        public void Set(int iValue) { _Instance.IntegerSet(iValue); }
        public int Get() { return _Instance.IntegerGet(); }
    }

    public MyWrapper(MyClass instance)
    {
        Integer = new _Integer(instance);
    }
}

Upvotes: 3

Related Questions