CSDN博客

img angelface

数据库与模式!

发表于2004/8/20 15:43:00  873人阅读

记得以前看过一个贴子关于数据库与面向对像的,所以
就写了这么一个小东西,(算是个演示程序吧, 也算是面向对像吧,嘿嘿),有问题的话请大家指正!    -------CLH
//抽像接口
unit User;

interface

type
  TUser = class
  protected
    FUserName: string;
    FPassword: string;
    FEmail: string;
    procedure SetUserName(const AName: string); virtual; abstract;
    function GetUserName: string; virtual; abstract;
    procedure SetPassword(const APass: string); virtual; abstract;
    function GetPassword: string; virtual; abstract;
    procedure SetEmail(const AEmail: string); virtual; abstract;
    function GetEmail: string; virtual; abstract;
  public
    property UserName: string read GetUserName write SetUserName;
    property Password: string read GetPassword write SetPassword;
    property Email: string read GetEmail write SetEmail;
  end;

  TUserBuilder = class
  private
    FUser: TUser;
  public
    procedure SetUserName(const AName: string); dynamic;
    procedure SetPassword(const APass: string); dynamic;
    procedure SetEmail(const AEmail: string); dynamic;
    constructor Create(User: TUser);
  end;

implementation

{ TUserBuilder }

constructor TUserBuilder.Create(User: TUser);
begin
  FUser := User;
end;

procedure TUserBuilder.SetEmail(const AEmail: string);
begin
  FUser.SetEmail(AEmail);
end;

procedure TUserBuilder.SetPassword(const APass: string);
begin
  FUser.SetPassword(APass);
end;

procedure TUserBuilder.SetUserName(const AName: string);
begin
  FUser.SetUserName(AName);
end;

end.

//真实的用户

unit GeneralUser;

interface

uses User, SysUtils, Windows, Controls, Classes, Variants;

type
  TGeneralUser = class(TUser)
  protected
    procedure SetUserName(const AName: string); override;
    function GetUserName: string; override;
    procedure SetPassword(const APass: string); override;
    function GetPassword: string; override;
    procedure SetEmail(const AEmail: string); override;
    function GetEmail: string; override;
  end;

implementation

{ TGeneralUser }

function TGeneralUser.GetEmail: string;
begin
  Result := FEmail;
end;

function TGeneralUser.GetPassword: string;
begin
  if FPassword = '' then Result := ' ' else
  Result := FPassword;
end;

function TGeneralUser.GetUserName: string;
begin
  Result := FUserName;
end;

procedure TGeneralUser.SetEmail(const AEmail: string);
begin
  if AEmail <> '' then FEmail := AEmail;
end;

procedure TGeneralUser.SetPassword(const APass: string);
begin
  if APass <> '' then FPassword := APass;
end;

procedure TGeneralUser.SetUserName(const AName: string);
begin
  if AName <> '' then FUserName := AName;
end;

end.

//用户控制接口
unit UserController;

interface

uses User;

type
  IUserController = interface
    function GetUser(UserName: string): TUser;
    function Insert(User: TUser): Boolean;
    function Update(User: TUser): Boolean;
    function Delete(UserName: string): Boolean;
  end;

implementation

end.

//代理用户控制
unit ProxyUserController;

interface

uses User, UserController, DbController;

type
  TProxyUserController = class(TInterfacedObject, IUserController)
  private
    FDbUserController: IUserController;
  public
    function GetUser(UserName: string): TUser;
    function Insert(User: TUser): Boolean;
    function Update(User: TUser): Boolean;
    function Delete(UserName: string): Boolean;
    function CheckExsit(userName: string): Boolean;
    constructor Create(connStr: string);
  end;

implementation

{ TProxyUserController }

function TProxyUserController.CheckExsit(userName: string): Boolean;
begin

end;

constructor TProxyUserController.Create(connStr: string);
begin
  FDbUserController := TDbController.Create(connStr);
end;

function TProxyUserController.Delete(UserName: string): Boolean;
begin
  FDbUserController.Delete(UserName);
end;

function TProxyUserController.GetUser(UserName: string): TUser;
begin
  Result := FDbUserController.GetUser(UserName);
end;

function TProxyUserController.Insert(User: TUser): Boolean;
begin
  if not CheckExsit(User.UserName) then Exit else
  FDbUserController.Insert(User);
end;

function TProxyUserController.Update(User: TUser): Boolean;
begin
  FDbUserController.Update(User);
end;

end.

//真实的用户控制类,主要是和数据库打交道
unit DbController;

interface

uses User, UserController, Resource, DB, ADODB, SysUtils;

type
  TDbController = class(TInterfacedObject, IUserController)
  private
    FConnection: TADOConnection;
    FQuery: TADOQuery;
  public
    function GetUser(UserName: string): TUser;
    function Insert(User: TUser): Boolean;
    function Update(User: TUser): Boolean;
    function Delete(UserName: string): Boolean;
    constructor Create(connStr: string); virtual;
    destructor Destroy; override;
  end;

implementation

{ TDbController }

constructor TDbController.Create(connStr: string);
begin
  FConnection := TADOConnection.Create(nil);
  FConnection.ConnectionString := connStr;
  if not FConnection.Connected then FConnection.Connected := true;
  FQuery := TADOQuery.Create(nil);
  FQuery.Connection := FConnection;
end;

function TDbController.Delete(UserName: string): Boolean;
begin

end;

destructor TDbController.Destroy;
begin
  FConnection.Close;
  FreeAndNil(FConnection);
  FreeAndNil(FQuery);
  inherited;
end;

function TDbController.GetUser(UserName: string): TUser;
begin

end;

function TDbController.Insert(User: TUser): Boolean;
begin
  with FQuery do
  begin
    Close;
    SQL.Clear;
    SQL.Add('INSERT INTO Account(userName, pass, email) VALUES(:userName, :pass, :email)');
    Parameters.ParamByName('userName').Value := User.UserName;
    Parameters.ParamByName('Pass').Value := User.Password;
    Parameters.ParamByName('Email').Value := User.Email;
    ExecSQL;
  end;
end;

function TDbController.Update(User: TUser): Boolean;
begin

end;

end.

//客户端
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, GeneralUser, User, UserController, DbController, ProxyUserController;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    FUser: TUser;
    FUserController: IUserController;
    FUserBuilder: TUserBuilder;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  DbPath: string;
begin
  DbPath := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + ExtractFilePath(Application.ExeName) + 'user.mdb' + ';Persist Security Info=false';
  FUser := TGeneralUser.Create;
  FUserBuilder := TUserBuilder.Create(FUser);
  FUserController := TProxyUserController.Create(DbPath);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FUserBuilder.SetUserName(edit1.Text);
  FUserBuilder.SetPassword(edit2.Text);
  FUserBuilder.SetEmail(edit3.Text);
  FUserController.Insert(FUser);
end;

end.

0 0

相关博文

我的热门文章

img
取 消
img