Chain of Responsabilities

hivecore.patterns.chain_of_responsibility(responsable_key: str) Callable

Decorator to create a chain of responsibility for the given responsable key.

Parameters:

responsable_key (str) – The key associated with the chain of responsibility.

Returns:

The decorated class acting as the chain of responsibility.

Return type:

Callable

Responsable

hivecore.patterns.responsable(responsable_key: str, order: float = inf, **method_map: str) Callable

Decorator to mark a class as a responsable and associate it with a responsable key.

Parameters:
  • responsable_key (str) – The key associated with the responsable.

  • order (float) – The order of execution for the responsable (default: float(‘inf’)).

  • method_map (Dict[str, str]) – Keyword arguments representing the mapping of methods in the responsable.

Returns:

The decorated class.

Return type:

Callable

Example

The @responsable decorator helps define responsabilities for the chain of responsabilities.

from hivecore.patterns import responsable

@responsable("currency")
class Hundredresponsable:
    def handle(self, amount):
        if amount >= 100:
            num = amount // 100
            remainder = amount % 100
            print(f"Dispensing {num} $100 note")
            return remainder
        return amount

@responsable("currency")
class Fiftyresponsable:
    def handle(self, amount):
        if amount >= 50:
            num = amount // 50
            remainder = amount % 50
            print(f"Dispensing {num} $50 note")
            return remainder
        return amount

@responsable("currency")
class Twentyresponsable:
    def handle(self, amount):
        if amount >= 20:
            num = amount // 20
            remainder = amount % 20
            print(f"Dispensing {num} $20 note")
            return remainder
        return amount

Creating a chain of responsabilities

The @chain_of_responsibility decorator helps define the class that is going to orchastrate the responsabilities. Note that execution order is defined by the order the responsabilities are defined unless the order parameter is used for the @responsable decorator.

from hivecore.patterns import chain_of_responsibility

@chain_of_responsibility("currency")
    class ATMDispenser:
        def dispense(self, amount):
            self.handle_request(amount)

Usage

# Create chain object
atm = ATMDispenser()

# Perform operations
atm.dispense(580)

Output

Dispensing 5 $100 note
Dispensing 1 $50 note
Dispensing 1 $20 note