VB OOD

How to design my classes - musing...

Page 1 of 1

6 Replies - 2644 Views - Last Post: 03 October 2008 - 09:05 AM Rate Topic: -----

#1 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

VB OOD

Post icon  Posted 03 October 2008 - 02:20 AM

No code, just wondering in theory...

Suppose I'm to build a phone book.
In this book I want to have customers who are "contacts".
Each contact may have several adresses - "address".
Each contact may consist of several persons - "person".
Each person may have several phone numbers - "phoneNumber"

So, I'd think I should make one class "contact", one class "adress", one class "person" and one class "phoneNumber"

The person class would consist of name, birthdate and so on but also of a collection of phoneNumber.

The contact class would then be made from a few data fields and a collection of address and another collection of person.

Is this the way to do it?

The question I think I'm asking is if there are any problems with classes within classes within classes AND if that approach has any serious drawbacks.

Thanks
Jens

Is This A Good Question/Topic? 0
  • +

Replies To: VB OOD

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: VB OOD

Posted 03 October 2008 - 03:25 AM

There can be many ways to do that. I would suggest you to...

PhoneNumber class, containing member pointing to the person who owns it.
Person class containing member pointing to address where he lives.
Address class containing member pointing to the contact it is connected to.

This will look like opposite of what you said but it will make your life easier.

If you keep collections in classes then it will put limit on having say phone numbers or addresses which is not allowed by your assignment.
plus, this will be wastage of memory as if you store phoneNumber in person still you will need to store person in phoneNumber as well, causing redundancy.

But in given solution here you will be avoiding all these complexities. and if you keep your objects collection sorted all the time, it will become easy and fast to search for the information.

Say if you have id of person and you need all his phoneNumbers. and If your collection of PhoneNumber objects is sorted on id of person, you will be able to search it very fast.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#3 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: VB OOD

Posted 03 October 2008 - 05:40 AM

View PostAmitTheInfinity, on 3 Oct, 2008 - 05:25 AM, said:

If you keep collections in classes then it will put limit on having say phone numbers or addresses which is not allowed by your assignment.
plus, this will be wastage of memory as if you store phoneNumber in person still you will need to store person in phoneNumber as well, causing redundancy.


The question I have here is should there ever be a phone number not related to a person? If he is storing this data in a relational database I would forsee him building the same relationships at the database level. A phone number without a name seems kinda silly.

View PostAmitTheInfinity, on 3 Oct, 2008 - 05:25 AM, said:

But in given solution here you will be avoiding all these complexities. and if you keep your objects collection sorted all the time, it will become easy and fast to search for the information.

Say if you have id of person and you need all his phoneNumbers. and If your collection of PhoneNumber objects is sorted on id of person, you will be able to search it very fast.


Would it not be faster to loop through a collection of just the 1 or more phone numbers related to the person?

For Each selPhoneNumber in Contact.Persons(selectedPersonID).PhoneNumbers

The other advantage of this design is if you have 1million phone numbers you don't have to retrieve them all upfront. You only have to retrieve the ones related to the contact as each contact is selected. And if you have a specific requirement to search by phone number you can call a proc to return you the contactid, personid, phonenumberid of the phonenumber.


It takes alot more effort to design your classes this way, but hey I spent the last 8 years developing my own framework based on such concepts. I just wanted to try and show that there are advantages to the OPs design as well.
Was This Post Helpful? 0
  • +
  • -

#4 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: VB OOD

Posted 03 October 2008 - 07:34 AM

Since person works at contact many person can share a common phoneNumber, i.e. a many-to-many relationship.

About storing stuff in a DBMS I think I'd use a three table design to store person - phoneNumber.
Like...
persID
FName
LName
BDate

persID
phoneID

phoneID
phoneNumber


But how do one handle this in the OO world? E.g. in VB.NET? I am probably asking more about principles than about how to code it,

Thanks for your replys.
Jens

BTW: Did I use "i.e." and "e.g." correctly above?
Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: VB OOD

Posted 03 October 2008 - 08:03 AM

There can always be multiple designs for one problem and every design will be better than other by some way. :)

Quote

Suppose I'm to build a phone book.
In this book I want to have customers who are "contacts".
Each contact may have several adresses - "address".
Each contact may consist of several persons - "person".
Each person may have several phone numbers - "phoneNumber"


Based on given explanation, I thought the phoeNumber is related with only one Person at a time.

magicmonkey

Quote

The question I have here is should there ever be a phone number not related to a person? If he is storing this data in a relational database I would forsee him building the same relationships at the database level. A phone number without a name seems kinda silly.


Well, I never said that phoneNumber is not related to person. See the second line of my earlier post

Quote

PhoneNumber class, containing member pointing to the person who owns it.

I am saying that as Person and PhoneNumber has 1-Many relationship so we should have person's id in PhoneNumber object.
and yes phone Number without name IS silly.

Quote

Would it not be faster to loop through a collection of just the 1 or more phone numbers related to the person?

Yes it would be bit faster. But think about memory waste plus data consistency as well. what if someone update a contact number? you have to update at 2 places every time. and if we don't have any other member variable in phoneNumber class then why we need it [if we already have them as a collection in person]?

As I said, it is possible to have multiple designs for single problem so I won't say that there are issues in OP's design, I just tried to give him another viewpoint. :)

This post has been edited by AmitTheInfinity: 03 October 2008 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#6 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: VB OOD

Posted 03 October 2008 - 08:14 AM

We could prob debate for hours about how to best store contact information, but a many-to-many relationship for phone numbers seems overkill to me personally. You would probably want to identify the type of phone number as well, Cell, Home, Work.

Hopefully this can get you started with some basic concepts.

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim MCM As New MyContactManager
        Dim C As Contact = MCM.AddContact("My Company")
        Dim P As Person = C.AddPerson("John", "Doe")
        Dim A As Address = P.AddAddress("My Street")
        Dim PN As PhoneNumber = P.AddPhoneNumber("Work", "5555555")
    End Sub
End Class

Public Class MyContactManager
    Private _Contacts As Contacts
    Public ReadOnly Property Contacts() As Contacts
        Get
            Return _Contacts
        End Get
    End Property

    Public Function AddContact(ByVal CompanyName As String) As Contact
        Dim newContact As New Contact(CompanyName)
        Contacts.Add(newContact)
        Return newContact
    End Function

    Public Sub New()
        _Contacts = New Contacts
    End Sub
End Class

Public Class Contacts
    Inherits List(Of Contact)
End Class

Public Class Contact
    Private _Persons As Persons
    Public ReadOnly Property Persons() As Persons
        Get
            Return _Persons
        End Get
    End Property

    Private _CompanyName As String
    Public Property CompanyName() As String
        Get
            Return _CompanyName
        End Get
        Set(ByVal value As String)
            _CompanyName = value
        End Set
    End Property

    Public Function AddPerson(ByVal FirstName As String, ByVal LastName As String) As Person
        Dim newPerson As New Person(FirstName, LastName)
        Persons.Add(newPerson)
        Return newPerson
    End Function

    Public Sub New()
        _Persons = New Persons
    End Sub

    Public Sub New(ByVal CompanyName As String)
        Me.New()
        _CompanyName = CompanyName
    End Sub
End Class

Public Class Persons
    Inherits List(Of Person)
End Class

Public Class Person
    Private _Addresses As Addresses
    Public ReadOnly Property Addresses() As Addresses
        Get
            Return _Addresses
        End Get
    End Property

    Private _PhoneNumbers As PhoneNumbers
    Public ReadOnly Property PhoneNumbers() As PhoneNumbers
        Get
            Return _PhoneNumbers
        End Get
    End Property

    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property

    Private _LastName As String
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Public Function AddAddress(ByVal StreetName As String) As Address
        Dim newAddress As New Address(StreetName)
        Addresses.Add(newAddress)
        Return newAddress
    End Function

    Public Function AddPhoneNumber(ByVal NumberType As String, ByVal Number As String) As PhoneNumber
        Dim newPhoneNumber As New PhoneNumber(NumberType, Number)
        PhoneNumbers.Add(newPhoneNumber)
        Return newPhoneNumber
    End Function

    Public Sub New()
        _Addresses = New Addresses
        _PhoneNumbers = New PhoneNumbers
    End Sub

    Public Sub New(ByVal FirstName As String, ByVal LastName As String)
        Me.New()
        _FirstName = FirstName
        _LastName = LastName
    End Sub
End Class

Public Class Addresses
    Inherits List(Of Address)
End Class

Public Class Address
    Private _StreetName As String
    Public Property StreetName() As String
        Get
            Return _StreetName
        End Get
        Set(ByVal value As String)
            _StreetName = value
        End Set
    End Property

    Public Sub New()
    End Sub

    Public Sub New(ByVal StreetName As String)
        Me.New()
        _StreetName = StreetName
    End Sub
End Class

Public Class PhoneNumbers
    Inherits List(Of PhoneNumber)
End Class

Public Class PhoneNumber
    Private _NumberType As String
    Public Property NumberType() As String
        Get
            Return _NumberType
        End Get
        Set(ByVal value As String)
            _NumberType = value
        End Set
    End Property

    Private _Number As String
    Public Property Number() As String
        Get
            Return _Number
        End Get
        Set(ByVal value As String)
            _Number = value
        End Set
    End Property

    Public Sub New()
    End Sub

    Public Sub New(ByVal NumberType As String, ByVal Number As String)
        Me.New()
        _NumberType = NumberType
        _Number = Number
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#7 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: VB OOD

Posted 03 October 2008 - 09:05 AM

View PostAmitTheInfinity, on 3 Oct, 2008 - 10:03 AM, said:

Yes it would be bit faster. But think about memory waste plus data consistency as well. what if someone update a contact number? you have to update at 2 places every time. and if we don't have any other member variable in phoneNumber class then why we need it [if we already have them as a collection in person]?

As I said, it is possible to have multiple designs for single problem so I won't say that there are issues in OP's design, I just tried to give him another viewpoint. :)


I just don't understand the reason for placing all the phone numbers in their own collection. Or maybe that was not what you were saying? And I am not sure why you would have to update a phone number in 2 places or why it would take up more memory to place the phone numbers of a person under a collection within person. If anything it would result in a smaller memory foot print and faster application respone by only loading the details of a contact as they were selected witihin the application. Again I think we both don't understand what each other is suggesting.

As for the many-to-many relationship mentioned by the OP, I would not personally normalize a phone number down to such levels. I could care less if i had the same phone number for Bill and Sandy, how often is this going to happen? 99% of all businesses have extenstions assosiated with the phone number making them unique. In a table of 1 million contact phone numbers maybe 1000 would be duplicated? And what if Bill and Sandy did have the same number then you get an email from Bill that he has a new number, you update Bills number and 1 week later you go to call Sandy and Bill answers. It is easy to over normalize data sometimes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1