(Continued from the first article)
Keeping object-oriented thinking throughout, this topic seems a bit big. What I just said and mentioned here are actually just some minor issues that should be paid attention to when coding. Therefore, it may be more appropriate to change the term 'throughout' to 'keep in mind'.
Some comments on some features of Delphi:
I wonder if you have noticed that all components (including controls) placed in a Delphi Form are visible to other Forms. To be precise, these components are the contents of the Public part of the Form. On the one hand, this result is good, because of its flexibility, other classes can easily reference these components on the Form, set their properties, execute their methods, events, etc.; but on the other hand, its flaws are also obvious. Yes, that leads to the loss of Form's encapsulation. In my opinion, these components placed on the Form, as far as the user's intention is concerned, should exist as private properties of the Form and should not be visible to other classes or other Forms. Even if you need to access them, you should access them indirectly through a series of property methods provided by Form.
Give an example to give everyone some perceptual understanding:
PRocedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Edit1.Text := 'abc'; // <-- I don't agree with the way this sentence is written.
end;
Many people may not have the concept of encapsulation in their minds when writing such code, but after reading this article, you will never do such a thing again (please change your ways!). In my opinion, TForm1 is TForm1, and TForm2 is TForm2. They all exist to achieve certain specific functions, so they provide some interfaces to the outside world (some attributes, methods, and events. Strictly speaking, events are also attributes. ) to achieve their promised functions. As for the specific implementation of these interfaces, they should be maintained by themselves. There is no need or way for the outside world to intervene. This idea corresponds to practical applications, that is, the question of whether Form2.Edit1 needs to be directly accessed by From1. I personally prefer the following implementation:
//The following is part of the content of TForm1 in Unit1
procedure TForm1.Button1Click(Sender: TObject);
begin
TForm2(FAnotherForm).EditText := 'abc'; // <-- This implementation embodies the idea of encapsulation
end;
//The following is the definition of TForm2 in Unit2
type
TForm2 = class(TForm)
Edit1: TEdit;
private
function GetEditText: string;
procedure SetEditText(const Value: string);
public
property EditText: string read GetEditText write SetEditText;
// <-- My recommended usage;
end;
…
function TForm2.GetEditText: string;
begin
result := Edit1.Text;
end;
procedure TForm2.SetEditText(const Value: string);
begin
if Value <> EditText then
Edit1.Text := Value;
end;
FAnotherForm here is a private property of TForm1, which is a pointer to an instance of TForm2 (this usage was emphasized in the first article). Accessing the EditText property of TForm2 instead of recklessly directly accessing Edit1.Text of TForm2 embodies an idea, that is, the idea of division of labor and collaboration, that is, the idea of independence, that is, the idea of encapsulation.
(Unfinished, to be continued)
More articles