Oliver said:
Alternatively, you could pass a factory to GenericDemo, and the factory
could create your objects for you. But then, what is it exactly that
GenericDemo does? If all it does is creates objects, then GenericDemo itself
should be made into a factory.
Maybe we are talking across each other... basically I have some class
that as a result of a rather costly, O(n^2), calculation will create n
or fewer elements of some type... Since almost all the logic (except
the user interface) for this is in the base class ("Foo") and I just
want to create decorators for different UI standards (i.e. toString()
when called from a CmdLineFoo spits out a user readable version but
from anything else just returns this.getClass()@this.hashCode()
[default Object method])
Some details on the application might make it a little clearer:
Assume I have some class that prepares a list of "chunks" of difference
between two files (simelor to diff(1) but using a slightly improved LCS
algorithm)... The super class Chunk knows everything there is know
about chunks (i.e. there size, where they are in the file(s), and what
edits they require)....
Now I want to make two different (maybe more in the future) UI's for
this app
one is a command line version that will produce output identical to
diff(1) (I want to make it so this can be done as a call to some method
in the diff class [version control and a few other apps would do it
this way]).
I have defined the following:
public class DiffChunk
{
// impliment UI independant code... DOES NOT overide toString()!!!!
}
public class DiffChunkText extends DiffChunk
{
// overides toString() ONLY!!!!
}
So in the differnence finding class I have something like this:
public class FileDiff<T extends DiffChunk>
{
public FileDiff(String file1, String file2)
{
// parse and generate LCS for both files
T chunk=new T(...); // Complor error
T chunk=(T) new DiffChunk(...); // Problem for reasons
discussed alrady
while(operations are left in the LCS)
if op != NOP // the lines are the SAME
chunk.add(op)
else {
chunks.add(chunk);
T chunk=new T(...); // see comment above
T chunk=(T) new DiffChunk(...);
}
}
}
public class CmdLine
{
public void main(String [] args)
{
for(DiffChunkPrint chunk:new FileDiff(arg[0],arg[1])
System.out.println(chunk);
}
}
public class GUI {
public void main(String [] args)
{
for(DiffChunkPrint chunk:new FileDiff(arg[0],arg[1])
Draw(chunk);
}
}
I don't understand the question. In fact, I don't really understand the
application described earlier in the thread. What makes a subtype desired?
What do you mean by prototyped? Are you implying you already need an
instance of a subtype to clone() it? 'Cause you don't, you just generate
instances of the subtypes via the factory.
I hope the above makes it clearer
--Aryeh