Search code examples
javadesign-patternsstrategy-pattern

Strategy Pattern help needed


There exists interface Algorithm

There exists class MathAlgorythm implements Algorithm (returns MathResult, which implements Result)

There exists class ChemitryAlgorythm implements Algorithm (returns ChemitryResult, which implements Result)

Additionally, there exists a Context class, which is used to pass data to these Algos. Data is passed in the following manner:

public Result executeStrategy(Data data) {
    return algo.execute(data);
}

Suppose I, executeStrategy and get back return MathAlgorithm.execute(data); I get something of type Result right?

I then execute return ChemitryAlgorithm.execute(data); Again i get something Result

Question: Result is an interface. I need to gain access to concrete class implementation as MathResult or ChemistryResult. In other words. Once i get something of type Result, i need to dig deeper and know what class hides behind the interface

I hope this rambling is not too confusing.

Thank you for reading and responding


Solution

  • If you give Result a method like T get() which the concrete implementations have to implement, then you don't need to know about the concrete implementations.