We help IT Professionals succeed at work.

Advice

I have the following class which loads a set of tasks, the problem is I now need a LoadScheduled Tasks, is it recommended that I provide an optional parameter to GetInstance so that instead of calling Load it calls Load Tasks, the tasks cannot be filtered so I have to go this way they call a completely different area

 Private Shared _taskMgr As TaskMgr

    Public Shared Function GetInstance() As TaskMgr
        If _taskMgr Is Nothing Then
            _taskMgr = New TaskMgr
        End If
        Return _taskMgr
    End Function

    Protected Sub New()
        Load()
    End Sub
    Public Sub Add(ByVal task As Task)
        Dictionary.Add(task.TaskID, task)
    End Sub

    Public Function Item(ByVal Key As Object) As Task
        Return CType(Dictionary.Item(Key), Task)
    End Function

    Public Sub Remove(ByVal Key As Object)
        dictionary.Remove(Key)
    End Sub

    Private Sub Load()
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:
>is it recommended that I provide an optional parameter to GetInstance so that instead of calling Load it calls Load Tasks

GetInstance is not calling Load, your constructor is calling Load. And you can have multiple constructors with different parameters.

Author

Commented:
Get instance does call load, as it create a new instance which in turn calls load. I refer in code to get instance to save creating a new object each time
GetInstance is implementing what is known as a Singleton pattern.  That is, you will only ever have a SINGLE instance of TaskMgr.  If, when GwetInstance is call, there is no instance of TaskMgr already created, a new one will be created.  However, when GetInstance is called and a instance of TaskMgr already exists, having been created previously, then that instance will be return, and in that case, Load is NOT called.

AW

Author

Commented:
I know it is a singleton pattern, that is what I intended, my issues is whether it is acceptable to initiate GetInstance with an optional parameter which loads a different set of tasks
Most Valuable Expert 2012
Top Expert 2014
Commented:
I don't see any problem. But I think you should overload the GetInstance instead of using optional parameter.

Author

Commented:
Yeah I think you are right