samedi 9 octobre 2021

What we called this design pattern and it is same as strategy pattern?

abstract class BaseService {
 public void doSomething();
}  


class AService {
  public void doSomething(){
   // Do something...
  }
}

class BService {
  public void doSomething(){
   // Do something...
  }
}

class ServiceResolver {
 pubic static BaseService resolveService(String type) {
  if(type == "A") return new AService();
  if (type == "B") return new BService();
 }
}

Usage:
BaseService resolvedService = ServiceResolver.resolveService(type);
resolvedService.doSomething();

What do we call the above pattern? Is it the same as the strategy pattern? I feel it is different because in the strategy pattern client chooses the type of algorithm (https://refactoring.guru/design-patterns/strategy) and passes it to the context class for execution but in my above example, client doesn't have to know anything.

Aucun commentaire:

Enregistrer un commentaire