Skip to content

Commit

Permalink
Change Proxy.py
Browse files Browse the repository at this point in the history
Add some notes.
  • Loading branch information
davidcorne committed Feb 22, 2013
1 parent 5600a16 commit 1d553df
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions Structural/Proxy.py
@@ -1,31 +1,42 @@
#!/usr/bin/env python
# Written by: DGC

#==============================================================================
class Huge(object):

def __init__(self, filename):
self.read_variables(filename)

def read_variables(self, filename):
for i in range(1000000):
name = "var_" + str(i)
# would be read from filename
self.__setattr__(name, i)

def write_variables(self, filename):
print("Would write to " + filename)
# http://sourcemaking.com/design_patterns/proxy
# give 4 good reasons for a proxy to be made.

# A virtual proxy is a placeholder for "expensive to create" objects. The real
# object is only created when a client first requests/accesses the object.
#
# A remote proxy provides a local representative for an object that resides in
# a different address space. This is what the "stub" code in RPC and CORBA
# provides.

# A protective proxy controls access to a sensitive master object. The
# "surrogate" object checks that the caller has the access permissions required
# prior to forwarding the request.

# A smart proxy interposes additional actions when an object is accessed.
# Typical uses include:
# o Counting the number of references to the real object so that it can be
# freed automatically when there are no more references (aka smart pointer)
# o Loading a persistent object into memory when it's first referenced,
# o Checking that the real object is locked before it is accessed to ensure
# that no other object can change it.


#==============================================================================
class HugeProxy(object):
class SharedData(object):

def __init__(self, filename):
self.data = Huge(filename)
def __init__(self):
self.resource = "A resource"

#==============================================================================
if (__name__ == "__main__"):
test_file_vars = Huge("Test File")
test_file_vars = HugeProxy("Test File")
test_file_vars = Huge("Test File")

class AsyncProxy(object):

def __init__(self, data):
"""
Takes some data which should now only be accessed through this class,
otherwise you could get
"""
self.data = data

0 comments on commit 1d553df

Please sign in to comment.