|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.proxy.SequenceProxyFactory
public class SequenceProxyFactory
A sequence proxy factory creates various dynamic proxies for
any type of sequence
.
The proxies are all instances of the
java.lang.reflect.Proxy
class. Each created proxy can be cast into any interface implemented
by the proxied sequence.
Six different kinds of dynamic sequence proxies can be created by this factory:
getLoggableSequence(Sequence)
: returns a sequence proxy
that will record and time all access to the proxied sequence
if and only if all access is through the proxy only.
getSynchronisedSequence(Sequence)
: returns
a sequence proxy that guarantees thread-safe access to the proxied
sequence if and only if all access is through the proxy only.
The lock used synchronise on is the proxied sequence.
getNonResettableSequence(Sequence)
: returns a sequence
proxy that disallows the proxied sequence to be reset
if and only if all access is through the proxy only.
getImmutableSequence(Sequence)
: returns a sequence
proxy that disallows the proxied sequence to be reset
, advanced
, or copied
if and only if all access is through the proxy only.
Copying will simply return the proxy instance, not the proxied
object.
getVirtualSequence(InstantiableTypeLiteral, Object...)
:
returns a virtual sequence proxy that will create the
actual sequence to use once the first method declared in
the specific Sequence
type or a super-interface is invoked.
getSharedSequence(Sequence)
: returns a smart pointer
proxy that will allow a given sequence to be shared among copies of
it until a given copy invokes either Sequence.next()
or
Sequence.reset()
, which will detach the copy from the
reference and copy the sequence used internally.
SynchronisedSequence
,
NonResettableSequence
, and ImmutableSequence
, respectively,
but the returned dynamic proxies can still be cast into any interface
implemented by the proxied sequence. It is perfectly legal to create sequence proxies based on other sequence proxies.
Implementation notes:
The factory defers the actual proxy creation to the
ProxyFactory
class, which allows creation of proxies
based on any type. The ProxyFactory
could have been
inherited, but composition is chosen instead to keep the
interface of this class clean and focused on sequences.
Each method used to create a sequence proxy defined in this class is an application of the Factory Method pattern.
ProxyFactory
Field Summary | |
---|---|
private Method |
copy
The copy() method defined in the
Sequence interface. |
(package private) static ProxyFactory |
factory
The singleton proxy factory used to create the proxy objects representing sequences . |
private Method |
next
The next() method defined in the
Sequence interface. |
private Method |
reset
The reset() method defined in the
Sequence interface. |
Constructor Summary | |
---|---|
SequenceProxyFactory()
No-arg constructor. |
Method Summary | ||
---|---|---|
|
getImmutableSequence(Sequence<E> sequence)
Returns a sequence proxy implementing all interfaces sequence
implements that will disallow Sequence.reset() ,
Sequence.next() , and Sequence.copy() to alter
sequence if and only if all access is though the
returned proxy. |
|
|
getLoggableSequence(Sequence<E> sequence)
Returns a sequence proxy implementing all interfaces sequence
implements that will log all access to sequence
if and only if all access is though the returned proxy. |
|
|
getNonResettableSequence(Sequence<E> sequence)
Returns a sequence proxy implementing all interfaces sequence
implements that will disallow Sequence.reset() to be invoked
for sequence if and only if all access is though the
returned proxy. |
|
|
getSharedSequence(Sequence<E> sequence)
Creates a smart pointer based on the sequence
supplied as sequence . |
|
|
getSynchronisedSequence(Sequence<E> sequence)
Returns a sequence proxy implementing all interfaces sequence
implements that will ensure synchronised access to sequence
if and only if all access is though the returned proxy. |
|
|
getVirtualSequence(InstantiableTypeLiteral<T> type,
Object... arguments)
Creates a virtual sequence based on the sequence
type T . |
|
boolean |
isProxied(Sequence<?> sequence)
Returns true if sequence has at least one created
proxy sequence associated with it, false if not. |
|
boolean |
isProxy(Sequence<?> sequence)
Returns true if sequence is a proxy sequence created
by a sequence proxy factory, false if not. |
|
String |
toString()
Returns the string representation of this factory. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private final Method copy
copy()
method defined in the
Sequence
interface. Never null.
static final ProxyFactory factory
sequences
. Never null.
private final Method next
next()
method defined in the
Sequence
interface. Never null.
private final Method reset
reset()
method defined in the
Sequence
interface. Never null.
Constructor Detail |
---|
public SequenceProxyFactory()
Method Detail |
---|
@ParticipantUsage(value="Proxy", type=Sequence.class) public <E> Sequence<E> getImmutableSequence(@Participant(value="RealSubject") Sequence<E> sequence)
sequence
implements that will disallow Sequence.reset()
,
Sequence.next()
, and Sequence.copy()
to alter
sequence
if and only if all access is though the
returned proxy.
The copy()
method simply returns the proxy instance, not
the proxied object.
The returned proxy has similar functionality to the
ImmutableSequence
class, but the returned proxy can still be
cast into any interface implemented by sequence
, though
not the actual sequence type (class) itself. Trying to cast
the returned proxy into the actual type (class) of sequence
will cause a class cast exception to be thrown!
E
- The type of values delivered by sequence
.sequence
- The sequence to proxy; cannot be null.
ImmutableSequence
@ParticipantUsage(value="Proxy", type=Sequence.class) public <E> Sequence<E> getLoggableSequence(@Participant(value="RealSubject") Sequence<E> sequence)
sequence
implements that will log all access to sequence
if and only if all access is though the returned proxy.
The returned proxy can be cast into any interface implemented
by sequence
, but not the actual sequence type
(class) itself. Trying to cast the returned proxy into the actual
type (class) of sequence
will cause a class cast exception
to be thrown!
E
- The type of values delivered by sequence
.sequence
- The sequence to proxy; cannot be null.
@ParticipantUsage(value="Proxy", type=Sequence.class) public <E> Sequence<E> getNonResettableSequence(@Participant(value="RealSubject") Sequence<E> sequence)
sequence
implements that will disallow Sequence.reset()
to be invoked
for sequence
if and only if all access is though the
returned proxy.
The returned proxy has similar functionality to the
NonResettableSequence
class, but the returned proxy can still be
cast into any interface implemented by sequence
, though
not the actual sequence type (class) itself. Trying to cast
the returned proxy into the actual type (class) of sequence
will cause a class cast exception to be thrown!
E
- The type of values delivered by sequence
.sequence
- The sequence to proxy; cannot be null.
NonResettableSequence
public <E> Sequence<E> getSharedSequence(@Participant(value="RealSubject") Sequence<E> sequence)
sequence
supplied as sequence
.
Call the returned proxy X, using sequence
as its internal
sequence, which may be shared. Hence, sequence
will be
shared among all copies
of X (inclusive), say
X and Y, until a given copy, say Y, invokes either
next()
or reset()
. This
will copy sequence
and detach Y. Y will henceforth use the copy
of sequence
just made, and Y can be shared (copied) as well, say
into Z, which will use the same internal sequence as Y, and so forth.
The returned proxy will not be registered
to this factory.
E
- The type of sequence values delivered by sequence
.sequence
- The sequence to share; cannot be null.
NullPointerException
- If sequence
is null.@ParticipantUsage(value="Proxy", type=Sequence.class) public <E> Sequence<E> getSynchronisedSequence(@Participant(value="RealSubject") Sequence<E> sequence)
sequence
implements that will ensure synchronised access to sequence
if and only if all access is though the returned proxy.
The returned proxy has similar functionality to the
SynchronisedSequence
class, but the returned proxy can still be
cast into any interface implemented by sequence
, though
not the actual sequence type (class) itself. Trying to cast
the returned proxy into the actual type (class) of sequence
will cause a class cast exception to be thrown!
E
- The type of values delivered by sequence
.sequence
- The sequence to proxy; cannot be null.
SynchronisedSequence
@ParticipantUsage(value="Proxy", type=Sequence.class) public <E,T extends Sequence<E>> T getVirtualSequence(@Participant(value="RealSubject") InstantiableTypeLiteral<T> type, Object... arguments)
sequence
type T
.
Once the first method declared in T
, or in
a super-type of T
, is invoked on the returned proxy,
the proxy creates the actual proxied sequence that will be used
as the subject hence forth.
The returned proxy is not registered to this factory as the
proxied object is not yet created. Hence, any call to
isProxy(Sequence)
with the returned proxy as the
argument will return false!
The returned proxy can be cast into any super-interface of
T
, or T
itself if T
is not a class.
E
- The type of sequence values delivered by the sequence.T
- The actual type of sequence to create a virtual proxy for.type
- The type literal representing the actual type; cannot be null.arguments
- The arguments used to construct the proxied sequence.
NullPointerException
- If type
is null.public boolean isProxied(Sequence<?> sequence)
sequence
has at least one created
proxy sequence associated with it, false if not.
sequence
may be a proxy itself.
sequence
- The sequence to test; cannot be null.
sequence
has at least one associated
proxy, false if not.
NullPointerException
- If sequence
is null.isProxy(Sequence)
public boolean isProxy(Sequence<?> sequence)
sequence
is a proxy sequence created
by a sequence proxy factory, false if not.
sequence
- The sequence to test; cannot be null.
sequence
is a proxy sequence, false if not.
NullPointerException
- If sequence
is null.isProxied(Sequence)
public String toString()
toString
in class Object
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |