vendredi 22 octobre 2021

How could I call an abstract class method who is common between subclasses without instanciating any subclass

I have a code like this :

interface Contract {
  createSomething(); //not common
  updateSomething(); //not common
  getSomething(); //method who is supposed to be common between all strategies
}
interface Strategy {
  createSomething();
  updateSomething();
  getSomething();
}
Abstract class AbstractStrategy implements Strategy {

  @Override
  getSomething() {
    // the common code
  }
}
class strategyA extends AbstractStrategy {

  @Override
  createSomething() {...}

  @Override
  updateSomething() {...}
}
class ContractImpl implements Contract {

  @Override
  createSomething() {
    //get the good strategy 
    //call the strategy.createSomething();
  }

  @Override
  updateSomething() {
    //get the good strategy 
    //call the strategy.updateSomething();
  }

  @Override
  getSomething() {
     **Here is the question**
  }
}

Question:

  • How could I rewrite this code so I could call the getSomething() method without having to instanciate a random subclass just to call it with the super keyword ?

Aucun commentaire:

Enregistrer un commentaire