Since Python is a duck-typed language is writing factory classes meaningless in Python? http://en.wikipedia.org/wiki/Factory_method_pattern
While there may be times when the factory pattern is unnecessary where it may be required in other languages, there are still times when it would be valid to use it - it might just be a way of making your API cleaner - for example as a way of preventing duplication of code that decides which of a series of subclasses to return.
From the Wikipedia article you linked:
Use the factory pattern when:
- The creation of the object precludes reuse without significantly duplicating code.
- The creation of the object requires access to information or resources not appropriate to contain within the composing object.
- The lifetime management of created objects needs to be centralised to ensure consistent behavior.
All of these can still apply when the language is duck typed.
It's not exactly a factory class, but the Python standard library has at least one factory method: http://docs.python.org/library/collections.html#collections.namedtuple.
And then, of course, there's the fact that you can create classes dynamically using the type()
builtin.
I wouldn't say they're meaningless so much as that Python offers a large amount of possibilities for the sort of factories you can create. It's comparatively simple even to write a factory class that creates factory classes as callable instances of itself.
© 2022 - 2024 — McMap. All rights reserved.