How to separate interface code and function code (based on DelphiVCL)

zhaozj2021-02-08  263

East day documentation

Many friends have seen the last time I wrote "Create a well-designed code (Based on Delphi / VCL)", I feel that I can accept the point of view, but it seems to be too simple, not specific; some friends are A small example of some objections. So there is this article.

Last time, I will give this example: Supplementation to get a string list from somewhere, then displayed in TListBox, the code I am: Objectxxx: = TobjectXxxx.create; listbox1.items: = ObjectXX.GetStringList Objectxxx.free; indeed, I admit that I have a simple from these three lines of code. It seems that there is a "abuse of the object". Perhaps the example is too simple, the feeling of people is TOBJECTXXX only getStringList this Public member function. If this is the case, that is really "abuse". The class is an abstraction of the object, and the object is a collection of state and operation (that is, data and operation of the data). Therefore, there is no state of the object is not an object! The design of a class without private data is a failure design (that is not class, but an interface).

Ok, let's take a detailed example to explain how to separate the interface code and function code. Suppose I have to be a simple personal address book management software, it is clear that the entire software is divided into two parts: a part is an iconic user, that is, the so-called interface part, I can provide four buttons ("Add", " Delete, "Modify", "Find") and an editing box (display contact information and accept user input) is used to interact with the user; the other part is functional, that is, the internal access to the address book. . So, with a TaddrBook class, it is an abstraction of the functionalized part. TAddrBook = class private // Some private members public constructor Create; destructor Destroy; override; GetCount: Integer; FindRecord (strString): Integer; GetRecord (nIndex: Integer): String; SetRecord (nIndex: integer; strRec: String): Boolean AddRecord (strrec: string): Boolean; DelRecord (NINDEX): Boolean; // Other common member function END; private member cannot be determined, mainly depends on this class's implementation. As such, the logic package of the access to the address book can be used. The code of the interface portion does not involve these access logic. The interface part of the interface is as follows: Var Form1: TFORM1; AddRBook: Taddrbook; ncurrec: integer;

Implementation procedure tform1.formcreate (sender: TOBJECT); begin addrbook: = taddrbook.create; ncurrec: = addrbook.getcount;

转载请注明原文地址:https://www.9cbs.com/read-2054.html

New Post(0)