Re: My LogGenerator.java won't compile - too many errors - help



See below, thanx

James Westby wrote:
phillip.s.powell@xxxxxxxxx wrote:
James Westby wrote:

phillip.s.powell@xxxxxxxxx wrote:

BTW, what's a factory?

Phil


I meant static factory method, it's what you implemented to replace the
constructor that you made private.



I don't know what a static factory anything is. You lost me.

Phil


James


A static factory method is a normal method (like toString(), getName())
that is used to create instances of a class. Normally instances are
created by a constructor. However there are cases, e.g. the singleton
pattern, where you, the designer of the class, want to restrict object
creation, or you want more complicated construction code than can be
implemented in a constructor. The usual way to acheive this is by making
the constructor private or protected, so that only a certain group of
people can create instances of your class. Then the problem is how to
let instances be accessed, this is where the factory method comes in.

Consider the singleton pattern. You have a class named Singleton that
you want to implement this pattern. Normal creation with a constructor
would be

new Singleton();

however you don't want this as anyone can create instances, and so you
can't guarantee that there is only one. Instead you make the constructor
private, so the above code will not compile. Instead you provide a
method called getInstance(), which returns the one and only singleton.
This is known as a factory method.

This method cannot be called on an instance of the Singleton class, as
an instance can only be got by calling it, leading to a circular
dependency. Instead the method is made static, so you call it like

Singleton.getInstance();

This is where the static part of the name comes from.

Put all of this together and you get the typical signature of the method as

public static Singleton getInstance();

The idea can be extended to non-singleton classes, but that is the basic
idea of a static factory method.


James

I designed a class, NicknameBin, that supposedly is just that (see
http://www.myjavaserver.com/~ppowell/docs/NicknameBin.java ) , however,
a review is always helpful.

I decided to trash the idea of a Singleton LogGenerator because the
ultimate goal is to write to a single log file, and a Singleton Design
Pattern is unnecessary (not to mention impossible for me to write!)
here, multiple instances can exist to write to a single file as long as
the instances employ a synchronized method to do so (which I'm probably
wrong again, but hey why not!)

Here is the redesigned LogGenerator and it compiles nicely!:

[code]
package ppowell;

import java.io.*;
import java.util.*;

/**
* Public implementation for LogGenerator to do logging
*
* @version JSDK 1.4
* @author Phil Powell
* @package PPOWELL
*/
public class LogGenerator extends File implements Loggable,
Serializable {

public static final String TIME_PLACEHOLDER = "#time#", IP_PLACEHOLDER
= "#IP#";
public static final String UNIX_LOG_FORMAT = "[#time#]<#IP#> - - ";

String logFileName = "";

/**
* Constructor
*
* @access public
*/
public LogGenerator(String logFileName) {
super(logFileName);
this.logFileName = logFileName;
}

//------------------------------------------- --* GETTER/SETTER
METHODS *-- ----------------------------------------------

public String getLogFileName() {
return this.getName();
}

public void setLogFileName() {} // YOU DON'T NEED THIS METHOD BUT IT'S
IMPLEMENTED SO YOU MUST WRITE IT
//------------------------------------- --* END OF GETTER/SETTER
METHODS *-- ----------------------------------------------


public synchronized void deleteLog() throws FileNotFoundException,
IOException {
this.delete();
}

/**
* Legacy
*
* @ignore
*/
public void doLog() throws IOException {}

/**
* Write log with message
*
* @access public
* @param String msg
* @uses FileWriter
* @uses BufferedWriter
*/
public synchronized void doLog(String msg) throws
FileNotFoundException, IOException {
BufferedWriter writer = new BufferedWriter(new
FileWriter((File)this));
writer.write(msg);
writer.flush();
writer.close();
}

}
[/code]

.



Relevant Pages

  • Re: Observer Design Pattern
    ... require 'singleton' ... singletons would have a static factory method ") to make themselves useful. ... how does the factory method decide which constructor to invoke? ... Will the include statement make the constructors become private if they were declared public? ...
    (comp.object)
  • Re: My LogGenerator.java wont compile - too many errors - help
    ... A static factory method is a normal method, getName) that is used to create instances of a class. ... Normally instances are created by a constructor. ... Consider the singleton pattern. ...
    (comp.lang.java.help)
  • Re: Singletons
    ... A singleton would usually have a private constructor to prevent ... but you're then breaking one of the rules of the singleton pattern. ... public function __construct ...
    (php.general)
  • Re: Singletons
    ... A singleton would usually have a private constructor to prevent ... but you're then breaking one of the rules of the singleton pattern. ... public function __construct ...
    (php.general)
  • Re: Could an object refuse to be created!?
    ... that you might instead use a factory method. ... also capable of returning null rather than throwing an exception. ... > Hi Kevin, ... throw an Exception in the object's constructor. ...
    (microsoft.public.dotnet.languages.csharp)