 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jednostanicni organizam Guest
|
Posted: Sun May 13, 2007 2:21 am Post subject: Factory methods |
|
|
Is there a way (pattern, ...) to enforce the Class B (example bellow) to
implement some factory method. Something that would have the same effect as
the flawed example bellow.
All classes that implement A should have to know how to instantiate some
special case object of B.
Thanks for the help :-)
public interface A {
// !! Imposible static factory method
// declaration in an interface
public static A someFactoryMethodToImplement();
public void methodToImplement(A a);
}
public class B implements A {
//////
// implements A.someFactoryMethodToImplement()
//////
public static A someFactoryMethodToImplement() {...}
public void methodToImplement(A a) {...}
} |
|
| Back to top |
|
 |
Richard Reynolds Guest
|
Posted: Sun May 13, 2007 3:54 am Post subject: Re: Factory methods |
|
|
"Jednostanicni organizam" <jednostanicniTOCKAorganizam (AT) gmail (DOT) com> wrote in
message news:9v38q7283927.vmycihe4x6at$.dlg (AT) 40tude (DOT) net...
| Quote: |
Is there a way (pattern, ...) to enforce the Class B (example bellow) to
implement some factory method. Something that would have the same effect
as
the flawed example bellow.
All classes that implement A should have to know how to instantiate some
special case object of B.
Thanks for the help :-)
public interface A {
// !! Imposible static factory method
// declaration in an interface
public static A someFactoryMethodToImplement();
public void methodToImplement(A a);
}
public class B implements A {
//////
// implements A.someFactoryMethodToImplement()
//////
public static A someFactoryMethodToImplement() {...}
public void methodToImplement(A a) {...}
}
Does someFactoryMethodToImplement have to be static? Could you make B a |
singleton and use an instance method? |
|
| Back to top |
|
 |
Tom Hawtin Guest
|
Posted: Sun May 13, 2007 7:11 am Post subject: Re: Factory methods |
|
|
Richard Reynolds wrote:
| Quote: | "Jednostanicni organizam" <jednostanicniTOCKAorganizam (AT) gmail (DOT) com> wrote in
message news:9v38q7283927.vmycihe4x6at$.dlg (AT) 40tude (DOT) net...
Is there a way (pattern, ...) to enforce the Class B (example bellow) to
implement some factory method. Something that would have the same effect
as
the flawed example bellow.
Does someFactoryMethodToImplement have to be static? Could you make B a
singleton and use an instance method?
|
Then each derived class would have to have a singleton - back to the
original problem.
AFAICS, (statically) requiring a class to implement a static method
achieves nothing. The only use I could think for this is if reflection
was to be used. The first, second and third rule of using reflection is
don't chuffing well use it.
Tom Hawtin |
|
| Back to top |
|
 |
Jednostanicni organizam Guest
|
Posted: Sun May 13, 2007 1:18 pm Post subject: Re: Factory methods |
|
|
On Sun, 13 May 2007 03:36:47 +0100, Tom Hawtin wrote:
| Quote: | Richard Reynolds wrote:
"Jednostanicni organizam" <jednostanicniTOCKAorganizam (AT) gmail (DOT) com> wrote in
message news:9v38q7283927.vmycihe4x6at$.dlg (AT) 40tude (DOT) net...
Is there a way (pattern, ...) to enforce the Class B (example bellow) to
implement some factory method. Something that would have the same effect
as
the flawed example bellow.
Does someFactoryMethodToImplement have to be static? Could you make B a
singleton and use an instance method?
Then each derived class would have to have a singleton - back to the
original problem.
AFAICS, (statically) requiring a class to implement a static method
achieves nothing. The only use I could think for this is if reflection
was to be used. The first, second and third rule of using reflection is
don't chuffing well use it.
Tom Hawtin
|
The use for that is as follows (maybe it can be achived in some other way):
We have the next:
A interface that every B,X,.. if they want to be A have to implement.
class Box<E extends A> that can contain any element of type B, X, ...
class Box has method getABoxOfSomeNiceA() { .. } so that method would have
to ask B or X what is the nice B or X. And it would do so by calling the
B.someFactoryMethodToImplement().
So if someFactoryMethodToImplement() is not enforced by A to be implemented
by subclasses, Box will not be able to return a result of
getABoxOfSomeNiceA() because maybe that implementation of A ,it is using,
doesn't have that method implemented. |
|
| Back to top |
|
 |
Jednostanicni organizam Guest
|
Posted: Sun May 13, 2007 3:33 pm Post subject: Re: Factory methods |
|
|
On Sun, 13 May 2007 10:18:16 +0200, Jednostanicni organizam wrote:
| Quote: | On Sun, 13 May 2007 03:36:47 +0100, Tom Hawtin wrote:
Richard Reynolds wrote:
"Jednostanicni organizam" <jednostanicniTOCKAorganizam (AT) gmail (DOT) com> wrote in
message news:9v38q7283927.vmycihe4x6at$.dlg (AT) 40tude (DOT) net...
Is there a way (pattern, ...) to enforce the Class B (example bellow) to
implement some factory method. Something that would have the same effect
as
the flawed example bellow.
Does someFactoryMethodToImplement have to be static? Could you make B a
singleton and use an instance method?
Then each derived class would have to have a singleton - back to the
original problem.
AFAICS, (statically) requiring a class to implement a static method
achieves nothing. The only use I could think for this is if reflection
was to be used. The first, second and third rule of using reflection is
don't chuffing well use it.
Tom Hawtin
The use for that is as follows (maybe it can be achived in some other way):
We have the next:
A interface that every B,X,.. if they want to be A have to implement.
class Box<E extends A> that can contain any element of type B, X, ...
class Box has method getABoxOfSomeNiceA() { .. } so that method would have
to ask B or X what is the nice B or X. And it would do so by calling the
B.someFactoryMethodToImplement().
So if someFactoryMethodToImplement() is not enforced by A to be implemented
by subclasses, Box will not be able to return a result of
getABoxOfSomeNiceA() because maybe that implementation of A ,it is using,
doesn't have that method implemented.
|
The only way to do it, as it seems is to give up on A being an interface
and to make it an abstract class. I don't like it but here is the idea.
public abstract class A {
public enum Special { NICE, NOT_NICE; }
protected A() {}
public A(A.Special special) {}
public abstract void methodToImplement(A a);
}
public class B extends A {
public B (A.Special special) {
if(special == Special.NICE) {
// make nice B
} else if (special == Special.NOT_NICE){
// make not nice B
} else {
throw new IllegalArgumentException();
}
}
public void methodToImplement(A a) { ... }
} |
|
| Back to top |
|
 |
Tom Hawtin Guest
|
Posted: Sun May 13, 2007 8:26 pm Post subject: Re: Factory methods |
|
|
Jednostanicni organizam wrote:
| Quote: |
class Box<E extends A> that can contain any element of type B, X, ...
class Box has method getABoxOfSomeNiceA() { .. } so that method would have
to ask B or X what is the nice B or X. And it would do so by calling the
B.someFactoryMethodToImplement().
|
I'm having some trouble following what you are attempting to do.
It seems like you want an abstract factory.
So we have your base product interface and implementations:
public interface Base {
...
}
public class Derived1 implements Base {
...
}
public class Derived2 implements Base {
...
}
....
You then need a factory interface for the base product type, and
implementations for each implementation:
public interface BaseFactory<E extends Base> {
E create();
}
public final class Derived1Factory extends BaseFactory<Derived1> {
public static final BaseFactory<Derived1> INSTANCE =
new Derived1Factory();
private Derived1Factory() {
}
public Derived1 create() {
return new Derived1();
}
}
....
Box is then constructed with a particular factory with which it can make
elements with:
public class Box<E extends A> {
private final BaseFactory<E> factory;
private final List<E> contents = new java.util.ArrayList<E>();
public Box(BaseFactory<E> factory) {
this.factory = factory;
}
public void grow() {
contents.add(factory.create());
}
...
}
Tom Hawtin |
|
| Back to top |
|
 |
Richard Reynolds Guest
|
Posted: Sun May 13, 2007 9:12 pm Post subject: Re: Factory methods |
|
|
"Tom Hawtin" <usenet (AT) tackline (DOT) plus.com> wrote in message
news:46467924$0$8754$ed2619ec@ptn-nntp-reader02.plus.net...
| Quote: | Richard Reynolds wrote:
"Jednostanicni organizam" <jednostanicniTOCKAorganizam (AT) gmail (DOT) com> wrote
in message news:9v38q7283927.vmycihe4x6at$.dlg (AT) 40tude (DOT) net...
Is there a way (pattern, ...) to enforce the Class B (example bellow) to
implement some factory method. Something that would have the same effect
as
the flawed example bellow.
Does someFactoryMethodToImplement have to be static? Could you make B a
singleton and use an instance method?
Then each derived class would have to have a singleton - back to the
original problem.
AFAICS, (statically) requiring a class to implement a static method
achieves nothing. The only use I could think for this is if reflection was
to be used. The first, second and third rule of using reflection is don't
chuffing well use it.
Tom Hawtin
|
hee hee, "chuffing", love that word! |
|
| Back to top |
|
 |
Lew Guest
|
Posted: Tue May 15, 2007 12:26 am Post subject: Re: Factory methods |
|
|
Jednostanicni organizam wrote:
| Quote: | Thanks It seems that separate factory objects are the only solution to
the problem, as you sugested.
Wouldn't it be much easier and more elegant if abstract classes could have
abstract constructors that subclasses wold have to implement. That would
end the need for factory objects .. just a thought
|
It would not end the preference for factory objects. Factories are useful to
support polymorphism and other Good Things.
--
Lew |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|