2015-05-14 11:38:56 +03:00
|
|
|
"""`Factory` and `Singleton` providers example."""
|
2015-04-14 23:17:53 +03:00
|
|
|
|
2015-05-14 11:38:56 +03:00
|
|
|
from objects.providers import Factory
|
2015-04-14 23:17:53 +03:00
|
|
|
from objects.providers import Singleton
|
|
|
|
|
|
|
|
|
2015-05-14 11:38:56 +03:00
|
|
|
# Factory provider creates new instance of specified class on every call.
|
2015-05-15 17:44:20 +03:00
|
|
|
object_factory = Factory(object)
|
2015-04-14 23:17:53 +03:00
|
|
|
|
2015-05-15 17:44:20 +03:00
|
|
|
object_1 = object_factory()
|
|
|
|
object_2 = object_factory()
|
2015-04-14 23:17:53 +03:00
|
|
|
|
|
|
|
assert object_1 is not object_2
|
|
|
|
assert isinstance(object_1, object) and isinstance(object_2, object)
|
|
|
|
|
|
|
|
# Singleton provider creates new instance of specified class on first call
|
|
|
|
# and returns same instance on every next call.
|
|
|
|
single_object = Singleton(object)
|
|
|
|
|
|
|
|
single_object_1 = single_object()
|
|
|
|
single_object_2 = single_object()
|
|
|
|
|
|
|
|
assert single_object_1 is single_object_2
|
|
|
|
assert isinstance(object_1, object) and isinstance(object_2, object)
|