I'm currently working on somekind of device handler. The idea is to use Observer design pattern.
Let's say for example you as a 'user' want to add a new device for everyone else to use and would be globally avaiable in scope of the program.
What I wanted to do is implement DeviceHandler
base class which will be marked as singleton using decorator. There would be 3 publicly visible methods register
, unregister
and get_device_list
.
Also I wanted to implement a base class Device
which you as a 'user' must inherit in order that your 'device' can be registered. Using abc.ABCMeta
and abc.abstractmethod
I would force your class to implement 2 functions get_instance()
and find_me()
. find_me()
returns either True or False (depending if your device is connected to the system or not), get_instance()
returns obviously instance of your class.
When an instance of DeviceHandler
is created, in the __init__
method daemon thread is created which is running __discover
method with 1 second period. __discover
basicly calls find_me()
method of registered device and if method returns True
then get_instance()
is called XY times until valid instance of your class is obtained (one call per iteration).
What the problem is that my DeviceHandler
class expects your __init__
method to be non-blocking. When I call get_instance()
I either expect an instance of your class or somekind of error code that would inform me that your class is not yet instantiated and that get_instance()
needs to be called XY times until I get valid instance of your class (you could implement some kind of state-machine inside your class).
I wanted to find out what is better approach in acquiring instance of your class. Should I force you to implement get_instance()
method and then you would implement state-machine handling (if needed) inside that method or should i just lose that method and instantiate your class as usually YourDeviceClass()
and then you would implement state-maching handling in the __init__
method?
Aucun commentaire:
Enregistrer un commentaire