How to describe templates in java?

For example, I need 2 classes having the same methods but working with different object types. Should I write the same code twice?

interface I{

getValue();

setValue(value);

}

class Aimplements I{

Integer value;

Integer getValue(){return value;}

setValue(Integer value){ this.value = value;}

}

class Bimplements I{

Float value;

Float getValue(){return value;}

setValue(Float value){ this.value = value;}

}

[1113 byte] By [valjoka] at [2007-9-28 1:15:34]
# 1
> Should I write the same code twice?Yes.
jschella at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Oops...note that in no way is an interface going to help with that example
jschella at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

You don't always have to write two different classes. If you are willing to accept a less strict class it can be implemented as below. I can see that both your classes use numerical values, so a choice of type could be the Number class (which is a super-class of both Integer and Float)

class AB {

Number value;

Number getValue() { return value; }

void setValue(Number value) { this.value = value; }

}

Søren

soren_baka at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
Alternatively you could wait till 1.5 (or go to the "adding generics" forum, and follow the links to the JSR14 proposal and the prototype implementation and start doing it now.
brucechapmana at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
orclass O { Object value; Object getValue() { return value;} void setValue(Object value) { this.value = value;}}
huyngan21a at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

> For example, I need 2 classes having the same methods

> but working with different object types. Should I

> write the same code twice?

No, abstract the parameteric differences away with a common base to both parameter types.

However in your example your parameters are primatives not objects. You could wrap these.

interface I {

getValue();

setValue(abstractType value) ;

}

class intObj implements astractType { ... } ;

class floatObj implements astractType { ... } ;

class A implements I {

Integer value;

Integer getValue() { return value; }

setValue(intObj value) { this.value = value; }

}

class B implements I {

Float value;

Float getValue() { return value; }

setValue(floatObj value) { this.value = value; }

}

MartinS.a at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
This is terribly unhelpful.What is astractType used for?How did making the two classes but using the set with abstractType help?Seems your answer is just mixing us up. The former answers where comprehensive.
pashutea at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

Hi,

please don`t use an interface here ! In my opinion setters and getter have to be defined on classes, which actually can have instance variables and an interface can evidently not !

If class 'A' and class 'B' are semantically used in the same context and there is really a possible super type which has a 'is a'-relation to 'A' and 'B' then define an abstract superclass.

If no semantic relation exists between 'A' and 'B' then just implement two independent classes.

Adrian

adrianOa at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 9
There are valid cases for using an interface or abstract class with getters/setters. If you forget that they look like properties, they are just methods that can be called polymorphically.
ounosa at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

> There are valid cases for using an interface or

> abstract class with getters/setters.

of course, but the initial reason to have interfaces is not to define setters / getters. it is to define a certain common behaviour. And in this example, I can see no actual common behaviour, except that it seems to be that an instance var has the same name (although it has a different type)

> If you forget

> that they look like properties, they are just methods

> that can be called polymorphically.

you are right. D`accord. But why do I need polymohistic behaviour of a getter for example !

Adrian

adrianOa at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 11

>> There are valid cases for using an interface or

>> abstract class with getters/setters.

>of course, but the initial reason to have interfaces is not to define >setters / getters. it is to define a certain common behaviour

Yep, that's what I meant, the common behavior to be the setters/getters.

This is pretty much irrelevant to the OP topic though, as the use of interface there is inappropriate. Code-generation would be a viable solution, but wouldn't worth the effort if it is just for a couple of classes.

ounosa at 2007-7-7 20:50:13 > top of Java-index,Other Topics,Patterns & OO Design...