New to design patterns so my question maybe silly.
I want to use strategy pattern for returning data from a databases in the same format. Nevertheless it varies the query on the underlying database that collects the data.
So I am trying to implement strategy pattern.
I don't use an interface but an abstract superclass because the main function "getData()" is always the same. What it varies is a method on the subclasses which gives the query to "getData()".
I am using php so I can have an abstract class with some function implemented and what I have done already is:
I have a superclass called Collector as:
abstract class Collector {
public function getData() {
return $this -> Db -> setQuery( $this -> getQuery() ) -> loadRows();
}
abstract protected function getQuery();
}
And a subclass is:
CollectorA extends Collector {
protected function getQuery() {
return "select a, b from t where c = 2";
}
}
If a have understand correctly the public function getData() is my interface and all clients can use it. The protected method can not be used by the client. Also according to the strategy method theory this is also the function that must override the subclasses.
But in my case I want the sublcasses to override the protected function "getQuery()". So I am not sure If what I am doing is correct.
Is my implementation for strategy pattern correct even if my subclasses override a function, "getQuery()", that is different from the public interface function "getData()"?