• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best design approach

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a program which contains a log file, and the log file must be accessed across several different classes. In the log file class, I open/create the log file and close it in the finalizer() method. In the class which constitutes the starting point of the program, I create a new instance of the log file. My question though is how best would I implement the accessing of the log file (instance) across the other classes which require it? Would passing the instance to each class' constructor and having a class variable be the best approach, or is there a more feasible way?
Thanks,
SP
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephen - Is this really a threading question? You probably want your main logging class to be a singleton. Flush output to that stream (normally), and provide public methods to set/reset flushing behavior.
HTH, regards, Guy

 
Stephen Pride
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, no threads (yet). Basically I have the following (really scaled down example) ...
<code>
// file 1
public class PHLog {
private FileWriter fw = null;
public PHLog(String filename) {
// open/create file here
}
protected void finalizer() throws Throwable {
if (fw != null) {
fw.close();
}
}
}
// file Main
public class Main {
private PHLog logFile = null;
public static void main(String[] args) {
Main m = new Main();
m.logFile = new PHLog("logfile");
ClassOne c = new ClassOne(m.logFile);
}
}
// file 2, file 3, etc.
public class ClassOne {
private PHLog logFile = null;
public ClassOne(PHLog logFile) {
this.logFile = logFile;
}
}
</code>
Is there a better, more feasible, way to access the one instance of the PHLog instance created in the Main class across all the other classes (i.e., ClassOne, ClassTwo, etc.)? I originally made the instance static in PHLog and referenced statically in each class that required it, but this seemed rather cumbersome (although this other method doesn't seem much better).
Thanks,
SP
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic