help me to choose between two designs

Posted by alex on Stack Overflow See other posts from Stack Overflow or by alex
Published on 2010-05-24T06:54:02Z Indexed on 2010/05/24 7:11 UTC
Read the original article Hit count: 202

Filed under:
|
|
|

// stupid title, but I could not think anything smarter

I have a code (see below, sorry for long code but it's very-very simple):

namespace Option1
{
    class AuxClass1
    {
        string _field1;
        public string Field1
        {
            get
            {
                return _field1;
            }
            set
            {
                _field1 = value;
            }
        }

        // another fields. maybe many fields maybe several properties

        public void Method1()
        {
            // some action
        }

        public void Method2()
        {
            // some action 2
        }
    }

    class MainClass
    {
        AuxClass1 _auxClass;
        public AuxClass1 AuxClass
        {
            get
            {
                return _auxClass;
            }
            set
            {
                _auxClass = value;
            }
        }

        public MainClass()
        {
            _auxClass = new AuxClass1();
        }
    }
}

namespace Option2
{
    class AuxClass1
    {
        string _field1;
        public string Field1
        {
            get
            {
                return _field1;
            }
            set
            {
                _field1 = value;
            }
        }

        // another fields. maybe many fields maybe several properties

        public void Method1()
        {
            // some action
        }

        public void Method2()
        {
            // some action 2
        }
    }

    class MainClass
    {
        AuxClass1 _auxClass;

        public string Field1
        {
            get
            {
                return _auxClass.Field1;
            }
            set
            {
                _auxClass.Field1 = value;
            }
        }

        public void Method1()
        {
            _auxClass.Method1();
        }

        public void Method2()
        {
            _auxClass.Method2();
        }

        public MainClass()
        {
            _auxClass = new AuxClass1();
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Option1
        Option1.MainClass mainClass1 = new Option1.MainClass();
        mainClass1.AuxClass.Field1 = "string1";
        mainClass1.AuxClass.Method1();
        mainClass1.AuxClass.Method2();

        // Option2
        Option2.MainClass mainClass2 = new Option2.MainClass();
        mainClass2.Field1 = "string2";
        mainClass2.Method1();
        mainClass2.Method2();

        Console.ReadKey();
    }
}

What option (option1 or option2) do you prefer ? In which cases should I use option1 or option2 ? Is there any special name for option1 or option2 (composition, aggregation) ?

© Stack Overflow or respective owner

Related posts about c#

Related posts about best-practices