Weird atypical - can new() be old ?

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

I am writing some classes for use in an application language that provides
hooks into java objects.

In my app language I have this syntax (I know, not pretty)
j = new ('classname', constructor-arg-1, ..., constructor-arg-N);
j.call{Type}Method ('methodname', method-arg-1, ... methord-arg-N,
result-var);

The args can only be double or String. The {Type} is any numeric primitive
or String. If numeric the return is cast to double.

I am developing classes R and A.
R is runnable.
A is an adapter. A will have to do things the app language can't (because
the app language can not receive an object in return, only Strings and
numbers).
A will manage a pool of threads running R.
i.e. (I know lots of possible problems here)

static private maxCount = 5;
static private count = -1;
static private Thread[] rthreads = new Thread(maxcount);
static private R[] rs = new Thread(maxcount);
void startR () {
count++;
rthreads[count] = new Thread (rs[count] = new R(count)); // let R know
which one it is
rthreads[count].start();
return count;
}

Suppose R has
private double valueX;
public double getValueX() { return this.valueX; }
void setValueX ( int valueX ) { this.valueX=valueX; }

Now, I could have lots of A methods to gain access to a specific R
void setCounter (double index, double valueX) {
rs[index].setValueX (valueX);
}

I would prefer instead to be able to access the R class directly from my app
language, so that I could more simply do:
ja = new ('A');
ja.callIntMethod ('startR', index);
j = new ('R', index);
j.callVoidMethod ('setValueX', 3.141926);
and avoid multiple indirection/dispatch


So I want this weird thing to happen
r1 = new R(1);
r2 = new R(1);
// r1 and r2 should reference the same object

I am hoping that R's constructor can replace what new() wants to return with
R corresponding to A.getR(index)
public class R implements Runnable {
R (double index) {
// somehow cause new to 'return' A.getR(index)
}
....
}

Is there a pattern name for any of this ?
I've heard of factory, but haven't done any reading about that yet.
 
J

Joona I Palaste

Richard A. DeVenezia said:
So I want this weird thing to happen
r1 = new R(1);
r2 = new R(1);
// r1 and r2 should reference the same object
I am hoping that R's constructor can replace what new() wants to return with
R corresponding to A.getR(index)
public class R implements Runnable {
R (double index) {
// somehow cause new to 'return' A.getR(index)
}
...
}

Can't be done in Java. In Java, new always returns a new object or
throws an exception.
Is there a pattern name for any of this ?
I've heard of factory, but haven't done any reading about that yet.

I suggest you don't call the constructor directly, but instead make
some kind of method like:
public static R getR(double index);
which then can return either a new R or an old one. This is known as
the factory pattern.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top