Python implementation of the Delegation Pattern
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8deeceaff9715d61a302ab2f7a6dd8bc75f1967ebe16fb0ead4560652dc8fea
|
|
| MD5 |
dd7d50088d41d54f4e73b7fdb3a46de6
|
|
| BLAKE2b-256 |
08cd3c42ae55165c59446dca5c358d29cc4c9a85833ffa3da95b77f6309625b9
|
Provenance
The following attestation bundles were made for delegate_pattern-0.1.0.tar.gz:
Publisher:
python-publish.yml on apmadsen/delegate-pattern
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
delegate_pattern-0.1.0.tar.gz -
Subject digest:
c8deeceaff9715d61a302ab2f7a6dd8bc75f1967ebe16fb0ead4560652dc8fea - Sigstore transparency entry: 643896268
- Sigstore integration time:
-
Permalink:
apmadsen/delegate-pattern@2ecbc4df125dce51f127cf57d4b671cc7f5b3be5 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/apmadsen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2ecbc4df125dce51f127cf57d4b671cc7f5b3be5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file delegate_pattern-0.1.0-py3-none-any.whl.
File metadata
- Download URL: delegate_pattern-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
624058a99072893b006c467d947c5ccb23ebfc3dc1098d54536059fdabbd9524
|
|
| MD5 |
95dd7e1013686e886c5c639b084b4a46
|
|
| BLAKE2b-256 |
5b1fb6b9f2586410f51449b1ba5572b468e6bb09ef2ec7de0f4909d9bb4b689d
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
delegate_pattern-0.1.0-py3-none-any.whl -
Subject digest:
624058a99072893b006c467d947c5ccb23ebfc3dc1098d54536059fdabbd9524 - Sigstore transparency entry: 643896273
- Sigstore integration time:
-
Permalink:
apmadsen/delegate-pattern@2ecbc4df125dce51f127cf57d4b671cc7f5b3be5 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/apmadsen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2ecbc4df125dce51f127cf57d4b671cc7f5b3be5 -
Trigger Event:
release
-
Statement type: