Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Why design patterns are easier in dynamic languages
Message
De
11/02/2008 11:38:18
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
 
 
À
11/02/2008 11:21:58
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
01291156
Message ID:
01291493
Vues:
15
>>># 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.

Not too bad :). I see that the class name is actually an integer, which I figure can be enumerated somewhere, and the class to instantiate needs to be in a .dll (or at least you need to know its name), which is just a minor nuisance, not a showstopper.

So OK it is feasible - maybe not as simple and elegant as it would be in Fox (the 50 lines I mentioned was "with factory.dbf and localfactory.dbf, with local overriding the main, plus error checking"), and maybe a bit limited, but it'd work.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform