# Path: src/singleton/singleton_factory.py
# class SingletonFactory(object):
#     """
#     A factory of the same instances of injected classes.
#     """
# 
#     # a mapping between the name of a class and the instance.
#     __mappings = {}
# 
#     @staticmethod
#     def build(class_, **constructor_args):
#         """
#         Builds an instance of the given class pointer together with the provided constructor arguments.
#         Returns the SAME instance for a given class.
#         
#         :param class_: A pointer to the definition of the class. 
#         :param constructor_args: The arguments for the class instance.
#         :return: An instance of the provided class.
#         """
# 
#         # if the class instance is mapped, then retrieve it.
#         if str(class_) in SingletonFactory.__mappings:
#             instance_ = SingletonFactory.__mappings[str(class_)]
# 
#         # else create the instance and map it to the class name.
#         else:
#             instance_ = class_(**constructor_args)
#             SingletonFactory.__mappings[str(class_)] = instance_
# 
#         return instance_


from unittest import TestCase
from src.singleton.singleton_factory import SingletonFactory

"""
Description: 

@author: Eugen
@date: 24/07/2017
"""



class A(object):

    def __init__(self, name):
        self.__name = name

    def __str__(self):
        return 'A' + str(self.__name)


class B(object):

    def __init__(self, name):
        self.__name = name

    def __str__(self):
        return 'B' + str(self.__name)


class TestSingletonFactory(TestCase):
    def test_build(self):
        a = SingletonFactory.build(A, name='class A')

# Path: src/singleton/singleton_factory.py
# class SingletonFactory(object):
#     """
#     A factory of the same instances of injected classes.
#     """
# 
#     # a mapping between the name of a class and the instance.
#     __mappings = {}
# 
#     @staticmethod
#     def build(class_, **constructor_args):
#         """
#         Builds an instance of the given class pointer together with the provided constructor arguments.
#         Returns the SAME instance for a given class.
#         
#         :param class_: A pointer to the definition of the class. 
#         :param constructor_args: The arguments for the class instance.
#         :return: An instance of the provided class.
#         """
# 
#         # if the class instance is mapped, then retrieve it.
#         if str(class_) in SingletonFactory.__mappings:
#             instance_ = SingletonFactory.__mappings[str(class_)]
# 
#         # else create the instance and map it to the class name.
#         else:
#             instance_ = class_(**constructor_args)
#             SingletonFactory.__mappings[str(class_)] = instance_
# 
#         return instance_


# Path: tests/singleton/test_singleton_factory.py
from unittest import TestCase
from src.singleton.singleton_factory import SingletonFactory

"""
Description: 

@author: Eugen
@date: 24/07/2017
"""



class A(object):

    def __init__(self, name):
        self.__name = name

    def __str__(self):
        return 'A' + str(self.__name)


class B(object):

    def __init__(self, name):
        self.__name = name

    def __str__(self):
        return 'B' + str(self.__name)


class TestSingletonFactory(TestCase):
    def test_build(self):
        a = SingletonFactory.build(A, name='class A')

        aa = SingletonFactory.build(A)
