質問

オブジェクトのさまざまなプロパティを変更できるDelphi / C ++ Builderのダイアログを設計する必要がよくあり、それを使用するコードは通常次のようになります。

Dialog.Edit1.Text := MyObject.Username;
Dialog.Edit2.Text := MyObject.Password;
// ... many more of the same

if (Dialog.ShowModal = mrOk) 
begin
  MyObject.Username := Dialog.Edit1.Text;
  MyObject.Password := Dialog.Edit2.Text;
  // ... again, many more of the same
end;

また、xml / ini-files / whateverとの間でオブジェクトをマーシャリングするために、同様のコードが必要になることがよくあります。

この種の単純だが反復的なコードを避けるための一般的なイディオムやテクニックはありますか?

役に立ちましたか?

解決

まあ、完全に貴重だと思うのは、 GExperts プラグインウィザード" Reverse Statement"です。 Shift + Alt + Rを押してGExpertsをインストールした後に呼び出されます

これは、強調表示されたブロックの割り当てを自動的に切り替えます。例:

edit1.text := dbfield.asString;

なる

dbField.asString := edit1.text;

まさにあなたが探しているものではありませんが、多数の課題がある場合は時間を大幅に節約できます。

他のヒント

これは私のバリエーションです。同じ繰り返しコードにうんざりしてしまった私がしたことは、必要なXMLノード名に従ってすべての編集ボックスに名前を付けてから、コンポーネントを反復処理して値を出力することでした。 XMLコードは明白なはずで、編集とチェックボックスしかありませんが、アイデアを見ることができるはずです。

procedure TfrmFTPSetup.LoadFromXML(szFileName : string);
var
xComponent : TComponent;
nLoop : Integer;
xMainNode : TXmlNode;
xDocument : TNativeXml;
begin
inherited;

xDocument := TNativeXml.Create;
try
    xDocument.LoadFromFile(szFileName);
    xMainNode := xml_ChildNodeByName(xDocument.Root, 'options');
    for nLoop := 0 to ComponentCount - 1 do
    begin
        xComponent := Components[nLoop];
        if xComponent is TRzCustomEdit then
        begin
            (xComponent as TRzCustomEdit).Text := xMainNode.AttributeByName[xComponent.Name];
        end;
        if xComponent is TRzCheckBox then
        begin
            (xComponent as TRzCheckBox).Checked := xml_X2Boolean(xMainNode.AttributeByName[xComponent.Name], false);
        end;
    end;
finally
    FreeAndNil(xDocument);
end;
 end;

   procedure TfrmFTPSetup.SaveToXML(szFileName : string);
var
xComponent : TComponent;
nLoop : Integer;
xMainNode : TXmlNode;
xDocument : TNativeXml;
begin
inherited;

xDocument := TNativeXml.CreateName('ftpcontrol');
try
    xMainNode := xml_ChildNodeByNameCreate(xDocument.Root, 'options');
    for nLoop := 0 to ComponentCount - 1 do
    begin
        xComponent := Components[nLoop];
        if xComponent is TRzCustomEdit then
        begin
            xMainNode.AttributeByName[xComponent.Name] := (xComponent as TRzCustomEdit).Text;
        end;
        if xComponent is TRzCheckBox then
        begin
            xMainNode.AttributeByName[xComponent.Name] := xml_Boolean2X((xComponent as TRzCheckBox).Checked);
        end;
    end;

    xDocument.XmlFormat := xfReadable;
    xDocument.SaveToFile(szFileName);
finally
    FreeAndNil(xDocument);
end;
 end;

フォーム上の視覚コンポーネントのプロパティにアクセスすることは、良い習慣とは見なされません。別々のプロパティを持つ方が良いと考えられます。上記の例では、getおよびsetメソッドを持つユーザー名とパスワードのプロパティがあります。

例:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
  private
    function GetPassword: string;
    function GetUsername: string;
    procedure SetPassword(const Value: string);
    procedure SetUsername(const Value: string);
  public
    property Password: string read GetPassword write SetPassword;
    property Username: string read GetUsername write SetUsername;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.GetPassword: string;
begin
 Result := Edit2.Text;
end;

function TForm1.GetUsername: string;
begin
 Result := Edit1.Text;
end;

procedure TForm1.SetPassword(const Value: string);
begin
  Edit2.Text := Value;
end;

procedure TForm1.SetUsername(const Value: string);
begin
  Edit1.Text := Value;
end;

end.

これは、呼び出しコードに影響を与えることなく、フォーム上の視覚的なコンポーネントを変更できることを意味します。

別のオプションは、オブジェクトをプロパティとしてダイアログに渡すことです;

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TUserObject = class(TObject)
  private
   FPassword: string;
   FUsername: string;
  public
   property Password: string read FPassword write FPassword;
   property Username: string read FUsername write FUsername;
  end;

  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    btnOK: TButton;
    procedure btnOKClick(Sender: TObject);
  private
    FUserObject: TUserObject;
    procedure SetUserObject(const Value: Integer);
  public
    property UserObject: Integer read FUserObject write SetUserObject;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnOKClick(Sender: TObject);
begin
 FUserObject.Username := Edit1.Text;
 FUserObject.Password := Edit2.Text;
 ModalResult := mrOK;
end;

procedure TForm1.SetUserObject(const Value: Integer);
begin
 FUserObject := Value;
 Edit1.Text := FUserObject.Username;
 Edit2.Text := FUserObject.Password;
end;

end.

役立つこと。

Delphiには少なくとも「With」がありますが、問題を完全に解決するわけではありません。

if (Dialog.ShowModal = mrOk) 
begin
  with MyObject do
  begin
    Username := Dialog.Edit1.Text;
    Password := Dialog.Edit2.Text;
    // ... again, many more of the same
  end;
end;

そして、ビルダーAFAIKには何も似ていません。

データへのコントロールのバインドはDelphiではうまく機能しますが、残念なことに、そのデータがTDataSetの子孫に存在する場合のみです。データストレージにオブジェクトを使用するTDataSetの子孫を書くことができますが、そのようなものがすでに存在することがわかります。以下のリンクを参照してください...この実装は、単一のオブジェクトではなく、オブジェクトのコレクション(TCollectionまたはTObjectList)でのみ動作するようです。

http://www.torry.net/pages.php?id=563 -「Snap Object DataSet」のページを検索します

これに関する個人的な経験はありませんが、機能する場合、特にデータモジュールのプロパティなどの単一オブジェクトインスタンスでも機能する場合は非常に便利です...

" メディエーターパターン"を検索します。それはGoFのデザインパターンであり、彼らの本ではGoFは実際にあなたがここで説明しているものと多少似た状況でこのデザインパターンを動機付けています。カップリングという別の問題を解決することを目的としていますが、この問題もやはりあると思います。

要するに、アイデアは、すべてのダイアログウィジェットの間にある追加のオブジェクトであるダイアログメディエーターを作成することです。他のウィジェットを知っているウィジェットはありませんが、各ウィジェットはメディエーターを知っています。メディエーターはすべてのウィジェットを知っています。 1つのウィジェットが変更されると、メディエーターに通知されます。その後、メディエーターはこれについて関連するウィジェットに通知します。たとえば、[OK]をクリックすると、メディエーターはこのイベントについて他のウィジェットに通知する場合があります。

このようにして、各ウィジェットは自身に関連するイベントとアクションのみを処理します。メディエーターはすべてのウィジェット間の相互作用を処理するため、この「ボイラープレート」はすべてコードはすべてのウィジェットに分割され、"残基"これはすべてのウィジェットに対してグローバルであり、相互作用であり、メディエーターの責任です。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top