unit UMVCPattern;
interface
uses
Contnrs,classes;
type
TDataUser =
class;
TDataContainer =
class(TThread)
protected
FDataUsers : TObjectList;
public
constructor Create;
destructor Destroy;
override;
procedure attachDataUser(dataUser: TDataUser);
procedure detachDataUser(dataUser: TDataUser);
procedure updateDataUsers;
end;
TDataUser =
class(TObject)
protected
FDataContainer : TDataContainer;
public
procedure attachTo(dataContainer: TDataContainer);
procedure detachFrom(dataContainer: TDataContainer);
procedure update;
virtual;
abstract;
end;
implementation
constructor TDataContainer.Create;
begin
inherited create(false);
FDataUsers := TObjectList.create(false);
end;
destructor TDataContainer.Destroy;
begin
FDataUsers.Free;
inherited destroy;
end;
procedure TDataContainer.attachDataUser(dataUser: TDataUser);
begin
if FDataUsers.IndexOf(dataUser) = -1
then
FDataUsers.Add(dataUser);
end;
procedure TDataContainer.detachDataUser(dataUser: TDataUser);
begin
FDataUsers.Remove(dataUser);
end;
procedure TDataContainer.updateDataUsers;
var
i: Byte;
begin
for i:=0
to FDataUsers.Count -1
do
begin
(FDataUsers.Items[i]
as TDataUser).update;
end;
end;
procedure TDataUser.attachTo(dataContainer: TDataContainer);
begin
FDataContainer := dataContainer;
FDataContainer.attachDataUser(self);
end;
procedure TDataUser.detachFrom(dataContainer: TDataContainer);
begin
FDataContainer := dataContainer;
FDataContainer.detachDataUser(self);
FDataContainer :=
nil;
end;
end.