Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why design patterns are easier in dynamic languages
Message
From
11/02/2008 11:21:58
 
 
To
10/02/2008 14:14:59
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01291156
Message ID:
01291473
Views:
13
>># of lines of code is, at best, a specious argument. A seasoned developer who has previously solved other problems may wind up leveraging previously-written code - the resulting # of lines might be longer...so what? That doesn't mean it's "less easy"
>
>OK, then let us describe it in terms of what needs to be done and how it gets done. To apply a factory pattern, so that it can be used for
>
>loObj=oFactory.create("mytoken")
>
>there needs to be a factory.dbf (or other table), and the code in the factory class needs to
>
>- get the table (make sure it is open etc),
>
>- find the record with "mytoken" in the name field
>
>- .newobject(classfield, classlibfield) with possible complication if parameters are to be passed to object's init
>
>- return the object, or return .null. if anything is screwed up.
>
>Now how do you do that in a static language?

I'm jumping in a little late, but here is how we did it (VB)
  Public Function GetCustomScrubberDetails(ByVal iImportDefaultSeqNo As Integer) As ICustomScrubs

        Dim objDALImport As New CommonParsingData
        Dim dsScrubberAssembly As New DataSet
        Dim hdlScrubber As System.Runtime.Remoting.ObjectHandle
        Dim ifCustomScrubs As ICustomScrubs

        Try
            Dim sLoc As String
            Dim sPath As String
            Dim sDll As String
            Dim sClass As String
            Dim asm As Reflection.Assembly

            dsScrubberAssembly = objDALImport.RtnCustomScrubberDetails(iImportDefaultSeqNo)
            If dsScrubberAssembly.Tables.Count > 1 Then
                If dsScrubberAssembly.Tables("CustomScrubbers").Rows.Count > 0 Then
                    Dim drAssembly As DataRow
                    drAssembly = dsScrubberAssembly.Tables("CustomScrubbers").Rows(0)
                    sDll = drAssembly("Scrubber_Assembly_Name").ToString.Trim()
                    sClass = drAssembly("Class_Name").ToString.ToUpper.Trim()

                    asm = Reflection.Assembly.GetExecutingAssembly()
                    sLoc = asm.Location

                    sPath = Path.Combine(Path.GetPathRoot(sLoc), Path.GetDirectoryName(sLoc))
                    sDll = Path.Combine(sPath, sDll)

                    ' Note: The type parameter (sClass) of Activator.CreateInstanceFrom is CASE SENSITIVE!!!
                    hdlScrubber = Activator.CreateInstanceFrom(sDll, sClass)
                    ifCustomScrubs = DirectCast(hdlScrubber.Unwrap(), ICustomScrubs)

                End If

            End If


            If objHewException.CheckForErrorTable(dsScrubberAssembly) = False Then

                dsScrubberAssembly.Tables.Add(objHewException.CreateErrorTable())

            End If

        Catch objEx As Exception

            If objHewException.CheckForErrorTable(dsScrubberAssembly) = True Then

                dsScrubberAssembly.Tables.Remove("ErrorTable")

            End If

            dtError = objHewException.AddHewException(objEx, System.Reflection.Assembly.GetExecutingAssembly)
            dsScrubberAssembly.Tables.Add(dtError)

        Finally

            GetCustomScrubberDetails = ifCustomScrubs
            dsScrubberAssembly = Nothing
            objDALClaimImport = Nothing

        End Try

    End Function
This was some of the first dotnet code I wrote (the guys who developed the system could not figure out how to implement the factory pattern). I would do things a little differently if I did it again, but it works.

The key lines are:
     hdlScrubber = Activator.CreateInstanceFrom(sDll, sClass)
     ifCustomScrubs = DirectCast(hdlScrubber.Unwrap(), ICustomScrubs)
Later on in code we only need to check if ifCustomScrubs is NOT NULL. Since it is an interface, we know what methods are implemented.

I love the Fox, and I am constantly frustrared with how little I know about dotnet and how slow I am developing in dotnet, but I have not yet encountered anything I could do in the Fox, that I could not do in dotnet.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform