Changes

Jump to: navigation, search

Proxy

1,873 bytes added, 16:48, 22 March 2007
no edit summary
The <i>Proxy</i> pattern is used in software development to create a placeholder for an object. The Proxies provide an interface to another object is not actually created until the information that the such as a network connection, a large object holds is required. This extra layer of abstraction saves time when in memory, a program must access a database file, or a disk for the information.<br /><br />If the information some other resource that is never required, the database/disk will never be queried and the system will run more efficiently with less slowdowns for unnecessary materializationsexpensive or impossible to duplicate.
<br />
<br />
==Proxy Design Pattern==
When a request is made "<i>Any operations performed on the proxies are forwarded to a Proxy, the real original object is then instantiated. From then onOnce all instances of the proxy are out of scope, any further requests are made to the real complex object's memory may be deallocated.</i>" - <b>Proxy Pattern, Wikipedia. org</b><br />There are four common situations where a Proxy pattern is required:
<br />
<br />
[[Image:ProxyUML.gif|UML Diagram for the Proxy Design Pattern]]
<br />
Source: Downloaded from http://www.developer.com/design/article.php/3309461
<br />
<br />
<br />
[[Image:Proxy.gif|A Complete UML Diagram for the Proxy Design Pattern]]
<br />
Source: Downloaded from http://www.dofactory.com/Patterns/PatternProxy.aspx
<br />
<br />
<br />
[[Image:ProxyDCD.jpg|Design Class Diagram for the Proxy Design Pattern]]
<br />
Source: Downloaded from http://home.earthlink.net/~huston2/dp/all_uml.html
<br />
<br />
===Virtual Proxy - Python===
This Sample code is from a Python Remote Method Invocation program. It is copyrighted by <b>Irmen de Jong</b> and licensed by <b>[http://www.google.com/codesearch?hl=en&q=show:dgvZt53GNuE:9VmFCU8eSwo:nSqKvMM2F_M&sa=N&cd=13&ct=rl&cs_p=http://gentoo.osuosl.org/distfiles/Pyro-3.4.tar.gz&cs_f=Pyro-3.4/Pyro/EventService/Clients.py MIT]</b>. The code in this example is located in the <i>Clients.py</i> file of the <b>Pyro</b> system. It allows a user to write a module containing a class to be accessed remotely. The class the user created is registered by the Pyro Name Server. The client creates proxies for the remote objects to be invoked when a call is made to the object. The Virtual Proxy pattern can be seen throughout this code file as highlighted:
<br />
<pre>
daemon.connect(self) # will also set self.daemon...
uri = self.NS.resolve(Pyro.constants.EVENTSERVER_NAME)
# creates a proxy for the remote class to be called on later to access the
# remote object and saves making an expensive call to a DB/disk if it isn't
# required.
self.eventservice=Pyro.core.getProxyForURI(uri)
self.eventservice._setIdentification(ident)
# Subscribe to one or more subjects.
# It is safe to call this multiple times.
# gets a proxy for the service so that an expensive call isn't require until
# the actual service is needed.
self.eventservice.subscribe(subjects, self.getProxy())
def subscribeMatch(self,subjectPatterns):
# Subscribe to one or more subjects (by pattern)
# It is safe to call this multiple times.
# gets a proxy for the service so that an expensive call isn't require until
# the actual service is needed.
self.eventservice.subscribeMatch(subjectPatterns, self.getProxy())
def unsubscribe(self, subjects):
# Unsubscribe the subscriber for the given subject(s).
# gets a proxy for the service so that an expensive call isn't require until
# the actual service is needed.
self.eventservice.unsubscribe(subjects, self.getProxy())
ns = locator.getNS(host=Pyro.config.PYRO_NS_HOSTNAME)
uri = ns.resolve(Pyro.constants.EVENTSERVER_NAME)
# creates a proxy for the remote class to be called on later to access the
# remote object and saves making an expensive call to a DB/disk if it isn't
# required.
self.eventservice=Pyro.core.getProxyForURI(uri)
self.eventservice._setIdentification(ident)
</pre>
<br />
 
===Virtual Proxy - Java===
This sample code is from the Apache Geronimo server runtime framework written in J2EE. It is copyrighted by <b>IBM</b> and licensed by <b>[http://www.google.com/codesearch?hl=en&q=show:x-Vlj2phf84:hfeVps_3TKA:KxOJN1EfkwE&sa=N&ct=rd&cs_p=http://archive.apache.org/dist/geronimo/1.0-M5/geronimo-1.0-M5-src.tar.gz&cs_f=geronimo-1.0-M5/LICENSE.txt Apache]</b>. The code in this example is located in the <i>GBeanCollectionReference.java</i> of the <b>Geronimo</b> framework. It allows a user to create their own J2EE 1.4 certified application server. It can deploy servlets, XML, and other web applications. The Virtual Proxy pattern can be seen throughout this code file as highlighted:
<br />
<pre>
if (!getPatterns().isEmpty() && getProxy() == null) {
// add a dependency on our target and create the proxy
/* sets a collection of proxies for each object instead of making an expensive
call to a DB/disk to retrieve all of the object information when it isn't
required. */
setProxy(new ProxyCollection(getName(), getReferenceType(),
getKernel().getProxyManager(), getTargets()));
}
/* Destroys all of the proxies for the objects as the system is deconstructing. */
public synchronized void stop() {
ProxyCollection proxy = (ProxyCollection) getProxy();
}
/* Adds a target object to the proxy to be instantiated if or when it is
required. */
protected synchronized void targetAdded(ObjectName target) {
ProxyCollection proxy = (ProxyCollection) getProxy();
}
/* Removes a target object from the proxy to to open it's use up to another
object. */
protected synchronized void targetRemoved(ObjectName target) {
ProxyCollection proxy = (ProxyCollection) getProxy();
1
edit

Navigation menu