This is a variation on my previous question on how to hide inherited constructors. How do you hide inherited methods:
Modelling after the way Delphi lets you construct COM objects:
CoDOMDocument = class
class function Create: IXMLDOMDocument2;
end;
i have a factory that creates an object that implements an interface:
CoCondition = class
public
class function Create: ICondition;
end;
This works fine and great. It works fine, even though there's a method in the ancestor called Create
. It works because i don't have the overload
keyword present. As soon as i add the overload
keyword: Delphi will allow the inherited Create
method to "shine through":
CoCondition = class
public
class function Create: ICondition; overload;
end;
So now CoCondition
has two Create
method available:
class function CoCondition.Create: ICondition;
constructor TObject.Create;
And it's ambiguous over which one you want to call. The fix, obviously is to just not have the overload
keyword (Why would you, you're not overloading anything?). Well it turns out i am overloading something:
CoCondition = class
public
class function Create: ICondition; overload;
class function Create(const ConditionType: TConditionType): ICondition; overload;
class function Create(const PropertyName: string; const Operation: TConditionOperation; const Value: Variant): ICondition; overload;
class function Create(const ConditionType: TConditionType; const SubConditions: IInterfaceList): ICondition; overload;
end;
Since i have the overload
keyword, the class has five overloads, rather than just the four i want:
class function CoCondition.Create: ICondition;
class function CoCondition.Create(const ConditionType: TConditionType): ICondition; overload;
class function CoCondition.Create(const PropertyName: string; const Operation: TConditionOperation; const Value: Variant): ICondition; overload;
class function CoCondition.Create(const ConditionType: TConditionType; const SubConditions: IInterfaceList): ICondition; overload;
constructor TObject.Create;
i only want my four overloads to be present, and no others. i.e. i want to hide any ancestor methods.
How do i hide ancestor methods?
i also tried explicitely declaring the ancestor method, but having it in protected, so nobody can get at it:
CoCondition = class
protected
constructor Create; overload;
public
class function Create(): ICondition; overload;
class function Create(const ConditionType: TConditionType): ICondition; overload;
class function Create(const PropertyName: string; const Operation: TConditionOperation; const Value: Variant): ICondition; overload; //leaf
class function Create(const ConditionType: TConditionType; const SubConditions: IInterfaceList): ICondition; overload; //AND/OR/NOT children
end;
But that doesn't compile because of ambigious overload of unparametered Create()
.
i also considered:
CoCondition = class
public
class function Make(): ICondition; overload;
class function Make(const ConditionType: TConditionType): ICondition; overload;
class function Make(const PropertyName: string; const Operation: TConditionOperation; const Value: Variant): ICondition; overload; //leaf
class function Make(const ConditionType: TConditionType; const SubConditions: IInterfaceList): ICondition; overload; //AND/OR/NOT children
end;
But rejected it.
i could just expose the object that implements the object:
TCondition = class(TInterfacedObject, ICondition)
...
public
constructor Create; overload;
constructor Create(const ConditionType: TConditionType); overload;
constructor Create(const PropertyName: string; const Operation: TConditionOperation; const Value: Variant); overload; //leaf
constructor Create(const ConditionType: TConditionType; const SubConditions: IInterfaceList); overload; //AND/OR/NOT children
end;
But i thought all the cool kids hide their objects.
CreateInstance
and be done with it. – The_Fox