Logo

Programming-Idioms

History of Idiom 73 > diff from v9 to v10

Edit summary for version 10 by :

Version 9

2015-09-03, 17:02:38

Version 10

2015-09-04, 13:02:16

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;
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
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
Demo URL
http://ideone.com/zIruEC