Warto zauważyć, że leniwe inicjowanie może być też używane w nieobiektowych językach programowania.
Ruby Poniżej jest przykład napisany w języku Ruby:
require 'net/http' class Blogger def auth_token @auth_token ||= get_token_from_http_response(Net::HTTP.post_form(uri, params)) end
# get_token_from_http_response, uri and params are defined later in the class end
b = Blogger.new b.instance_variable_get(:@auth_token) # returns nil b.auth_token # returns token b.instance_variable_get(:@auth_token) # returns token
Zobacz też singleton blokada z podwójnym zatwierdzaniem
Linki zewnętrzne Article "Java Tip 67: Lazy instantiation – Balancing performance and resource usage" by Philip Bishop and Nigel Warren Java code examples Use Lazy Initialization to Conserve Resources Description from the Portland Pattern Repository
Lazy Inheritance in JavaScript
Kategoria:Wzorce projektowe