2009-07-03 8 views
0

最初の要件:数式ファクトリはどのように実装できますか?

  • 管理要件によって、私はオープンソースコードを使用できません。
  • 私は、ユーザーがプロジェクト用に独自の数式を定義する必要があります。
  • 私のユーザーはコード作成方法を知らない。
  • 数式は何とか保存し、後で読み込む必要があります。
  • 私は書かれた数式をデバッグする必要があるので、それらを見て理解できるようにする必要があります。
  • 使用される公式は非常に特殊化されたもので、特定のもの(数理計算式)です。
  • 新しいフォーミュラ(機能性)の追加は、迅速かつ維持可能な方法で行う必要があります。

私は何をしましたか?

私は抽象クラスでのすべての式が継承することをFormulaBase言わせて実装

:その後、私は建設する工場を作成し

Public Class SumFormula 
    Inherits FormulaBase 

    Public Shared ReadOnly Property XMLTag() As String 
     Get 
      Return "sum" 
     End Get 
    End Property 

    Private X As FormulaBase 
    Private Y As FormulaBase 

    Public Sub New(ByVal xmlText as Xml.XmlNode) 
     ' Code to obtain read the sum parameters form XML.' 
    End Sub 

    Public Overrides Function Formula(ByVal p as Parameters) as Result 
     Return X.Formula(p) + Y.Formula(p) 
    End Function 

    Public Override Function GetXml() as Text.StringBuilder 
     Return New Text.StringBuilder().Append("<sum>").Append(X.GetXml()).Append(Y.GetXml()).Append("</sum>") 
    End Function 
End Class 

Public MustInherit Class FormulaBase 
    Public MustInherit Function Formula(ByVal p as Parameters) as Result 
    Public MustInherit Function GetXML() as Text.StringBuilder 
End Class 

それから私はこのような式をラップするクラスを作成しました次のような数式:

Public NotInheritable Class FormulaFactory 
    Private Shared Formulas As Dictionary(Of String, Reflection.ConstructorInfo) = InitializeFormulas() 

    Private Shared Sub Add(ByVal collection As Dictionary(Of String, Reflection.ConstructorInfo), ByVal formula as Type) 
     ' Some code to extract the contructor and XmlTag from each class and add them to the dictionary.' 
    End Sub 

    Private Shared Function InitializeFormulas() As Dictionary(Of String, Reflection.ConstructorInfo) 
     Dim Collection As New Dictionary(Of String, Reflection.ConstructorInfo) 
     Add(Collection, GetType(SumFormula)) 
     Return Collection 
    End Sub 

    Public Shared Function ConstructFormula(xmlText as Xml.XmlNode) as FormulaBase 
     Return DirectCast(Formulas(xmlText).Invoke(New Object(){xmlText}), FormulaBase) 
    End Function 
End Class 

私はもう少し魔法を使うユーザーに式を表示してXMLを表示しないようにします。新しいフォーミュラを追加するには、ラッパークラスを作成し、Shared XMLTagプロパティと、パラメータとしてXMLNodeをとるコンストラクタを追加するだけです。次に、FormulaFactory.InitializeFormulasメソッドに行を追加します。

質問は、私が行ったかもしれない別の方法がありますか?

答えて

1

別のオプションは、PublicDomainの動的コード評価のようなものを使用していた可能性があります。

これにより、実行時に「スニペット」を読み込み、動的に解析することができます。この場合、GUIを介して数式を差し込んで保存し、ロードし、評価するのはかなり簡単です。

+0

私はそれをチェックしますが、私はオープンソースを使用できません。 : '( 要件に制限を追加しました – Wilhelm

+1

ライセンスを読んでいませんでした。その公開ドメイン、duh! – Wilhelm

関連する問題