Monday, July 5, 2010

Still use singleton? There's another choice in python: Borg design pattern

Few months ago i implemented a db extension for my own python web framework, which supports different database backend. To prevent it from creating multiple instance of db-connection i have chosen singleton design pattern as usual. But after spent some time on google i found another interesting design pattern Borg. It's done this job as good as singleton. Let's take a look of this magical pattern:

class Borg:
    __shared_state = {}
    def __init__(self):
        self.__dict__ = self.__shared_state
OK, that's it. Have fun.

2 comments:

hrroman said...

If you are looking for the Finance Accounting Assignments help then in this case you can opt for our Finance Accounting Assignments help.we provide the best Accounting and Finance Assignment.We also provide Financial Services Online Essay Help for students across the globe. for more information contact us +16692714848.

Does Trader Joe’s Accept EBT Card said...

This is a really too good post. This article gives truly quality and helpful information. I’m definitely going to look into it. Also Check fax services near me