jeudi 30 août 2018

Chain of responsibility pattern rookie attempt

I'm trying to find understanding of Chain of responsibility pattern, and, as an exercise, I've wrote some code. This code

class SomeObject:
    def __init__(self):
        self.integer_field = 0
        self.float_field = 0.0
        self.string_field = 'hi'


class EventGet:
    def __init__(self, data_type):
        self.data_type = data_type

class EventSet:
    def __init__(self, value):
        self.value = value


class NullHandler:
    def __init__(self, successor=None):
        self.__successor = successor

    def handle(self, obj, event):
        if self.__successor:
            self.__successor.handle(obj, event)


class IntHandler(NullHandler):
    def handle(self, obj, event):
        if isinstance(event, EventGet) and event.data_type is int:
            return obj.integer_field
        elif isinstance(event, EventSet) and isinstance(event.value, int):
            obj.integer_field = event.value
        else:
            super().handle(obj, event)


class FloatHandler(NullHandler):
    def handle(self, obj, event):
        if isinstance(event, EventGet) and event.data_type is float:
            return obj.float_field
        elif isinstance(event, EventSet) and isinstance(event.value, float):
            obj.float_field = event.value
        else:
            super().handle(obj, event)


class StrHandler(NullHandler):
    def handle(self, obj, event):
        if isinstance(event, EventGet) and event.data_type is str:
            return obj.string_field
        elif isinstance(event, EventSet) and isinstance(event.value, str):
            obj.string_field = event.value
        else:
            super().handle(obj, event)

But it doesn't work as I'm expecting. Being invoked in the following manner

obj = SomeObject()
chain = IntHandler(FloatHandler(StrHandler(NullHandler())))

print(chain.handle(obj, EventGet(int)))
print(chain.handle(obj, EventGet(float)))
print(chain.handle(obj, EventGet(str)))

chain.handle(obj, EventSet(1))
print(obj.integer_field)    

chain.handle(obj, EventSet(1.1))
print(obj.float_field)

chain.handle(obj, EventSet('str'))
print(obj.string_field)

it produces the confusing output:

0 # That's ok
None # Why not 0.0?
None # Why not hi?
1 # That's ok
1.1 # That's ok
str # That's ok

I can't figure out why calling the chain.handle(obj, EventGet(float)) and chain.handle(obj, EventGet(str)) produces None. After all, there's a return statement in corresponding positions (i.e. return obj.float_field and return obj.string_field).

Can you please explain to me what is wrong in this code and how to make it work as expected? Many thanks in advance!

Aucun commentaire:

Enregistrer un commentaire