Skip to content

Simplified the Abstract Factory Pattern to be Pythonic#215

Merged
faif merged 2 commits into
faif:masterfrom
spookylukey:pythonic_abstract_factory
Feb 1, 2018
Merged

Simplified the Abstract Factory Pattern to be Pythonic#215
faif merged 2 commits into
faif:masterfrom
spookylukey:pythonic_abstract_factory

Conversation

@spookylukey
Copy link
Copy Markdown
Contributor

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).

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-io
Copy link
Copy Markdown

Codecov Report

Merging #215 into master will decrease coverage by 0.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
tests/test_abstract_factory.py 100% <100%> (ø) ⬆️
creational/abstract_factory.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ac79516...6127f9d. Read the comment docs.

@faif faif merged commit 5e05ca1 into faif:master Feb 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants