mercredi 6 décembre 2017

Incorrect usage of stategy pattern?

Is the following an incorrect usage of the strategy

interface FilterStrategy {
  public void filter();
}

class OldProductsStrategy implements FilterStrategy {
  public filter();
}

class NewProductsStrategy implements FilterStrategy {
  public filter();
}

class ProductsFilterStrategy {
  public getFilter(a) {
   if(a) {
     return new NewProductsStrategy();
   } else {
     return new OldProductsStrategy();
   }
}

class Client {
  new ProductsFilterStrategy().getFilter(true).filter();
}

Is delegating the selection of the strategy implementation part of the strategy pattern?

Aucun commentaire:

Enregistrer un commentaire