File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Design Help required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design Help required" Watch "Design Help required" New topic
Author

Design Help required

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Hi all,

I have a Factory class which creates instances of Resource interface.

The Resource class has init and destroy method.

The requirement is invoke the destroy method when every one (client)is done using the Resource object.


I am not able to come up with a good solution.
This is how I am planning to implement
(1). Modify the getResource () as


This will create the Resource object (if not already there) and add the client object to a list, which tracks who are using the Resource object.


(2). Add a destroyResource()in the Factory class



This method will remove client from the list (mentioned in (1)) and if the list size is zero (=> no clients are using Resource), invoke destroy on
Resource object.

I am not convinced that the above implementation is a good one. Some one has a better idea?

Thanks a lot.

Pradeep
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
What you've described is more or less the only way to destroy the Resource object "immediately" when it is no longer needed.

If the clients won't tell you when they're done using the resource, your only other option I know of is to use weak references inside the factory to hold on to the Resource object it is handing out to clients. When client-1 requests the Resource, the factory creates the Resource object, stores it into an internal Map using a java.lang.ref.WeakReference, and returns the Resource object to client-1. Then, client-2 comes and requests for the same Resource so the factory picks up the WeakReference from the Map, asks for the object from the WeakReference, and return the object to the client. And so forth. What the WeakReference accomplished for you is that the Resource object is available for garbage collection as soon as any clients have released their references to it -- the factory's weak reference doesn't prevent the Resource object from being GC'd.

Obviously the garbage collection isn't immediate so this might not be a viable solution to your problem.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Thanks Lasse for a quick reply.


Fortunately clients will inform the factory when they are done using the Resource.

In case they are not able to do it due to some reason, the WeakRefernece solution does not make it possible to call destroy method, right?

:roll:

I am asking this because, the init method of the Resource creates a Timer and schedules a thread to run every 10 minutes. The destroy() does a timer.cancel(). If the Resource object is GC'd by using WR solution does the timer get canceled as well.(TimerTask implemenation class is an inner class of Resource) I am not sure it will gets canceled. It may run forever. Please let me know If I am wrong here.

Thanks again for your help.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Pradeep Bhat:
In case they are not able to do it due to some reason, the WeakRefernece solution does not make it possible to call destroy method, right?

You could make Resource implement finalize() and invoke the destroy() method from there. The only problem should be that you really have no idea whether the finalize() method is called 5 seconds or 5 minutes after all the references to the Resource object have been released.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Thanks Lasse
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design Help required
 
Similar Threads
Behaviour HashMap
Records in Arraylist
Factory Method/Abstract Factory
Locking scheme
Abstract Factory vs Factory pattern