Delphi problems caused by global variables

Recently wrote a Delphi program, think of a problem that needs to use in order to facilitate Delphi automatically creates all forms, and these forms variables are automatically declared as global variables. Thought for a moment, this will bring some problems:

1, the form is declared as a global variable can be used anywhere, but it also means, at any time be modified and call, the call relationship is complex and difficult to track.

2, does not meet the requirements as to the face. Due to the form of global variables, in each class method implementation, when it comes to the other form, the code is similar frmMain.XXX, frmAddInstance.XXX, using variables form a direct call. But this is unreasonable, a class that implements the method, the direct method is invoked on a specific object, rather than by creating a combination of continued way to implement the code, not in the spirit to face the object.

Such as: OnClick method TForm1's, often you see developers write:

procedure TForm1.OnClick(Sender:TObject);

begin

form1.Caption := 'abc';

form2.Visable := not form2.Visable;

end;

When using this direct form form1 this global variable in implementation of a class, although running without problems, but if TForm1 create a second instance of it, click on the second instance of the form, its title will not change, but instead changed the first instance. This code is certainly ridiculous. If remedy the situation, should from1 changed self. Here is a similar reference to form2, the code achieved cited specific examples of global rather than objects that are created or controlled by himself, it is unreasonable.

Delphi mechanism is difficult to change, temporarily each form of global variables can be considered as a single case of variables to use, that is, do not try to create a form of second instance.

3, Delphi data modules created, all of the above components are visible, although easy to operate, but it is easy to guide developers to go directly to the operating components. A more reasonable way is a method of encapsulating these operations disclosed modules, each specific business logic implemented method, by these methods other form (i.e., the generalized interface module) to the operation instead of directly operating components to achieve solution the purpose of decoupling.

Guess you like

Origin blog.51cto.com/bigdata1024/2427360