I ask my self if the Facade Pattern violates the SOLID principles and if the pattern itself is an Anti Pattern.
UPDATED
My Opinions:
OCP is violated, because you will add more methods to the facade
LSP/ISP is violated, because the consumer/client has dependency that will have too much methods, which the client does not need.
DIP, is in my opinion not violated as long as the interface itself only exposes abstraction or DTOs.
SOLID in general is in my opinion about small, stable and thereby composable interfaces.
A facade tends to be the opposite big and not stable.
Consequences. The Facade pattern offers the following benefits: It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use. It promotes weak coupling between the subsystem and its clients.
Common Mistakes while Implementing Facade Design Pattern Just naming a class as ABCDFacade. java doesn'r really make it a facade. Creating a java class and 'forcing' the UI to interact with other layers through it and calling it a facade layer is one more popular mistake.
SOLID is a popular set of design principles that are used in object-oriented software development. SOLID is an acronym that stands for five key design principles: single responsibility principle, open-closed principle, Liskov substitution principle, interface segregation principle, and dependency inversion principle.
In general idea without considering any implementation specifics, we can consider Facade as a way of hiding a set of sub-systems with a higher level wrapper class using Composition and Encapsulation. The higher level Wrapper class is the one that always talks with any client and the Sub-systems consisted inside the wrapper are the ones that are really doing the jobs.
Example:
public class Bulb{
public void on(){
//logic to turn on the bulb.
}
}
public class Room{
private Bulb bulb;
public void lightUp(){
this.bulb.on();
}
}
In above Bulb is a sub-system and Room is the wrapper (Facade). So a client would like to see as it is directly lighting up the room and not interested in knowing what it has to be done with the bulb.
So back into your question, If we take SOLID principles one by one.
And about considering Facade as an anti-pattern (Cons of Facade),
Still the choice of using Facade(Many patterns) would be by personal flavor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With