The Artima Developer Community
Sponsored Link

Java Answers Forum
impossible to create a Factory Method in Java?

5 replies on 1 page. Most recent reply: Nov 6, 2002 12:17 PM by Anil Philip

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

impossible to create a Factory Method in Java? Posted: Nov 4, 2002 12:48 PM
Reply to this message Reply
Advertisement
I would like to create a factory method as described in Design Patterns [Gang Of Four 95] page 107. This seems to be impossible to implement in Java?

I created an interface that has a static method eg.

public interface Task {
public static Task create();
public void doWork();
}

of course, the compiler complains. So I try to convert it into an abstract class - no luck again :(

But I really do need to have the interface so that concrete subclasses are forced to implement the factory method and also doWork().
And I really do need to have the method as static so that
it can function as a factory method.

Any way out?


Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

Re: impossible to create a Factory Method in Java? Posted: Nov 4, 2002 12:54 PM
Reply to this message Reply
Of course, another reason to keep the interface is - the instantiator must be ignorant of the concrete subclass.

Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

Re: impossible to create a Factory Method in Java? Posted: Nov 4, 2002 2:02 PM
Reply to this message Reply
Sorry, I intended to mean the factory method paradigm in C++ where
Task t = Task::create();

and create() can return ConcreteTask.

I am wondering if the solution is to call clone()
eg.
interface Task {
public void doWork();
}

assuming that tt is an instance of LogTask that implements Task,
Task t = tt.clone();

but then I wonder if t.doWork() will run Task.doWork() or LogTask.doWork()?
Also the original intent of a factory method is lost.
thanks,
Anil.

Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

one solution for Factory Method problem Posted: Nov 5, 2002 11:17 AM
Reply to this message Reply
/*
* Task.java
*
* Created on November 5, 2002, 12:52 PM
*/

/**
*
* @author aphili01
*/
public abstract class Task {

protected abstract Object clone() throws java.lang.CloneNotSupportedException;

public abstract void doWork();

public Task makeCopy() throws java.lang.CloneNotSupportedException { return (Task) clone();};

}
**
*
* @author aphili01
*/
public class Task1 extends Task {

/** Creates a new instance of Task1 */
public Task1() {
}
protected Object clone() throws java.lang.CloneNotSupportedException {
return new Task1();
}

public void doWork() {
System.out.println("Task1.doWork()");
}

public static void main(String[] args) throws java.lang.CloneNotSupportedException {
Task1 t = new Task1();
Task tt = t.makeCopy();
tt.doWork();
}
}

Joe Cheng

Posts: 65
Nickname: jcheng
Registered: Oct, 2002

Re: impossible to create a Factory Method in Java? Posted: Nov 6, 2002 9:38 AM
Reply to this message Reply
Hi Anil,

I don't think the factory method is supposed to be implemented as a static. Then you lose the whole advantage of the factory method pattern, i.e., let polymorphism choose the correct implementation of the factory method. Polymorphism doesn't work with static methods.

What you want instead is an instance method on some other object, for example, if you have some notion of a TaskManager. If there isn't a natural place for that instance method to live, then, well... are you sure you want a Factory Method? Maybe you just want a Factory.

-jmc

Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

Re: impossible to create a Factory Method in Java? Posted: Nov 6, 2002 12:17 PM
Reply to this message Reply
> Hi Anil,
>
> I don't think the factory method is supposed to be
> implemented as a static. Then you lose the whole
> advantage of the factory method pattern, i.e., let
> polymorphism choose the correct implementation of the
> factory method. Polymorphism doesn't work with static
> methods.
>
> What you want instead is an instance method on some other
> object, for example, if you have some notion of a
> TaskManager. If there isn't a natural place for that
> instance method to live, then, well... are you sure you
> want a Factory Method? Maybe you just want a Factory.
>
> -jmc

Yes, I have a TaskManager server that accepts requests via socket and performs tasks (rather like a simple webserver). However I require that new kinds of tasks "new services" be painlessly added to the task server (like in the Interceptor pattern). They just need to register themselves for callback, and to implement a task interface. Task Manager should be able to invoke doWork() on a task and if there are fewer task objects than requests, to clone a task for the pool.

Flat View: This topic has 5 replies on 1 page
Topic: error code - 1073741819 Previous Topic   Next Topic Topic: Boolean

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use