strategy pattern question ?

T

tomo

Let's say i have this example

public interface Caclucation{

public int calculateTax1(int a, int b, int c);
public int clalcuateTax2(int a, int b);

}

class ConcreteOne implements Calculation{

public int calculateTax1(int a, int b, int c){
return a+b+c;
}

public int calculateTax2(int a, int b){
return a+b;
}


}

class ConcreteTwo implements Calculation{

public int calculateTax1(int a, int b, int c){
return a+b-c;
}

public int calculateTax2(int a, int b){
return a+b;
}


}

So I have two different strategies, but calculateTax2 method is the same for
both od them ? How can I avoid this ? And if i have many methods that are
same in the first and second class is there a
way to put them to some common class ? whould that be a violation on the
strategy pattern ?

Thanks.






__________ Information from ESET NOD32 Antivirus, version of virus signature database 4482 (20091005) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
S

Stefan Ram

tomo said:
So I have two different strategies, but calculateTax2 method is the same for
both od them ? How can I avoid this ?

By delegation or inheritance of the common method
from a third class.
whould that be a violation on the strategy pattern ?

I deem this to be just an implementation detail.
 
J

John B. Matthews

By delegation or inheritance of the common method
from a third class.

OP: Using Stefan's insight, you can make calculateTax1() an abstract
method. Alternatively, consider this example in the tutorial, "When an
Abstract Class Implements an Interface":

<http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html>

In your case,

interface Calculation {
public int calculateTax1(int a, int b, int c);
}

abstract class AbstractCalculation implements Calculation {

// calculateTax1 implementation deferred

public int calculateTax2(int a, int b) {
return a + b;
}
}

class ConcreteOne extends AbstractCalculation {
public int calculateTax1(int a, int b, int c) {
return a + b + c;
}
}

class ConcreteTwo extends AbstractCalculation {
public int calculateTax1(int a, int b, int c) {
return a + b - c;
}
}
 
M

Mayeul

Stefan said:
By delegation or inheritance of the common method
from a third class.

Other possibilities I can think of and that may or may not be suitable,
depending on the precise case :

- Just have two strategy patterns. One for calculateTax1 and one for
calculateTax2.

- Make the implementation of calculateTax2 a strategy itself and inject
it in both ConcreteOne and ConcreteTwo.
 

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

Similar Threads

pattern question ? 14
hibernate number save ? 7
Hibernate runtime persist ? 5
enum or FK question ? 2
hibernate question ? 2
sql sort problem ? 4
Why this doesen't work 11
web application load xls file ? 1

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top