Choosing the right view for an object type

I had this problem many times before, and I never had a solution in which I felt good.

Say I have a Transaction base class and two derived classes AdjustmentTransaction and IssueTransaction.

I have a list of transactions in the user interface, and each transaction is of a specific type of AdjustmentTransaction or IssueTransaction.

When I select a transaction and click on the "Change" button, I need to decide whether to show the AdjustmentTransactionEditorForm or IssueTransactionEditorForm parameter.

The question is, how can I do this in OO without using the switch statement in the type of the selected transaction? The switch statement works, but feels shredded. I feel that I have to somehow use the parallel inheritance hierarchy between transactions and TransactionEditors.

I could have the EditorForm property in my transaction, but this is a terrible mix of my Ai butter UI with my Model chocolate.

Thanks in advance.

+3
source share
4 answers

At some point, you need to map your Editorform to a transaction. You have several options:

  • The switch statement ... like you, I think it stinks and does not scale well.
  • "EditorForm" Transaction, , .
  • โ†’ . .

# Mapper Type โ†’ Form:

Dictionary <Type,Type> typeMapper = new Dictionary<Type,Type>();
typeMapper.Add(typeof(AdjustTransaction), typeof(AdjustTransactionForm));
// etc, in this example, I'm populating it by hand, 
// in real life, I'd use a key/value pair mapping config file, 
// and populate it at runtime.

:

Type formToGet;
if (typeMapper.TryGetValue(CurrentTransaction.GetType(), out formToGet))
{
    Form newForm = (Form)Activator.CreateInstance(formToGet);
}
+1

, - , .

- . -, :

Editing AdujustmentTransaction = AdjustmentTransactionEditorForm
Editing IssueTransaction = IssueTransactionEditorForm

, , - , , , .

(, , "Joe" "JoeIssueTransactionEditorForm", "" )

, , . , Spring .

+1

- ? , OO: Polymorph

Transaction.editWindow() (, , ), AdjustmentTransaction IssueTrasaction . element.editWindow() .

0

Dictionary/Config File

1), .

2) EXE UI , .

3) , , , , .

3) , .

4) Edit, Show , .

. . , switch

.

(),

, . , , . - . Command. , EXE, Command. , Command, Undo/Redo . ( Unececute, Execute)

0

All Articles