Comparing revisions for page pyBorg
Back to history list==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 runpython program.py
== Metadata == * [[uses::Language:Python]] * [[implements::Feature:Hierarchical company]] * [[implements::Feature:Total]] * [[implements::Feature:Cut]] * [[developedBy::Contributor:kevin-klein]]