Simplified the Abstract Factory Pattern to be Pythonic#215
Merged
Conversation
Once we appreciate that: 1) classes are their own factories and 2) classes are first class objects in Python, this pattern is massively simpler in idiomatic Python. Additional factory classes are not needed. This change also removes the `get_food` method because: 1) This violates the single responsibility principle of the factory 2) It obscures the main point. If we want an association between animals and what food they eat we should either: 1) make it part of the Pet interface 2) or, have another callable which can make this decision (presumably it should be passed the animal instance in order to do so). Or, if we really need it, create an interface that does both creation and feeding as before, but to have that in the this pattern obscures the simplicity of how this can and should be implemented in Python. The second implementation was also deleted. This simply looks up the classes using a string name, which is pointless when you can just use the class itself (why use `"kitty"` when you can use `Cat`).
This all removes all the low value tests which were testing things that were trivial, irrelevant to the point or now no longer exist.
Codecov Report
@@ Coverage Diff @@
## master #215 +/- ##
==========================================
- Coverage 97.11% 97.04% -0.07%
==========================================
Files 61 61
Lines 2358 2303 -55
==========================================
- Hits 2290 2235 -55
Misses 68 68
Continue to review full report at Codecov.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Once we appreciate that:
this pattern is massively simpler in idiomatic Python. Additional factory
classes are not needed.
This change also removes the
get_foodmethod because:If we want an association between animals and what food they eat we should either:
(presumably it should be passed the animal instance in order to do so).
Or, if we really need it, create an interface that does both creation and
feeding as before, but to have that in the this pattern obscures the
simplicity of how this can and should be implemented in Python.
The second implementation was also deleted. This simply looks up the
classes using a string name, which is pointless when you can just
use the class itself (why use
"kitty"when you can useCat).