Skip to main content

Python implementation of the Delegation Pattern

Project description

Test Coverage Stable Version Pre-release Version PyPI - Python Version PyPI Downloads

delegate-pattern: Python implementation of the Delegation Pattern.

delegate-pattern provides a basic implementation of the well-known Delegation Pattern.

What is delegation

Delegation is a pattern in object oriented programming where a class (delegator) delegates responsibilities to one or more delegates.

This allows for greater code reusability and reduced class complexity and may help adhering to the DRY (Do not Repeat Yourself) and SoC (Separation of Concerns) principles.

Example

Consider a trivial task of delegating the printing of an objects name to the console. Here the delegator SomeClass delegates the task to the delegate PrintNameDelegate which is not much more than a function wrapped in a class.

The delegate class is always initialized with an argument specifying its delegator, and while this example shows the use of a protocol class called NamedClassProtocol, this is merely included for convenience, and will not be enforced at runtime. In fact the type of delegator can be anything, but delegate constructors cannot have any other arguments.

Note that it's strongly recommended to use weak references to the delegator in the delegate constructor.

from typing import Protocol
from weakref import ref
from delegate.pattern import delegate

class NamedClassProtocol(Protocol):
    _name: str

class PrintNameDelegate:
    # this is a stateful delegate because its constructor
    # takes a 'delegator' argument

    def __init__(self, delegator: NamedClassProtocol):
        self.__delegator = delegator

    def __call__(self):
        print(self.__delegator._name)

class NamePropertyDelegate:
    # this is a stateless delegate because it has no
    # constructor which takes a 'delegator' argument

    def __get__(self, delegator: NamedClassProtocol) -> str:
        return delegator._name

    def __set__(self, delegator: NamedClassProtocol, value: str):
        delegator._name = value

class SomeClass:
    _name: str
    def __init__(self, name: str) -> None:
        self._name = name

    name_printer = delegate(PrintNameDelegate) # => PrintNameDelegate instance
    name = delegate(NamePropertyDelegate, str) # => string getter

some_instance = SomeClass("Neo")
some_instance.name_printer() # prints Neo

name = some_instance.name # => Neo
some_instance.name = "Trinity"
new_name = some_instance.name # => Trinity

Full documentation

Go to documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

delegate_pattern-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

delegate_pattern-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file delegate_pattern-0.1.0.tar.gz.

File metadata

  • Download URL: delegate_pattern-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for delegate_pattern-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c8deeceaff9715d61a302ab2f7a6dd8bc75f1967ebe16fb0ead4560652dc8fea
MD5 dd7d50088d41d54f4e73b7fdb3a46de6
BLAKE2b-256 08cd3c42ae55165c59446dca5c358d29cc4c9a85833ffa3da95b77f6309625b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for delegate_pattern-0.1.0.tar.gz:

Publisher: python-publish.yml on apmadsen/delegate-pattern

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file delegate_pattern-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for delegate_pattern-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 624058a99072893b006c467d947c5ccb23ebfc3dc1098d54536059fdabbd9524
MD5 95dd7e1013686e886c5c639b084b4a46
BLAKE2b-256 5b1fb6b9f2586410f51449b1ba5572b468e6bb09ef2ec7de0f4909d9bb4b689d

See more details on using hashes here.

Provenance

The following attestation bundles were made for delegate_pattern-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on apmadsen/delegate-pattern

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page