"Tom Corcoran" <
XXXX@XXXXX.COM>a écrit dans le
message de news: 42e8f036$
XXXX@XXXXX.COM...
Quote
Any ideas on how to setup TOurEnum so it can cope with this?
I use this kind of enum for Aspects of the Observer pattern :
type
// base class
TAspect = class(TOurDataType)
private
fName: string;
protected
constructor Create(const Name: string); overload;
function CompareTo(const Other: TAspect): Boolean; virtual;
function GetName: string; virtual;
public
constructor Create; overload;
end;
// example Selection Aspect
TSelectionAspectEnum = (selEmpty, selSingle, selMultiple);
TSelectionAspect = class(TAspect)
public
class function Empty: TSelectionAspect;
class function Single: TSelectionAspect;
class function Multiple: TSelectionAspect;
private
fEnumValue: TSelectionAspectEnum;
constructor Create(EnumValue: TSelectionAspectEnum);
public
function EnumValue: TSelectionAspectEnum;
end;
implementation
constructor TAspect.Create;
begin
Create('');
end;
constructor TAspect.Create(const Name: string);
begin
inherited Create;
fName := Name;
end;
function TAspect.CompareTo(const Other: TAspect): Boolean;
begin
Result := Other = self;
if not Result then
Result := Other.GetName = fName;
end;
function TAspect.GetName: string;
begin
Result := fName;
end;
constructor TSelectionAspect.Create(EnumValue: TSelectionAspectEnum);
begin
inherited Create(GetEnumName(TypeInfo(TSelectionAspectEnum),
Ord(EnumValue)));
fEnumValue := EnumValue;
end;
function TSelectionAspect.EnumValue: TSelectionAspectEnum;
begin
Result := fEnumValue;
end;
class function TSelectionAspect.Empty: TSelectionAspect;
begin
Result := Create(selEmpty);
end;
class function TSelectionAspect.Single: TSelectionAspect;
begin
Result := Create(selSingle);
end;
class function TSelectionAspect.Multiple: TSelectionAspect;
begin
Result := Create(selMultiple);
end;
//////////////////////
OF course, you also have to consider that without interfaces, you would be
better off making the class functions return a Singleton instance to avoid
memory leaks, cleaning up in the finalisation section of the unit.
Joanna
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker