So I have a design question, and I want some opinions.
I have a situation where I need to save up a list of commands that can be called later on, repeatedly.
What I'm going to do is create a List(Of Command) and place each command into it, then have a method that can be called to loop over each command and call it.
So I started by imagining it where 'Command' is an abstract type, and each Command inherits (or implements if I use an interface) this Command and does its unique command operation and defines its required parameters.
Public Class CommandSetCache Private _commands As New List(Of ICommand)() Public Sub DoA(ByVal arg1 As Blargh) _commands.Add(New CommandA(arg1)) End Sub Public Sub DoB(ByVal arg1 As Blargh, ByVal arg2 As Bleep) _commands.Add(New CommandB(arg1, arg2)) End Sub Public Sub DoCached() For Each cmnd In _commands cmnd.Invoke() Next End Sub Private Interface ICommand Sub Invoke() End Interface Private Class CommandA Implements ICommand Public Arg1 As Blargh ''imply constructor Public Sub Invoke() Implements ICommand.Invoke Arg1.Foo("doing a") End Sub End Class Private Class CommandB Implements ICommand Public Arg1 As Blargh Public Arg2 As Bleep ''imply constructor Public Sub Invoke() Implements ICommand.Invoke Arg1.Foo("doing b with " & Arg2) End Su End Class End Class
But then I considered, I could probably shorten up all this code just using lambda expressions. As follows:
Public Class CommandSetCache Private _commands As New List(Of Action)() Public Sub DoA(ByVal arg1 As Blargh) _commands.Add(Sub() arg1.Foo("doing a") End Sub) End Sub Public Sub DoB(ByVal arg1 As Blargh, ByVal arg2 As Bleep) _commands.Add(Sub() arg1.Foo("doing b with" & arg2) End Sub) End Sub Public Sub DoCached() For Each cmnd In _commands cmnd() Next End Sub End Class
My concern though is, there could be TONS of these commands spread across multiple 'CommandSetCache' objects. I know the memory implications of the class/interface example, but the lambda expression scenario... I don't know what the memory implications are. I don't know how they nicely they compile up.
Does anyone with extensive experience with lambdas tell me if the lambda example has any loops holes. I do understand the scoping implications, I have that covered. I just mean if excessive use of lambdas like this may cause for memory issues I don't know about.
Currently standing, I'm going forward with lambdas. Unless someone can give me some reason not to.
This post has been edited by lordofduct: 15 May 2013 - 08:04 AM