unit Myc.Data.Types.Text; interface uses System.SysUtils, Myc.Data.Types; type TDataTextType = class(TInterfacedObject, IDataType, IDataTextType) strict private class var FSingleton: IDataTextType; class constructor CreateClass; public function GetName: String; function GetKind: TDataKind; function CreateValue(const AValue: string): IDataTextValue; class property Singleton: IDataTextType read FSingleton; end; implementation type TDataTextValue = class(TInterfacedObject, IDataValue, IDataTextValue) private FValue: string; function GetDataType: IDataType; function GetAsString: string; function GetValue: string; public constructor Create(const AValue: string); end; // Singleton implementation for the empty string value. TDataTextValueEmpty = class(TInterfacedObject, IDataValue, IDataTextValue) strict private class var FSingleton: IDataTextValue; class constructor CreateClass; private function GetDataType: IDataType; function GetAsString: string; function GetValue: string; public class property Singleton: IDataTextValue read FSingleton; end; { TDataTextType } class constructor TDataTextType.CreateClass; begin FSingleton := TDataTextType.Create; end; function TDataTextType.CreateValue(const AValue: string): IDataTextValue; begin // Use the singleton for empty strings. if (AValue = '') then Result := TDataTextValueEmpty.Singleton else Result := TDataTextValue.Create(AValue); end; function TDataTextType.GetName: String; begin Result := 'Text'; end; function TDataTextType.GetKind: TDataKind; begin Result := dkText; end; { TDataTextValue } constructor TDataTextValue.Create(const AValue: string); begin inherited Create; FValue := AValue; end; function TDataTextValue.GetDataType: IDataType; begin Result := TDataTextType.Singleton; end; function TDataTextValue.GetAsString: string; begin Result := FValue; end; function TDataTextValue.GetValue: string; begin Result := FValue; end; { TDataTextValueEmpty } class constructor TDataTextValueEmpty.CreateClass; begin FSingleton := TDataTextValueEmpty.Create; end; function TDataTextValueEmpty.GetAsString: string; begin Result := ''; end; function TDataTextValueEmpty.GetDataType: IDataType; begin Result := TDataTextType.Singleton; end; function TDataTextValueEmpty.GetValue: string; begin Result := ''; end; end.