Facade Pattern
Facade pattern is basically the same as Service. It used to initalise different dependency if you need when the class gets a bit complex
public class RateLimiterService {
private final RateLimitingAlgorithm rateLimitingAlgorithm;
private final Scheduler scheduler = createScheduler();
public RateLimiterService(BucketTokenConfig config) {
rateLimitingAlgorithm = new BucketToken(config, scheduler);
}
public RateLimiterService(LeakingBucketConfig config) {
rateLimitingAlgorithm = new LeakingBucket(config, scheduler);
}
private Scheduler createScheduler() {
Timer timer = new Timer();
ExecutorService executorService = Executors.newSingleThreadExecutor();
return new AlgorithmScheduler(executorService, timer);
}
public boolean apply(Request request) {
return rateLimitingAlgorithm.apply(request);
}
public int getCapacity() {
return rateLimitingAlgorithm.getCapacity();
}
}
For example in here, a rate limiting algorithm will take a Scheduler
which we can use Facade
pattern to inject the dependency in.