Are there some problems with Delphi with the "One class per file" rule?

Old name: How many classes per unit is desirable to have?

My question is specific to Delphi. I think that in Java and the C # world, a fairly common practice is usually one file for each class. I think this is the right rule to follow in Delphi, because in Delphi private members are not very private if you have more than one class in the unit.

So, I was surprised to hear from two different seniors (and probably more experienced than me), the programmers tell me that I am sharing my code too much. One of them said that I would not be shy to post 5-6 classes in a block.

Is there any problem with the “one class per module” rule that I don’t know about, which can guarantee and explain the reaction of these programmers?

+5
source share
3 answers

I do not know what you mean by a module.

Java requires one public class for each file, and the class name must match the file name. No ifs, ands or buts. This file may contain other private or private methods of the package, but only one public class.

If “module” means “package” for you, I would say that it has a total of more than one class in a package. There is no norm; look at the JDK itself to see it. There are many classes java.util, java.lang, java.sqland javax.swing.

, , " ". , - , .

- - , - . , .

+2

. . , .

, , , , . Borland DB.pas. DB: TDataSet, TDataSource, TField ..

, .. TForm Borland - dfm ..

, " FOO;" , FOO. , , .. , , .

Delphi namespaces. .

+2

One class per unit is too strict. Sometimes classes are interconnected, so they can be combined into one unit.

The backdoor problem for protected and private variables can be fixed using a strict directive:

type
  TClass1 = class 
  private
    FField1 : Integer;
  strict private
    FField2 : Integer;
  end;

  TClass2 = class 
  public
    procedure MessWithClass1;
  end;



implementation
  procedure TClass2.MessWithClass1;
  var
    c1 : TClass1;
  begin
    c1.FField1 := 1;
    c1.FField2 := 2;   // Fails!
  end;     
0
source

All Articles