commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Benson <gudnabr...@gmail.com>
Subject [flyweight] WAS [proxy] 2.0 WAS Re: a bit of commons-style code in search of a home
Date Tue, 20 Jul 2010 18:36:04 GMT

On Jul 20, 2010, at 1:01 PM, James Carman wrote:
[SNIP]
>  I have also been thinking a bit about
> how to best create proxies to things that have to be looked up (such
> as Spring beans) when they're deserialized.  Basically, you need to
> use the writeReplace()/readResolve() mechanism so that you don't write
> out actual references to the Spring bean.  That gave me another idea,
> commons-flyweight.  Basically, a flyweight would be a stand-in for a
> more heavyweight object and it can be used to retrieve it (or
> readResolve() it).  Here's some code I threw together really quick:
> 
> public class Flyweight<T> implements Serializable
> {
> //**********************************************************************************************************************
> // Fields
> //**********************************************************************************************************************
> 
>    private static Map<String, Object> flyweights = new
> TreeMap<String, Object>();
>    private final String id;
> 
> //**********************************************************************************************************************
> // Static Methods
> //**********************************************************************************************************************
> 
>    public static <T> Flyweight<T> register(T object)
>    {
>        final String id = UUID.randomUUID().toString();
>        flyweights.put(id, object);
>        return new Flyweight<T>(id);
>    }
> 
> //**********************************************************************************************************************
> // Constructors
> //**********************************************************************************************************************
> 
>    private Flyweight(String id)
>    {
>        this.id = id;
>    }
> 
> //**********************************************************************************************************************
> // Other Methods
> //**********************************************************************************************************************
> 
>    protected T readResolve()
>    {
>        return getObject();
>    }
> 
>    public T getObject()
>    {
>        return (T) flyweights.get(id);
>    }
> }
> 
> I haven't thought through the memory "leaks" and stuff yet.  This is
> just a rough idea.  Maybe this sort of thing belongs in [lang] rather
> as its own module.
> 

I know what you mean; it's potentially not that much code.  Maybe start in the sandbox and,
once everything's working satisfactorily, decide what to do with it?  As for memory, what
if you actually put the map of flyweight id:instance onto a Flyweights class and managed *its*
id mappings statically/weakly?  Then a given Flyweight would just become expired whenever
its owning Flyweights instance () was no longer accessible, and its getObject could reflect
that somehow.  Typically it would be a sign of a programming error if a Flyweight associated
with a given container were used after its container had been reclaimed, so a simple exception
might be enough.  Just ruminating...

-Matt

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message