Logo

Programming-Idioms

History of Idiom 73 > diff from v12 to v13

Edit summary for version 13 by :

Version 12

2015-09-17, 06:03:37

Version 13

2015-10-09, 08:16:02

Idiom #73 Create a factory

Create a factory named pFact for any sub class of Parent and taking exactly one string str as constructor parameter.

Idiom #73 Create a factory

Create a factory named pFact for any sub class of Parent and taking exactly one string str as constructor parameter.

Code
type Parent = class
  constructor create(const str: string);
end;

type ClassOfParent = class of Parent;

function pFact(ClassType: ClassOfParent; const str: string): Parent;
begin
  result := ClassType.Create(str);
end;
Comments bubble
Traditional Delphi or Object Pascal had poor compile-time features. The class of construct was the only way to make a factory for a particular object type
Demo URL
http://ideone.com/zIruEC