Using the Factory

The following code shows, how to work with the factory.

Employee e = new Employee();

e.FirstName = "John";

e.LastName = "Becker";

Travel t = e.NewTravel();

t.Purpose = "NDO Workshop";

ExpenseFactory exf = new ExpenseFactory();

Console.WriteLine("Suppose we have selected the first entry of the Expense type list in the UI: " + exf.Types[0]);

Expense ex = exf.NewExpense(exf.Types[0]);

Console.WriteLine("The new Expense has the type: " + ex.GetType().FullName);

In a console application the scenario seems a little bit quixotic, but in an application with a UI it makes perfect sense. And here comes the next challenge: Somehow the UI must show the right controls to enter the information for the concrete Expense type. Remember that for a Receipt object we have to enter the ReceiptText and the Sum, while for a PerDiemAllowance we have to enter the amount of time the traveller was on the road. We spent some efforts to keep the concrete Expense classes away from the creating context. We don't want to get them back again for the purpose of showing the right UI for the Expenses. But the solution is obvious: We need a factory, which is able to create Views in which we can manipulate the object data.