[fpc-pascal] Implementing Factory Method with Pascal
luciano de souza
luchyanus at gmail.com
Sat Nov 28 01:15:44 CET 2015
Your answer and the excelent article of Graeme clarify the question.
If I want to change the type of the instance, better is to use
generics. A factory is only a batch builder of the same objects.
Graeme, your article opened my mind. The mappings and the registers
caused me a very strong impression. Thank you!
2015-11-27 21:48 GMT-02:00, Marcos Douglas <md at delfire.net>:
> On Fri, Nov 27, 2015 at 7:44 PM, luciano de souza <luchyanus at gmail.com>
>> 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
> Decorator Pattern is more powerful than inheritance. Read about it.
> Best regards,
> Marcos Douglas
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
Luciano de Souza
More information about the fpc-pascal