Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Facade Pattern violates the SOLID principles?

I ask my self if the Facade Pattern violates the SOLID principles and if the pattern itself is an Anti Pattern.

UPDATED

My Opinions:

  • SRP is violated, when you don't just call methods directly, but do things like Transactions, Logging, Error Handling (What I have seen a lot of times). Even with just calling other methods I am in doubt.
  • 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.

like image 890
Rookian Avatar asked Aug 22 '16 12:08

Rookian


People also ask

What are the consequences of facade pattern?

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.

What is a common misuse of the facade pattern?

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.

Which design patterns follow SOLID principles?

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.


1 Answers

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.

  1. SRP: You should be thinking that, the wrapper class violates this principle, because it is doing more than a single duty. But on the other hand, it is just calling some others to do them, and not holding the implementation with the wrapper. Anyway that thought can be much personal one.
  2. OCP: Definitely yes, if you are adding some more subsystems/functionalities. Meantime you can use some modern implementation binding framework to avoid violating OCP in this situation.
  3. LSP: In general I don't find any. But there can be situations which are implementation specific, not with the pattern.
  4. ISP: Same as LSP.
  5. DIP: Same as LSP, ISP.

And about considering Facade as an anti-pattern (Cons of Facade),

  1. One can see, It is increasing the maintenance effort. For some changes you have to change the sub-system(s) implementation + respective wrapper calls.
  2. Sub-systems are tightly coupled with the Wrapper.
  3. It is some what procedural and a bit away from object orientation.

Still the choice of using Facade(Many patterns) would be by personal flavor.

like image 91
Supun Wijerathne Avatar answered Sep 18 '22 14:09

Supun Wijerathne