During the development of TE2 and IWS, I finally realized the extraordinary happiness and huge pain brought to me in the way, on the one hand, I can put a few components on Form or DataModule, set up. Properties, then I can press F9 to run. On the other hand, I often trapped in an inexplicable AV error, I will crash my Delphi when I accidentally can't care. However, as long as we figure out some basic methods of creating components, you can be careful to avoid all kinds of traps during the development process. Before reading this article, I suggest you can read it first:
Delphi enthusiasts: Form Class to Component, this document tells us that the basic method of creating components CREATING Components Dynamically, this document tells us to use the correct way to use the correct way to use Dynamic Component Creation Gotcha (Don't do this), this document Tell us to use the components to use Reuse Through Inheritance and Composition, this document tells us how to design component 10 guidelines to help you design for reuse, this document also tells us some of the design components to operate
The purpose of this article is to hope that we can turn from the past RAD development method to component-based development methods, but this document does not tell you how to write components, and write components, because it can be written A book.
Why use components?
The topic of the hotter is now the topic of OO and OO-based Patterns, when I just started using Delphi, I often ask: I use OOP? Let's take a look at the standard way to develop by Delphi: Place several components to the Form or DataModule, write a few events, press F9 Run. Yes, RAD makes me a happy program, but it won't cause me to carefully design:
Starting phase code is not complicated, and we will copy some common code to all places of the program, and these code seem to be unrelated, the easiest example is: I am in an action EXECUTE event. Create a query, perform a SQL, in another action, I will create a query to perform another SQL. Is there any way to abstract Creating Query? Object Inspector is very easy to use, I can write event processing logic very easy, but this will tighten the logic and form or datamodule. Although the business logic is written in DataModule, it is recommended by Delphi, but its resequence is not very good. Think about the situation of dozens of datasets in a DATAMODULE, can you still say this DATAMODULE? ?
So, I recommend using the component-based programming, why, let us see the three advantages of writing the components in the Form Class to Component:
Delphi has a dynamic creation and destruction of components. Conversely, TOBJECT's derived class must be explicitly created, used, and destroy in the code. You can set properties when designing, don't look at this advantage, we can develop a property editor, allowing users to select legal attribute values. For visual components, you can set the location and size of the component when designing.
This is just an obvious advantage, it is just a table, hidden in these advantages, is: OOP. Delphi provides a component framework, so when you start trying to simplify programming by writing components, you will not know the OO's programming method. The most important thing is that the VCL framework uses many design patterns that make programs easy to reuse: Composite mode: When you place a variety of components on the Form, make up a new TForm's derived class, you have used the Composite mode Builder mode: When you create When you define the form, you will use the Builder mode, usually the object created by the Builder mode is composed of composite mode. Template Method mode: This mode is too common, any class from Tcomponent's derived class! Mediator Mode: When you write an event, you use the Mediator mode, pay attention to the shortcomings in Mediator mode is: it will make the intermediaries as a horm big thing! Singleton Mode: Although there is no mechanism to block us from creating multiple Tapplicaion objects, we know that any GUI program can only have a TAPPLCAITON object, that is, global variable Applicaion.
When you develop components, you have started using OOP and will use the five modes above. At least theoretically guarantees that your code can be reused, your program is easy to change to adapt to more requirements.
Mail to: me