Skip to content

Commit a7045f8

Browse files
committed
Adds description to Abstract Factory
1 parent 5f0e1eb commit a7045f8

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

creational/abstract_factory.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
# http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
4+
"""
5+
*What is this pattern about?
6+
The Abstract Factory Pattern serves to provide an interface for
7+
creating related/dependent objects without need to specify their
8+
actual class.
9+
The idea is to abstract the creation of objects depending on business
10+
logic, platform choice, etc.
11+
12+
*What does this example do?
13+
This particular implementation abstracts the creation of a pet and
14+
does so depending on the AnimalFactory we chose (Dog or Cat)
15+
This works because both Dog/Cat and their factories respect a common
16+
interface (.speak(), get_pet() and get_food()).
17+
Now my application can create pets (and feed them) abstractly and decide later,
18+
based on my own criteria, dogs over cats.
19+
The second example allows us to create pets based on the string passed by the
20+
user, using cls.__subclasses__ (the list of sub classes for class cls)
21+
and sub_cls.__name__ to get its name.
22+
23+
*Where is the pattern used practically?
24+
25+
*References:
26+
https://sourcemaking.com/design_patterns/abstract_factory
27+
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
28+
29+
"""
30+
531

6-
"""Implementation of the abstract factory pattern"""
732
import six
833
import abc
934
import random

0 commit comments

Comments
 (0)