File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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"""
732import six
833import abc
934import random
Original file line number Diff line number Diff line change 22# -*- coding : utf-8 -*-
33
44"""
5+ *What is this pattern about?
6+ It decouples the creation of a complex object and its representation,
7+ so that the same process can be reused to build objects from the same
8+ family.
9+ This is useful when you must separate the specification of an object
10+ from its actual representation (generally for abstraction).
11+
12+ *What does this example do?
13+ This particular example uses a Director to abtract the
14+ construction of a building. The user specifies a Builder (House or
15+ Flat) and the director specifies the methods in the order necessary
16+ creating a different building dependding on the sepcified
17+ specification (through the Builder class).
18+
519@author: Diogenes Augusto Fernandes Herminio <diofeher@gmail.com>
620https://gist.github.com/420905#file_builder_python.py
21+
22+ *Where is the pattern used practically?
23+
24+ *References:
25+ https://sourcemaking.com/design_patterns/builder
26+
727"""
828
929
Original file line number Diff line number Diff line change 22# -*- coding: utf-8 -*-
33
44"""
5+ *What is this pattern about?
6+ This pattern is used when creating an object is costly (and they are
7+ created frequently) but only a few are used at a time. With a Pool we
8+ can manage those instances we have as of now by caching them. Now it
9+ is possible to skip the costly creation of an object if one is
10+ available in the pool.
11+ A pool allows to 'check out' an inactive object and then to return it.
12+ If none are available the pool creates one to provide without wait.
13+
14+ *What does this example do?
15+ In this example queue.Queue is used to create the pool (wrapped in a
16+ custom ObjectPool object to use with the with statement), and it is
17+ populated with strings.
18+ As we can see, the first string object put in "yam" is USED by the
19+ with statement. But because it is released back into the pool
20+ aftwerwards it is reused by the explicit call to sample_queue.get().
21+ Same thing happens with "sam", when the ObjectPool created insided the
22+ function is deleted (by the GC) and the object is returned.
23+
24+ *Where is the pattern used practically?
25+
26+ *References:
527http://stackoverflow.com/questions/1514120/python-implementation-of-the-object-pool-design-pattern
28+ https://sourcemaking.com/design_patterns/object_pool
629"""
730
831
9-
1032class ObjectPool (object ):
1133
1234 def __init__ (self , queue , auto_get = False ):
You can’t perform that action at this time.
0 commit comments