设计初衷:我的许多窗体都有打印功能,在点击打印后在一个打印设置窗体上设置显示的数据的条件、选择报表文件等。窗体不同打印打印窗体的条件设置控件也不一样,如果为每个窗体都建一个相应的打印窗体很明显特别繁琐,于是就想给打印窗体留一些接口用于调用窗体创建控件,但如果用具体的控件我以后想更改控件的时候必定要重新更改每个调用窗体上的代码
于是又想用接口实现要调用的控件,这样只在接口内部指定创建何种控件,以后想更改控件就变的非常方便,于是就有了下面的初步模型,只是实现创建一个ComboBox,以后还准备在这个单元中实出现TextBox等控件。各位看看这样设计合不合理,哪里可以改进,欢迎大家指点这是接口单元
unit uInterface;interface
uses Forms,stdCtrls,Classes,StrUtils,SysUtils,Controls,Dialogs;
type IComboBox=interface
  procedure CreateCombo(iLeft, iTop, iWidth, iHeight:integer;aParent:TForm);
  function GetItems:TStrings;
  procedure SetItems(aItems:TStrings);
  function GetText:TCaption;
  procedure SetText(aText:TCaption);
  property Items:TStrings Read GetItems Write SetItems;
  property Text:TCaption Read GetText Write SetText;end;type TIComboBox=class(Tinterfacedobject,IComboBox)
  private
    FComboBox:TComboBox;
    function GetItems:TStrings;
    function GetText:TCaption;
    procedure SetItems(aItems:TStrings);
    procedure SetText(aText:TCaption);
  public
    procedure CreateCombo(iLeft, iTop, iWidth, iHeight:integer;aParent:TForm);
    property Items:TStrings Read GetItems Write SetItems;
    property Text:TCaption Read GetText Write SetText;
    destructor Destroy;override;
end;
implementation{ TIComboBox }procedure TIComboBox.CreateCombo(iLeft, iTop, iWidth, iHeight: integer;
  aParent: TForm);
begin
FComboBox:=TComboBox.Create(aParent);
FComboBox.SetBounds(iLeft,iTop,iWidth,iHeight);
FComboBox.Parent:=aParent;end;destructor TIComboBox.Destroy;
begin
  //showmessage('aa');
  //if assigned(FComboBox.Items) then FComboBox.free;  inherited;
end;function TIComboBox.GetItems: TStrings;
begin
  Result:=FComboBox.Items;
end;function TIComboBox.GetText: TCaption;
begin
  Result:=FComboBox.Text;
end;procedure TIComboBox.SetItems(aItems: TStrings);
begin
  FComboBox.Items:=aItems;
end;procedure TIComboBox.SetText(aText: TCaption);
begin
  FComboBox.Text:=aText
end;end.这是调用:
unit uMain;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,uInterface, StdCtrls, Buttons;type
  TfrmMain = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  frmMain: TfrmMain;implementation{$R *.dfm}procedure TfrmMain.BitBtn1Click(Sender: TObject);
var cboName:IComboBox;
begin
  cboName:=TIComboBox.Create;
  cboName.CreateCombo(10,10,150,20,frmMain);
  cboName.Items.Add('one');
  cboName.Items.Add('two');
end;end.