samedi 4 mars 2017

Design pattern for exclusive method calls?

Let's assume I have a class A, representing a piece of hardware. For simplicity sake let it be a camera.

Now class B wants to use class A and take pictures. In order to do that undisturbed, B requests exclusive usage permissions from A before doing anything else.

Now let's take another class C that performs some fancy operation with the camera, e.g. taking a panorama. But if B asks C to do that, we run into a problem: C isn't allowed to use A, since that one is used exclusively by B!

Now my question: is there some design pattern dealing with this kind of problem?

One way of course would be to have methods like acquire_exclusive() and release_exclusive() in all classes and passing around some kind of token. Then B could pass on exclusive permissions to C and so on. But this feels so complicated (especially introducing new parameters to methods, which are not related to the method at all) and I've got a feeling that there must be an easier solution, but I just don't see it...

If it helps: I'm working with Python.

Aucun commentaire:

Enregistrer un commentaire