/software-patterns

Reusable Software Design Patterns with types, in Python.

Primary LanguagePythonGNU Affero General Public License v3.0AGPL-3.0

Software Patterns

A Python package with common Software Design Patterns.

build
CircleCI Codecov Read the Docs (version)
package
PyPI PyPI - Python Version PyPI - Downloads PyPI - Wheel GitHub commits since tagged version (branch)
code quality Better Code Hub Scrutinizer code quality

Documentation: https://software-patterns.readthedocs.io/en/master
Source Code: https://github.com/boromir674/software-patterns/tree/master
CI/CD: https://circleci.com/gh/boromir674/software-patterns/

Overview

This repository hosts the open-source code of the Software Patterns project. The project's main component is the software-patterns python package hosted on Pypi at https://pypi.org/project/software-patterns It also features online Documentation Pages hosted at https://software-patterns.readthedocs.io/ and a public CI workflow hosted on CircleCI.

What are Software Design Patterns?

Software Engineers are employing various designs and solutions to solve their problems. The emerging (software) patterns, among the code solutions, targeting reoccuring problems have been studied and formalized in terms of how they are used, what problem they solve and why they are a fit candidate to solve it. These code designs, which are frequently found in various code bases, are known as Software Design Patterns.

The software-patterns package exposes a set of Python Classes that allow convient usage of common Software Design Patterns.

Design Patterns implemented:

  • Notification (aka Broadcast/Listener pattern)
  • Object Pool (aka Memoize)
  • Classes Registry (aka Abstract Factory)
  • Proxy

Installation

Install from the Pypi server:

pip install software-patterns

Quickstart

Example code to use the factory pattern in the form of a (sub) class registry:

from software_patterns import SubclassRegistry

class MyClassRegistry(metaclass=SubclassRegistry):
    pass

@MyClassRegistry.register_as_subclass('a')
class ClassA:
    def __init__(self, number):
        self.attr = number

@MyClassRegistry.register_as_subclass('b')
class ClassB:
    def __init__(self, number):
        self.attr = number - 1

assert MyClassRegistry.subclasses == {'a': ClassA, 'b': ClassB}

instance_a = MyClassRegistry.create('a', 10)
assert type(instance_a) == ClassA
assert instance_a.attr == 10

assert isinstance(instance_a, ClassA)

instance_b = MyClassRegistry.create('b', 10)
assert type(instance_b) == ClassB
assert instance_b.attr == 9

assert isinstance(instance_b, ClassB)