Which is more correct? C# implementation

Started by
1 comment, last by Telastyn 15 years, 9 months ago
I'm just looking for some feedback. Alternatives would also be interesting! I have a base class with a Size and Location. I also have a class that inherits from that base, but it doesn't care about the Size and Location being logged to Console. Of the two options below, which is more correct way to prevent the Console.WriteLine? Size and Location var's must remain on the base class, and the Console.WriteLine must happen for all classes except SomeFoo if you choose to write a 3rd option=) .NET 2.0 1:

    public class FooBase
    {
        Size mySize = new Size();
        Point myLocation = new Point();

        
        protected virtual void CallMeSomeTimeAfterMyClassIsConstructed()
        {   
            if (myLocation.IsEmpty && mySize.IsEmpty)
            {
                Console.WriteLine("Size and Location are empty.");
            }
        }
    }

    public class SomeFoo : FooBase
    {
        protected override void CallMeSomeTimeAfterMyClassIsConstructed()
        {
            
        }
    }






2:

    public class FooBase
    {
        protected bool hasSizeAndLocation = true;
        Size mySize = new Size();
        Point myLocation = new Point();

        private void CallMeSomeTimeAfterMyClassIsConstructed()
        {
            if (hasSizeAndLocation && myLocation.IsEmpty && mySize.IsEmpty)
            {
                Console.WriteLine("Size and Location are empty.");
            }
        }
    }

    public class SomeFoo : FooBase
    {
        public SomeFoo()
        {
            this.hasSizeAndLocation = false;
        }

    }







-BourkeIV
Advertisement
The second. Since the derived class doesn't need to know of the logging console, you can just keep that function as a private method of the base class.

Beginner in Game Development?  Read here. And read here.

 

Neither. If the subtype doesn't care about the base member variables (a direct departure from the base class' purpose), it shouldn't be a subtype. Without more info, I'd be inclined to think that they might be better as siblings of a common base class which actually is the common behavior.

This topic is closed to new replies.

Advertisement