[fpc-pascal] Implementing Factory Method with Pascal

Marcos Douglas md at delfire.net
Sat Nov 28 00:48:52 CET 2015


On Fri, Nov 27, 2015 at 7:44 PM, luciano de souza <luchyanus at gmail.com> wrote:
> If I need to do "TAnimalFactory.create(atDog) as Tdog", perhaps, it
> would be better not to use a factory, doing simply "TDog.create".

You're right, use simply TDog.Create.
Why do you have a factory to create different types of animals
(classes)? Doesn't make sense. If you need a factory, you should use a
factory for dogs, another for cats and so on.
Another tip: Factories resolve some problems but there is a cost. The
factory will creates your instance (object) but it know only one
constructor, ie, the base class constructor.
Objects should be immutable at first place, only if you have a good
reason for don't use immutability. So, if they should be immutable,
you have only the constructor to instantiate your object, passing
arguments for it. If you have a constructor without arguments (using a
base class for example), you won't pass arguments to instantiate your
classe properly.
Another tip: use interfaces, not inheritance. Inheritance is evil. You
always will have problems using inheritance. Instead, use small
objects with few methods (2-5) to resolve just one problem. You don't
need inheritance for that. Your code will be more simpler and
customizable.
Decorator Pattern is more powerful than inheritance. Read about it.

Best regards,

Marcos Douglas



More information about the fpc-pascal mailing list