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()"?

1 Answer 1


Your Collector class is an example of the template method pattern, where you provide a concrete method that calls into abstract methods to perform an operation. The template method and strategy patterns are indeed quite similar, and you could view the public interface as a strategy and the template method as an implementation detail. Don't worry about which one is "correct", the goal isn't to implement a specific pattern but rather to find the simplest solution that works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.