Ver Mensaje Individual
  #12  
Antiguo 29-06-2005
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Reputación: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
Los puristas dicen que las interfaces no se hicieron para implementar un recolector de basura. Pero como a mi esto me tiene sin cuidado se puede incluso ir más lejos. En lugar del QueryHolder se hace una interfaz que "publique" los métodos y propiedades principales del objeto Query:


Código Delphi [-]
type
  IQuery = interface
    function ParamByName(const Value: String): TParam;
    function FieldByName(const FieldName: string): TField;

    procedure Open;
    procedure ExecSQL;
  end;

  TQueryObject = class(TInterfacedObject, IQuery)
  private
    FQuery: TQuery;

  public
    constructor Create;
    destructor Destroy; override;

    { IQuery }
    (*
      La clase implementa estas funciones simplemente delegándolas
      a su objeto privado FQuery. Por ejemplo:

      ParamByName --> Result := FQuery.ParamByName(param);
    *)
    function ParamByName(const Value: String): TParam;
    function FieldByName(const FieldName: string): TField;

    procedure Open;
    procedure ExecSQL;
  end;

implementation

constructor TQueryObject.Create;
begin
  FQuery := TQuery.Create;
end;

destructor TQueryObject.Destroy;
begin
  FQuery.Free;
  inherited;
end;

function UnQuery(parámetros): IQuery;
begin
  Result := TQueryObject.Create;
  Result.ParamByName(param).AsXXX := param;
end;

end.

Y se usaría simplemente así:

Código Delphi [-]
var
  Query: IQuery;

begin
  Query := UnQuery(parámetros);
  Query.Open;
end;

De esta manera, el código que use lo devuelto por funciones como UnQuery se ve igual que si se hubiera usado un TQuery directamente.

// Saludos
Responder Con Cita