aspose file tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Managing internal resource Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "Managing internal resource" Watch "Managing internal resource" New topic
Author

Managing internal resource

Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 310
Hi,
I'm repeatedly running into the following problem (petty, "micro code design" problem). Was wondering whether it has a name, or standard solutions ?

Suppose you have a class that uses some auxiliary resource.
Problem is, class my be used in 2 modes: "simple mode", when it should manage its resources internally. And "complicated mode", when resources would be shard, and thus managed outside of the class.

A very simple example:

Of course, it's a primitive example - the point is that MyDbWriter is using some resources, and has to know whether they're shard on not, in order to decide whether to assume the responsibility of managing them (e.g. in "close()").

I run into this problem especially when trying to write reusable generic components, which programmers would want to use in 2 modes:
A)
Simple mode: programmer has simple requirements and wants a convenient, naiive interface:
MyDbWriter w=new MyDbWriter(); // resources allocated inside constructor
w.writeData("Hello");
w.writeData("World");
w.close();

B)
Complicated mode - programmer has complicated requirements (e.g. resource sharing) and agrees to work harder:
Connection con=... // shared
FileOutputStream logger=... // shared
MyDbWriter w1=new MyDbWriter(con,logger);
MyDbWriter w2=new MyDbWriter(con,logger);
con.close();
logger.close();

Of course, I can easilty solve it *technically*.
MyDbWriter could be an abstract class with 2 implementations.
Or it could have a flag telling it whether resources are shared or not.
But I lack the experience to tell whether these are the only/best solutions ?
The abstract class approach seems a bit cumbersome (double classes to maintain); on the other hand, I've never seen the "flag" approach used, say, in Sun's sources. Either way I get the uncomfortable feeling that whoever maintains my code would find it "non standard"...

Any thoughts would be appreciated - thanks
Gerald Davis
Ranch Hand

Joined: May 15, 2002
Posts: 872
I will help you out when I get home in 5 hours.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791

MyDbWriter could be an abstract class with 2 implementations.
Or it could have a flag telling it whether resources are shared or not.

I like the way you're looking at your options. Another might be to have a single class that delegates to one of two strategies. Or two classes that do not have a common ancestor but use a common helper to do the work. So many choices!

I'd move the "flag" solution to last choice. A mode test in every method would be A Bad Thing.

Think about how different the behavior is in the two modes. Is it posible to give a short description of the object's responsibilities, or do you wind up saying "in this mode it does this, in that mode it does that" over and over? I'm guessing the mode stuff will be pretty significant to the user, and that makes me tend toward two classes. If the method signatures are different, then polymorphism starts to lose its charm. The user can just choose which class he needs. But if you can keep the signatures consistent a factory might be nice to hide the actual implementation classes.

So once again the answer is "it depends". ( That's the answer to all questions for which the answer is not "42". ) Let us know if that brought up any new questions.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Gerald Davis
Ranch Hand

Joined: May 15, 2002
Posts: 872
Stan James basically said it
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 310
Thank you so much for this in-depth reply. It's really been a great help (at least now I don't think I'm the only one having a dilemma over this seemingly-simple question).

Extra thanks for the "it depends /42 " hint. Very useful when people get too inquisitive.

Thanks again.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Glad that helped.

"42" is a gag reference to the Hitchhikers Guide to the Galaxy. It's good among geeks with a certain twisted sense of humor, but not necessarily portable among cultures. Sorry if it just seemed like nonsense. No, wait, H2G2 is nonsense, too. Just a really good sort of nonsense.
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 310
LOL it's fine... I'm also a HHGTTG fan, weird sense of humor and all.
Anyway, "it depends" should be a winner in any culture (any galaxy, for that matter).
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Managing internal resource
 
Similar Threads
Interesting log4j problem.
How do Container Managed Transactions work?
Number of classes/interfaces
problems using prepared statements
My SCEA Part 1Study Notes