- Delphi Cookbook
- Daniele Spinetti Daniele Teti
- 292字
- 2025-04-04 16:22:47
Getting ready
What we want to do in this recipe is to dynamically create a class by looking for it by name among the classes that have been linked in the executable (or loaded from dynamic packages). The goal is to change the behavior of the program using only an external file, without relying on a lot of parameters and complex configuration code; just create the right class. Wonderful! Let's say you've developed a program to do orders. Your program allows only one-line orders, so you cannot buy different things in the same order (this is an example). The form is shown in the following:

There is a dataset field connected to each TDBEdit in the form. The TOTAL field is a calculated field, and its value is calculated in the OnCalcFields dataset.
The calculation is simple:
The customer is happy, and you are happy as well.
Now, a new, big customer, the City Mall, wants a customization—If the total is greater than $1,000, apply another 10% discount. Okay, you can create the customized version easily. So far so good, but now you have two different versions to maintain.
Now, another customer, the Country Road Shop, says, If there are more than 10 pieces, the discount must be at least 50%. Another customer, Spark Industries, specifies—Only at the weekend, all the calculated prices will be cut by 50%.
Argh! Four customers and four different versions of your software to maintain because of customization! You get the point in the beginning things are simple, but when you start to customize something, complexity (and bugs) can arise. Let's fix this problem in this recipe.