strategy pattern question ?

Discussion in 'Java' started by tomo, Oct 27, 2009.

  1. tomo

    tomo Guest

    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
     
    tomo, Oct 27, 2009
    #1
    1. Advertisements

  2. tomo

    Stefan Ram Guest

    By delegation or inheritance of the common method
    from a third class.
    I deem this to be just an implementation detail.
     
    Stefan Ram, Oct 27, 2009
    #2
    1. Advertisements

  3. 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;
    }
    }
     
    John B. Matthews, Oct 27, 2009
    #3
  4. tomo

    Mayeul Guest

    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.
     
    Mayeul, Oct 27, 2009
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.