Best way for creating a concrete class?-VBForums
Results 1 to 7 of 7

Thread: Best way for creating a concrete class?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Best way for creating a concrete class?

    So I have a base class which has protected properties and also implements an interface.
    The method in the interface, its signature has been changed in the base class to be abstract as I want the children/inheritors override/implement this method.


    So now I will have a range of classes, lets call them:

    Baby,
    Toddler,
    Child,
    Teenager


    All this inherit from "HumanBase", which implements an interface as explained above.

    Now, I need to have some factory of some kind where it will return me the correct type of object but as an Interface i.e:

    Code:
    public IHuman Create(string stuff)
    {
        // here create the concrete class and return it
    }
    so thats ok.

    But here is the problem:

    how would I set all the base properties etc... from the HumanBase from this factory type pattern? I know you can only access protected properties from the base class itself and also the children that inherit from the base class.
    What I mean is, should each of the classes (baby, toddler etc...) have a constructor which takes in the parameters and then in the constructor let it set itself up?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,208

    Re: Best way for creating a concrete class?

    I read your post and now am confused. Are you attempting to inject (pardon the terminology) a method in your base class and want this method will be available to the children derived from that base class?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3
    Addicted Member
    Join Date
    Jan 2008
    Location
    Tennessee
    Posts
    130

    Re: Best way for creating a concrete class?

    Keep in mind that an abstract class is different from an interface. An interface serves as a contract stating that an implementing object has to implement the "contract" properties and methods. An abstract can force an inheriting object to override abstract properties and methods, but can also contain functionality. I know sometimes it makes sense to use a base to refactor common methods, while you would prefer to leverage the flexibility of basing object operations on the interfaces. In this case, you could implement the interface with the abstract and the concrete objects. Since the interface gets implemented with both, it's contract is understood by the concrete objects (Child, etc) without having to reimplement them. This is because the concrete objects will inherit from the base that already implemented them. See attached class diagram.

    As for the coding.... This is what I did:


    also... note how in the Toddler and Baby classes I implemented the constructors to initialize the base property values. I did it two different ways.

    Code:
        public interface IFactory
        {
            IHuman CreateBaby(int age, double height, string hairColor, string eyeColor);
            IHuman CreateToddler(int age, double height, string hairColor, string eyeColor);
            IHuman CreateChild(int age, double height, string hairColor, string eyeColor);
            IHuman CreateTeenager(int age, double height, string hairColor, string eyeColor);
        }
    
    
        public class HumanFactory : IFactory
        {
    
            public IHuman CreateBaby(int age, double height, string hairColor, string eyeColor)
            {
                return new Baby(age, height, hairColor,eyeColor);
            }
    
            public IHuman CreateToddler(int age, double height, string hairColor, string eyeColor)
            {
                return new Toddler(age, height, hairColor,eyeColor);
            }
    
            public IHuman CreateChild(int age, double height, string hairColor, string eyeColor)
            {
                throw new NotImplementedException();
            }
    
            public IHuman CreateTeenager(int age, double height, string hairColor, string eyeColor)
            {
                throw new NotImplementedException();
            }
    
        }
    
    
        public interface IHuman
        {
            int Age { get; set; }
    
            double Height { get; set; }
    
            void AssignToFamily();
        }
    
    
        public abstract class HumanBase:IHuman
        {
            #region Interface members
            public int Age { get; set; }
    
            public double Height { get; set; }
    
            public void AssignToFamily()
            {
    
            }
    
            #endregion
    
            #region base members
    
            protected string HairColor { get; set; }
    
            protected void BuyClothes(string destination)
            {
    
            }
    
            #endregion
    
            #region Abstract members
    
            public abstract string EyeColor { get; set; }
    
            public abstract void WearGlasses();
    
            #endregion
    
            #region Constructors
    
            public HumanBase()
            { }
    
            public HumanBase(int age, double height, string hairColor)
            {
                this.Age = age;
                this.Height = height;
                this.HairColor = hairColor;
            }
    
            #endregion
        }
    
    
    
    
        public class Baby : HumanBase,IHuman
        {
            public Baby(int age, double height, string hairColor, string eyeColor)
                : base()
            {
                this.Age = age;
                this.Height = height;
                this.HairColor = hairColor;
                this.EyeColor = eyeColor;
            }
    
    
            public override string EyeColor
            {
                get;
                set;
            }
    
            public override void WearGlasses()
            {
                throw new NotImplementedException();
            }
        }
    
    
        public class Toddler : HumanBase,IHuman
        {
    
            public Toddler(int age, double height, string hairColor, string eyeColor)
                : base(age, height, hairColor)
            {
                //add normal Toddler stuff only
                this.EyeColor = eyeColor;
            }
    
    
            public override string EyeColor
            {
                get;
                set;
            }
    
            public override void WearGlasses()
            {
                throw new NotImplementedException();
            }
        }
    
    
        public class Program
        {
    
            public static void Main()
            {
                IFactory humanFactory = new HumanFactory();
    
                Baby clientBaby = humanFactory.CreateBaby(1, 20, "brown","blue") as Baby;
                Toddler clientToddler = humanFactory.CreateToddler(3, 36, "blonde","blue") as Toddler;
    
                /* I can access the IHuman properties and methods even though 
                 * this is the concrete objects because both the concrete and 
                 * the abstract base implement the same interface.  But it is the
                 * abstract base that actually places it's implementation in
                 * one place
                 */
    
                int babyAge = clientBaby.Age;
                double toddlerHeight = clientToddler.Height;
    
                /*error here though because this is the abstract base class's property
                 * string hairColor = clientBaby.HairColor;
                 * The point here is only designate an abstract base class's property as 
                 * abstract if you want every concrete class to override it so that it
                 * can be used here.  But that means it will be a signature only abstract
                 * property or method.
                 */
    
                 /*I can, however, access the abstracted properties and methods
                  */
                string babyEyeColor = clientBaby.EyeColor;
                
                /*I can also treat them as objects of the interface type and 
                 * leverage their properties and methods
                 */
    
                List<IHuman> humanEntities = new List<IHuman>()
                {
                    clientBaby,
                    clientToddler
                };
    
                foreach (IHuman humanEntity in humanEntities)
                {
                    Console.WriteLine("This human is {0} years old and is {1} inches tall.", humanEntity.Age, humanEntity.Height);
                }
    
                Console.ReadLine();
            }
    
        }
    Attached Images Attached Images  

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Best way for creating a concrete class?

    exactly. thats great, thanks. good to know im on the same lines. and yup, I know the difference between interfaces and abstract classes.

    good example.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,401

    Re: Best way for creating a concrete class?

    Baby, Toddler, Child and Teenager don't need to implement IHuman if they inherit from HumanBase and HumanBase implements IHuman. What do you think you are gaining by doing so?

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Best way for creating a concrete class?

    yup. overlooked that one - correct. no need for implementing the interface if they inherit the base which implements the interface

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7
    Addicted Member
    Join Date
    Jan 2008
    Location
    Tennessee
    Posts
    130

    Re: Best way for creating a concrete class?

    You're right. I think I had something else in mind (what? I'm not sure now. I must've been playing around with some what-if's... but your right, this works without.) Sorry about that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.