_images/software-patterns-logo.png

CircleCI Codecov Better Code Hub Scrutinizer code quality PyPI PyPI - Python Version PyPI - Wheel GitHub commits since tagged version (branch)


These are the documentation pages of the Software Patterns libre software.
Software Patterns’ open source code is hosted on the boromir674/software-patterns repository on Github and its respective software-patterns Python package (both source and wheel distributions) on Pypi.
It also features 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.

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)

Indices and tables