Adam Beck
Adam Beck

Reputation: 1271

Is this a correct/good coding practice?

I'm struggling a bit to understand the power behind OO programming. Granted I am only slightly experienced in coding in general, I was hoping this would come a lot easier than it has. For this example I created some basic code in order to determine if this is a correct/good way of containing objects within objects. If not, would you please guide me in the correct direction.

I have 2 classes: a Boy and a Dog Class. The Boy Class contains a Dog object. The Dog object knows who its owner is.

Here is a Boy class:

Public Class Boy

Protected mName As String
Public Property Name() As String
    Get
        Return mName
    End Get
    Set(ByVal value As String)
        mName = value
    End Set
End Property

Protected mAge As Integer
Public Property Age() As Integer
    Get
        Return mAge
    End Get
    Set(ByVal value As Integer)
        mAge = value
    End Set
End Property

Protected mReturnHome As New TimeSpan(3, 15, 0)
Public Property ReturnHome() As TimeSpan
    Get
        Return mReturnHome
    End Get
    Set(ByVal value As TimeSpan)
        mReturnHome = value
    End Set
End Property

Protected mPet As New Dog(Me)
Public Property Pet() As Dog
    Get
        Return mPet
    End Get
    Set(ByVal value As Dog)
        mPet = value
    End Set
End Property
End Class

And here is a Dog class:

Public Class Dog

Private _owner As Boy
Public Sub New(ByRef Owner As Boy)
    _owner = Owner
End Sub

Protected mName As String
Public Property Name() As String
    Get
        Return mName
    End Get
    Set(ByVal value As String)
        mName = value
    End Set
End Property

Protected mBreed As String
Public Property Breed() As String
    Get
        Return mBreed
    End Get
    Set(ByVal value As String)
        mBreed = value
    End Set
End Property

Protected mCanPlay As Boolean
Public Sub PlayBall()
    If Now.TimeOfDay >= _owner.ReturnHome Then
        mCanPlay = True
    Else
        mCanPlay = False
    End If
End Sub
End Class

I need to be able to gain access to the Boy Class from the Dog Class because the Dog needs to be able to recognize properties specific to its owner (Boy).

Thank you.

Upvotes: 0

Views: 176

Answers (2)

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

First off, make your variables Private, not Protected. There is no need whatsoever for derived classes to access them directly.

Secondly, although this is done a lot in .NET, consider not having so many setters. Most properties shouldn’t change in an object’s lifetime. The exception are DTOs – objects which represent database entities.

Also take care only to model those aspects of an object that you actually use. In real software, most attributes of a given entity are irrelevant (e.g. hair colour of the customers in a library management software) and only a few are really needed by the software. Only model those.

Thirdly, if your Dog class needs to access specific functionality from the Boy class, the easiest recourse is to make this specific functionality Public.

Finally, don’t pass the dog’s owner via ByRef to the constructor. This works, but makes absolutely no sense. Use ByVal everywhere except where it really is required (and I argue that it’s never required, there are better solutions).

Upvotes: 1

The Evil Greebo
The Evil Greebo

Reputation: 7138

You're on the right track, although there are a few things that Boy and Dog have in common, such as Name and Age, so now you would look at those common attributes and methods and create a base class Animal from which both Boy and Dog would derive.

WRT how you tie the two together - consider that a boy could have multiple dogs, but a dog can have only one owner, so probably Boy.Dog should be Boy.Dogs (a collection) but Dog.Owner (as Boy) is absolutely fine.

Upvotes: 0

Related Questions