mercredi 28 juin 2017

Python façade class: proper way to redirect methods

A façade. SmallManager objects are composed of a Manager object:

---------
Manager
---------
method1(a1, a2, a3)
method2()
method3()

---------
SmallManager           <--- façade of LargeManager
---------
__manager: Manager
method1(a1, a2, a3)    <--- Manager.method1()

I don't want to modify the arguments a1, a2, and a2 in SmallManager.method1 every time they are changed in Manager.method1. I could lazily implement SmallManager like this:

Implementation_1:

class SmallManager:
    def __init__(self, large_manager: Manager):
        self.__manager = large_manager
        self.self.__manager.method1

Implementation_2:

class SmallManager:
    def __init__(self, large_manager: Manager):
        self.__manager = large_manager

    def method1(self, *args, **kwargs):
        return self.__manager.method1(*args, **kwargs)

Problem:

Implementation_1:

when calling small_manager_instance.method1, the IDE can tell the user that this method is Manager.method1. Also, I might be possible for the user of SmallManager to get Manager attributes. This contradicts the façade pattern?

Implementation_2:

the IDE cannot sugggest the required arguments (pycharm suggests *args and **kwargs). Also I think I would need to rewrite the type hints in SmallManager if they are changed in Manager.method1.

What is the best option? Any other suggestions?

Thanks

Aucun commentaire:

Enregistrer un commentaire