Is facade a class which contains a lot of other classes?
What makes it a design pattern? To me, it is like a normal class.
Can you explain to me this Facade pattern?
Is facade a class which contains a lot of other classes?
What makes it a design pattern? To me, it is like a normal class.
Can you explain to me this Facade pattern?
Another example of facade: say your application connects to database and display results on the UI. You can use facade to make your application configurable, as in run using database or with mock objects. So you will make all the database calls to the facade class, where it will read app config and decide to fire the db query or return the mock object. this way application becomes db independent in case db is unavailable.
The facade pattern provides a unified interface to the subsystem interface group. The facade defines a high-level interface, which simplifies the work with the subsystem.
Regarding your queries:
Yes. It is a wrapper for many sub-systems in application.
All design patterns too are normal classes. @Unmesh Kondolikar rightly answered this query.
According to GoF, Facade design pattern is defind as :
Provide a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use
The Facade pattern is typically used when:
Let's take a real word example of cleartrip website.
This website provides options to book
Code snippet:
Explanation:
FlightBooking, TrainBooking and HotelBooking
are different sub-systems of large system :TravelFacade
TravelFacade
offers a simple interface to book one of below optionsbook API from TravelFacade internally calls below APIs of sub-systems
In this way,
TravelFacade
provides simpler and easier API with-out exposing sub-system APIs.Key takeaways : ( from journaldev article by Pankaj Kumar)
Have a look at sourcemaking article too for better understanding.
A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
The Facade design pattern is a structural pattern as it defines a manner for creating relationships between classes or entities. The facade design pattern is used to define a simplified interface to a more complex subsystem.
The facade pattern is ideal when working with a large number of interdependent classes, or with classes that require the use of multiple methods, particularly when they are complicated to use or difficult to understand. The facade class is a "wrapper" that contains a set of members that are easily understood and simple to use. These members access the subsystem on behalf of the facade user, hiding the implementation details.
The facade design pattern is particularly useful when wrapping subsystems that are poorly designed but cannot be refactored because the source code is unavailable or the existing interface is widely used. Sometimes you may decide to implement more than one facade to provide subsets of functionality for different purposes.
One example use of the facade pattern is for integrating a web site with a business application. The existing software may include large amounts of business logic that must be accessed in a particular manner. The web site may require only limited access to this business logic. For example, the web site may need to show whether an item for sale has reached a limited level of stock. The IsLowStock method of the facade class could return a Boolean value to indicate this. Behind the scenes, this method could be hiding the complexities of processing the current physical stock, incoming stock, allocated items and the low stock level for each item.
It is basically single window clearance system.You assign any work it will delegate to particular method in another class.
Source: https://sourcemaking.com/design_patterns/facade