| Name | delegate-pattern JSON | 
            
| Version | 
                  0.1.0
                   
                  JSON | 
            
 | download  | 
            
| home_page | None  | 
            
| Summary | Python implementation of the Delegation Pattern | 
            | upload_time | 2025-10-27 07:36:25 | 
            | maintainer | None | 
            
            | docs_url | None | 
            | author | None | 
            
            | requires_python | >=3.10 | 
            
            
            | license | None | 
            | keywords | 
                
                    windows
                
                     linux
                
                     delegates
                 | 
            | VCS | 
                
                     | 
                
            
            | bugtrack_url | 
                
                 | 
             
            
            | requirements | 
                
                  No requirements were recorded.
                
             | 
            
| Travis-CI | 
                
                   No Travis.
                
             | 
            | coveralls test coverage | 
                
                     
                
             | 
        
        
            
            [](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test.yml)
[](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test-coverage.yml)
[](https://github.com/apmadsen/delegate-pattern/releases)


[](https://pepy.tech/projects/delegate-pattern)
# 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.
```python
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](https://github.com/apmadsen/delegate-pattern/blob/main/docs/documentation.md)
            
         
        Raw data
        
            {
    "_id": null,
    "home_page": null,
    "name": "delegate-pattern",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "windows, linux, delegates",
    "author": null,
    "author_email": "Anders Madsen <anders.madsen@alphavue.com>",
    "download_url": "https://files.pythonhosted.org/packages/08/cd/3c42ae55165c59446dca5c358d29cc4c9a85833ffa3da95b77f6309625b9/delegate_pattern-0.1.0.tar.gz",
    "platform": null,
    "description": "[](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test.yml)\n[](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test-coverage.yml)\n[](https://github.com/apmadsen/delegate-pattern/releases)\n\n\n[](https://pepy.tech/projects/delegate-pattern)\n\n# delegate-pattern: Python implementation of the Delegation Pattern.\n\ndelegate-pattern provides a basic implementation of the well-known Delegation Pattern.\n\n## What is delegation\n\nDelegation is a pattern in object oriented programming where a class (delegator) delegates responsibilities to one or more delegates.\n\nThis 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.\n\n\n\n## Example\n\nConsider 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.\n\nThe 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.\n\nNote that it's strongly recommended to use weak references to the delegator in the delegate constructor.\n\n```python\nfrom typing import Protocol\nfrom weakref import ref\nfrom delegate.pattern import delegate\n\nclass NamedClassProtocol(Protocol):\n    _name: str\n\nclass PrintNameDelegate:\n    # this is a stateful delegate because its constructor\n    # takes a 'delegator' argument\n\n    def __init__(self, delegator: NamedClassProtocol):\n        self.__delegator = delegator\n\n    def __call__(self):\n        print(self.__delegator._name)\n\nclass NamePropertyDelegate:\n    # this is a stateless delegate because it has no\n    # constructor which takes a 'delegator' argument\n\n    def __get__(self, delegator: NamedClassProtocol) -> str:\n        return delegator._name\n\n    def __set__(self, delegator: NamedClassProtocol, value: str):\n        delegator._name = value\n\nclass SomeClass:\n    _name: str\n    def __init__(self, name: str) -> None:\n        self._name = name\n\n    name_printer = delegate(PrintNameDelegate) # => PrintNameDelegate instance\n    name = delegate(NamePropertyDelegate, str) # => string getter\n\nsome_instance = SomeClass(\"Neo\")\nsome_instance.name_printer() # prints Neo\n\nname = some_instance.name # => Neo\nsome_instance.name = \"Trinity\"\nnew_name = some_instance.name # => Trinity\n```\n\n## Full documentation\n\n[Go to documentation](https://github.com/apmadsen/delegate-pattern/blob/main/docs/documentation.md)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python implementation of the Delegation Pattern",
    "version": "0.1.0",
    "project_urls": {
        "repository": "https://github.com/apmadsen/delegate-pattern"
    },
    "split_keywords": [
        "windows",
        " linux",
        " delegates"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5b1fb6b9f2586410f51449b1ba5572b468e6bb09ef2ec7de0f4909d9bb4b689d",
                "md5": "95dd7e1013686e886c5c639b084b4a46",
                "sha256": "624058a99072893b006c467d947c5ccb23ebfc3dc1098d54536059fdabbd9524"
            },
            "downloads": -1,
            "filename": "delegate_pattern-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "95dd7e1013686e886c5c639b084b4a46",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 11016,
            "upload_time": "2025-10-27T07:36:24",
            "upload_time_iso_8601": "2025-10-27T07:36:24.564159Z",
            "url": "https://files.pythonhosted.org/packages/5b/1f/b6b9f2586410f51449b1ba5572b468e6bb09ef2ec7de0f4909d9bb4b689d/delegate_pattern-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "08cd3c42ae55165c59446dca5c358d29cc4c9a85833ffa3da95b77f6309625b9",
                "md5": "dd7d50088d41d54f4e73b7fdb3a46de6",
                "sha256": "c8deeceaff9715d61a302ab2f7a6dd8bc75f1967ebe16fb0ead4560652dc8fea"
            },
            "downloads": -1,
            "filename": "delegate_pattern-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "dd7d50088d41d54f4e73b7fdb3a46de6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 9818,
            "upload_time": "2025-10-27T07:36:25",
            "upload_time_iso_8601": "2025-10-27T07:36:25.939552Z",
            "url": "https://files.pythonhosted.org/packages/08/cd/3c42ae55165c59446dca5c358d29cc4c9a85833ffa3da95b77f6309625b9/delegate_pattern-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-27 07:36:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "apmadsen",
    "github_project": "delegate-pattern",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "delegate-pattern"
}