mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-22 18:01:07 +00:00
28 lines
563 B
Python
28 lines
563 B
Python
import threading
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
class Wrapper(ABC):
|
|
@abstractmethod
|
|
def get(self):
|
|
pass
|
|
|
|
|
|
class SingleWrapper(Wrapper):
|
|
def __init__(self, instance):
|
|
self.instance = instance
|
|
|
|
def get(self):
|
|
return self.instance
|
|
|
|
|
|
class ThreadedWrapper(Wrapper):
|
|
def __init__(self, generator):
|
|
self.generator = generator
|
|
self.local = threading.local()
|
|
|
|
def get(self):
|
|
if not hasattr(self.local, "value"):
|
|
self.local.value = self.generator()
|
|
return self.local.value
|