Headline

The Borg Pattern in Language:Python

Motivation

This implementation illustrates the Borg Pattern in Python, it is similar to the singleton pattern but the singleton pattern ensures instance equality which the Borg pattern does not, instead it ensures state equality. Using this pattern, one has several instances but they all have the exact same state. An advantage of this pattern is the possibility to write normal python classes and deriving from the Borg class is sufficient to make it a "Borg".

Illustration

The data model is mostly adapted from Contribution:py3k.

class Borg(object):

    state = {}
    def __init__(self):
        self.__dict__ = Borg.state
        

This class makes sure that all instances of a class derived from the Borg class have the exact same state by writing to the " dict " attribute which holds all attributes and methods of an object in Python. The Company class is very similar to the normal version.

class Company(Borg):
    
    def __init__(self, name, subunits):
        Borg.__init__(self)
        self.name = name
        self.subunits = subunits


By deriving from the Borg class, you can do this:

company = Company("Meganalysis", ["..."])
first_total = company.total()
company2 = Company("Meganalysis",["..."])
company2.cut()
assert company.total() * 2.0 == first_total

Usage

Just run python program.py


Kevin edited this article at Mon, 07 Jun 2021 20:31:37 +0200
Compare revisions Compare revisions

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.