Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] Singleton Software Design Pattern
Release Software Design Patterns v1.3.0
- Loading branch information
Showing
18 changed files
with
347 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
[build-system] | ||
requires = ["setuptools >= 40.6.0", "wheel"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
|
||
## Black formatting/linting | ||
|
||
[tool.black] | ||
line-length = 95 | ||
include = '\.pyi?$' | ||
extend-exclude = ''' | ||
''' | ||
|
||
|
||
[tool.isort] | ||
profile = 'black' | ||
|
||
|
||
[tool.software-release] | ||
version_variable = "src/software_patterns/__init__.py:__version__" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
__version__ = '1.2.1' | ||
__version__ = '1.3.0' | ||
|
||
from .notification import Observer, Subject | ||
from .memoize import ObjectsPool | ||
from .proxy import ProxySubject, Proxy | ||
from .notification import Observer, Subject | ||
from .proxy import Proxy, ProxySubject | ||
from .singleton import Singleton | ||
from .subclass_registry import SubclassRegistry | ||
|
||
|
||
__all__ = ['Observer', 'Subject', 'ObjectsPool', 'SubclassRegistry', | ||
'ProxySubject', 'Proxy'] | ||
__all__ = [ | ||
'Observer', | ||
'Subject', | ||
'ObjectsPool', | ||
'SubclassRegistry', | ||
'ProxySubject', | ||
'Proxy', | ||
'Singleton', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Any, Optional | ||
|
||
|
||
class Handler(ABC): | ||
""" | ||
The Handler interface declares a method for building the chain of handlers. | ||
It also declares a method for executing a request. | ||
""" | ||
|
||
@abstractmethod | ||
def set_next(self, handler: Handler) -> Handler: | ||
pass | ||
|
||
@abstractmethod | ||
def handle(self, request) -> Optional[str]: | ||
pass | ||
|
||
|
||
class AbstractHandler(Handler): | ||
""" | ||
The default chaining behavior can be implemented inside a base handler | ||
class. | ||
""" | ||
|
||
_next_handler: Optional[Handler] = None | ||
|
||
def set_next(self, handler: Handler) -> Handler: | ||
self._next_handler = handler | ||
# Returning a handler from here will let us link handlers in a | ||
# convenient way like this: | ||
# monkey.set_next(squirrel).set_next(dog) | ||
return handler | ||
|
||
@abstractmethod | ||
def handle(self, request: Any) -> Optional[str]: | ||
if self._next_handler: | ||
return self._next_handler.handle(request) | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import typing as t | ||
|
||
|
||
class Singleton(type): | ||
"""Singleton Design Pattern imlemented as a Metaclass. | ||
Use this Metaclass to design your class (constructor) with the Singleton | ||
Pattern. Setting your class's 'metaclass' key to this Metaclass (see | ||
example below), will restrict object instantiation so that it always return | ||
the same (singleton) object. | ||
Example: | ||
>>> class ObjectDict(metaclass=Singleton): | ||
... def __init__(self): | ||
... super().__init__() | ||
... self.objects = {} | ||
>>> reg1 = ObjectDict() | ||
>>> reg1.objects['a'] = 1 | ||
>>> reg2 = ObjectRegistry() | ||
>>> reg2.objects['b'] = 2 | ||
>>> reg3 = ObjectRegistry() | ||
>>> reg2.objects == {'a': 1} | ||
True | ||
>>> reg3.objects['a'] == 1 | ||
True | ||
>>> reg3.objects['b'] == 2 | ||
True | ||
>>> id(reg1) == id(reg2) == id(reg3) | ||
True | ||
""" | ||
|
||
_instances: t.Mapping[t.Type, t.Any] = {} | ||
|
||
def __call__(cls: t.Type, *args, **kwargs) -> t.Any: | ||
instance = cls._instances.get(cls) | ||
if not instance: | ||
instance = super(Singleton, cls).__call__(*args, **kwargs) | ||
cls._instances[cls] = instance | ||
return instance |
Oops, something went wrong.