いくつかの子クラス (この場合は Windows フォーム クラス) を派生させたい基本クラスを作成しました。子インスタンスのコレクションを維持するために Factory パターンを使用しているため、フォームは主キー値ごとに 1 つのインスタンス (Factory パターンと Singleton パターンのマッシュアップのようなもの)。
基本フォーム クラスで次のコードを使用しています。
Public Class PKSingletonForm
Inherits Form
Protected _PKValue As Int32 = 0
Protected _strFormKey As String = ""
Protected Shared _dictForms As New Dictionary(Of String, PKSingletonForm)
Public Shared Function GetForm(Of T As {PKSingletonForm, New})(Optional ByVal PKValue As Int32 = 0) As T
'** Create the key string based on form type and PK.
Dim strFormKey As String = GetType(T).Name & "::" & PKValue.ToString
'** If a valid instance of the form with that key doesn't exist in the collection, then create it.
If (Not _dictForms.ContainsKey(strFormKey)) OrElse (_dictForms(strFormKey) Is Nothing) OrElse (_dictForms(strFormKey).IsDisposed) Then
_dictForms(strFormKey) = New T()
_dictForms(strFormKey)._PKValue = PKValue
_dictForms(strFormKey)._strFormKey = strFormKey
End If
Return DirectCast(_dictForms(strFormKey), T)
End Function
End Class
アイデアは、次のように、基本フォームから継承する子フォーム (たとえば、UserInfoForm と呼ばれる) を作成し、ユーザー #42 のインスタンスを作成することです。
Dim formCurrentUser = PKSingletonForm.GetForm(of UserInfoForm)(42)
これはすべて意図したとおりに機能します。
ただし、UserInfoForm には、設定したいプロパティがいくつかあります。フォームがファクトリによって作成された後ではなく、オブジェクト初期化子を使用して設定したいと思います。
Dim formCurrentUser As New UserInfoForm With { .ShowDeleteButton = False, .ShowRoleTabs = False }
これらの 2 つの方法を組み合わせる方法はありますか?そのため、ファクトリとイニシャライザがありますか?
私は探していません:
Dim formCurrentUser = PKSingletonForm.GetForm(of UserInfoForm)(42)
formCurrentUser.ShowDeleteButton = False
formCurrentUser.ShowRoleTabs = False
...基本クラスには、追加の基本フォーム パラメータを取得し、GetForm() 関数をラップしてフォームを表示する ShowForm() メソッドも含まれているためです。