Association Classes and Link Management

If we have a design that incorporates an association class, then we have an alternative home for the link management that Developers may find is more cohesive.

Let's assume that association between Person and Car is supplemented with a 'Log Book' that records the Car's history:

We will now want to ensure that when a Person is associated to a Car, a LogBook is also associated. We will assume that the LogBook already exists. If we do this, we can end up with the following implementation.

(Note that some properties in the code have been omitted from the model to make it easier to read. In fact, we can see that the LogBook in the code actually has a number of {derived} attributes that get their information from the Car.)

Firstly, here is the LogBook association class, which also contains the link management code:

'LogBook Association Class 'My associations

Private m_theLoggedCar As Car Private m_theOwner As Person

Private m_myIssueDate As Date

'to check we have entered an 'initialized state' Private m_blnInitialized As Boolean

'When was I issued?

Public Property Get IssueDate() As Date

IssueDate = m_myIssueDate End Property

'When was I issued?

Public Property Get IssueDate() As Date

IssueDate = m_myIssueDate End Property

'set issue date

Public Property Let

IssueDate(ByVal aDate As Date)

m mylssueDate =

aDate

End Property

'you can only get the milage - you can't set it

'that would be illegal!

Public Property Get Milage() As Long

Debug.Assert m_blnInitialized = True

'you can only get the milage - you can't set it

'that would be illegal!

Public Property Get Milage() As Long

Debug.Assert m_blnInitialized = True

'derived property - get from the car Milage = m_theLoggedCar.Milage End Property

'who owns the car?

Public Property Get RegisteredOwner() As String Debug.Assert m_blnInitialized = True

'derived property

- get from the person

RegisteredOwner =

m theOwner.Name

End Property

'Register the log book for use

Public Sub Register(ByVal aPerson As Person, ByVal aCar As Car) 'delegate to link Link aPerson, aCar End Sub

'Unregister the log book from use Public Sub UnRegister()

'destroy the asociation 'just delegate to unlink UnLink End Sub

Private Sub Link(ByVal aPerson As Person, ByVal aCar As Car) Debug.Assert Not aPerson Is Nothing Debug.Assert Not aCar Is Nothing

'link up in both directions Set aPerson.Transport = aCar Set aCar.Owner = aPerson

'set my links too

Set m_theLoggedCar = aCar

Set m theOwner = aPerson

m blnInitialized =

= True

End Sub

'destroy the asociation

'deal

with person first

Set m_

theOwner.Transport =

Nothing

'person looses transport

Set m_

theOwner = Nothing

'log book looses person

'unlink car now

Set m theLoggedCar.

.Owner = Nothing

'car looses owner

Set m_theLoggedCar

= Nothing

'log book looses car

End Sub

Here is the Car source code now (some extra attributes have been added to spice things up):

'Car Class

'our private implementation

Private m_Make As String 'make of car

Private m_myRoadMilage As Long 'My Milage

'our hidden link implementation to person Private m_theOwner As Person

'get the car owner

Public Property Get Owner() As Person

Set Owner = m_theOwner End Property

'set the car owner

'note: this makes the owner changeable during 'the car's lifetime

Public Property Set Owner(ByVal theOwner As Person)

Set m_theOwner = theOwner End Property

'get the make of the car

Public Property Get Make() As String

Make = m_Make End Property

'set the make of the car

Public Property Let Make(ByVal newMake As String)

m_Make = newMake End Property

'get the car's milage so far Public Property Get Milage() As Long

Milage = m_myRoadMilage End Property

'Bump up the car milage

Public Sub BumpMilageBy(ByVal someMiles As Long) m_myRoadMilage = m_myRoadMilage + someMiles End Sub

When we now want to associate Person and Car, we first create a LogBook and then get the LogBook to associate all three objects together. In the example client code we first associate the objects and then iterate over the assocation and list the information:

Option Explicit

Sub Main()

'1 to 1 bidirectional association with association class

'1. create some objects to link Dim aPerson As New Person aPerson.Name = "Russell"

Dim aCar As New Car aCar.Make = "Ferrari" aCar.BumpMilageBy 50

Dim aLogBook As New LogBook aLogBook.IssueDate = Now

'2. Link everything up aLogBook.Register aPerson, aCar

'3. Print out the ownership details 'We can do this solely via the LogBook Debug.Print "Car Registration Details:"

Debug.Print "Registration Date : " + CStr(aLogBook.IssueDate) Debug.Print "Owned By : " + aLogBook.RegisteredOwner Debug.Print "Milage on Clock : " + CStr(aLogBook.Milage)

'4. Let's give the car

to someone else

'(I must be mad!)

'let's take it out for

the weekend first!

aCar.BumpMilageBy 200

'a new owner gets lucky Dim aLuckyPerson As New Person aLuckyPerson.Name = "Dave"

aLogBook.Register aLuckyPerson, aCar

'5. Print out the new ownership details 'car should now be registered to Dave: Debug.Print "Car Registration Details:"

Debug.Print "Registration Date : " + CStr(aLogBook.IssueDate) Debug.Print "Owned By : " + aLogBook.RegisteredOwner Debug.Print "Milage on Clock : " + CStr(aLogBook.Milage)

'6. finally, write off the car

'Unregistering the log book is as good as writing off the car 'so let's do that instead aLogBook.UnRegister End Sub

Was this article helpful?

0 0
365 Days Of Motivation

365 Days Of Motivation

Stop Wasting Time And Learn How To Stay Motivated. Finally! Discover How To Stop Your Mind From Wandering, And Upgrade Your Motivation. You Can Hack Your Motivation Levels, Allowing You To Take Your Life To The Next Level.

Get My Free Ebook


Post a comment