Attach additional responsibilities to an object dynamically. Proxys provide a flexible alternative to subclassing for extending functionality.
PerfectJPattern's offers a componentized version of the Proxy pattern. In order to implement
proxies and take advantage of PerfectJPattern's implementation, users are required to extend
AbstractProxy
and optionally override the invokeUnderlying
method. The invokeUnderlying
will intercept all method calls on the real Subject instance.
See example below or checkout the SynchronizedProxy.java
implementation that will protect any Subject type from
race-conditions. This implementation would replace the need for e.g.
Collections.synchronizedCollection(...)
, Collections.synchronizedSet(...)
,
Collections.synchronizedList(...)
, etc. See example under
TestAsynchronousSubject.java
Implementing proxies using PerfectJPattern's AbstractProxy
offers the following
advantages:
equals
is automatically handled to match that of the
Subject, thus to the outside world the Proxy still is the Subject assuming equals
defines identity