Is "Facade design pattern" and Java interface conceptually same ? As both achieve abstraction by providing simple way of expressing a complex functionality. Can we say by creating interface we followed Facade pattern ?
4 Answers
No, I's not the same. For implementing Facade design pattern is not necessary to implement any interface. It just provide a new layer to communicate to your services, as an example.
class ServiceFacade() {
private Service1 service1;
private Service2 service2;
public void update(Entity entity){
service1.update(entity);
service2.update(entity);
}
}
So it will give you one point to communicate with all services that are related. Instead of calling service1.update()
and service2.update()
you just call facade.update()
, so you are sure that both services update the entity.
One more example: maybe all the time service
is updated you need to refresh()
you cache. This also can be incapsulate in your Facade:
public void update(Entity entity) {
service.update(entity);
cache.refresh();
}
If your facade class has only one dependency, but you want to extend the functionality of this dependency, you can achieve that by implementing Decorator pattern. Here is where you do need to implement the interface.
Let's take a look at the following example. Suppose you have a service
and you want to extend it with a simple cache
class CachedService implement Service {
private Service service;
CachedService(Service service, Cache cache){
......
}
......
private Entity get(Long id) {
Entity cachedEntity = cache.get(id);
if (cachedEntity != null){
return cachedEntity;
}
return service.get(id);
}
}

- 8,331
- 6
- 40
- 69
-
Thanks. So can we say interface is one way of using "Facade pattern" ? – Kaushik Lele Feb 20 '17 at 11:10
-
@KaushikLele If your facade class implements the same interface as the service it contain it's rather Decorator pattern – Sergii Bishyr Feb 20 '17 at 11:13
Façade is a conceptual design pattern. There's no specific interface nor implementation.
It's just an additional layer on which you implement operations that otherwise would force upper layers to understand and be coupled to irrelevant implementation details for themselves.
For example, a façade method could be a credit card payment process on which you need to send many requests and process their responses to store the billing result on some database.
Without a façade method, every part of your system on which you need to perform a payment using a credit card would need to repeat those 5-10 code lines all over again. At some point, you find that you need an extra step when performing those operations and you realize that you need to refactor thousands of code lines. That is, your solution could have a security hole and you won't be able to respond to the security treat in time. A façade can make the difference from being a poor software provider to being a serious and robust one!
In the opposite side, when you implement proper façades, when some layer needs to perform a credit card payment it just injects a billing façade and it stays agnostic about how the billing process is being made hence you don't repeat yourself in many parts of your system and refactoring isn't a tedious and time-consuming task.
While façade pattern doesn't define an interface per se, façades should be defined as interfaces, since they can also act as proxies: an implementation could make use of the service layer directly and other might call a RESTful API to perform the same task, depending on who needs the façade itself.
Anyway, this design pattern isn't specific to simplifying a given service layer. You might consider a façade method a method that may simplify a complex string manipulation involving many regular expressions, replacements and insertions.

- 63,804
- 18
- 124
- 206
Good question. I was once asked in an interview what design pattern an Interface represents, and my best guess was Facade. I don't really know what the interviewer was trying to get at, though.

- 35,733
- 41
- 130
- 213