Created
March 2, 2011 17:40
-
-
Save gennad/851341 to your computer and use it in GitHub Desktop.
Lazy initialization - GoF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LazyCalc: | |
def __init__(self): | |
self.dict = {} | |
def calc(self, n): | |
if n not in self.dict: | |
def recursion(n): | |
if n <= 1: | |
return 1 | |
else: | |
return n * recursion(n - 1) | |
res = recursion(n) | |
self.dict[n] = res | |
return self.dict[n] | |
class Main(): | |
def __init__(self): | |
self.lazy_calc = None | |
def get_lazy_calc(self): | |
if not self.lazy_calc: | |
self.lazy_fact = LazyCalc() | |
return self.lazy_fact | |
main = Main() | |
lazy_calc = main.get_lazy_calc() | |
print lazy_calc.calc(5) | |
print lazy_calc.calc(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment