Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Facade in Laravel implements Facade or Proxy Pattern?

As I understood, the Facade Pattern's intent is

to provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. This can be used to simplify a number of complicated object interactions into a single interface.

enter image description here

From what i understood is, the pattern's goal is to hide complexity in subsystem (e.g. You facade class calls many objects in subsystem).

But in Laravel's Facade it only has one class calls another class (not a subsystem). To me it looks more like a Proxy more than Facade. Can some one helps clarify this to me.

like image 928
Rezigned Avatar asked Jun 26 '13 23:06

Rezigned


People also ask

What design patterns does a Facade follow in Laravel?

A facade in Laravel is a wrapper around a non-static function that turns it into a static function. The word "wrapper" can also be used when describing design patterns. Wrapping an object to provide a simplified interface to it is often described as the "facade" pattern. So in short, the wrapper is the facade.

How does facades work in Laravel?

How Facades Work. In a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

What is difference between Facade and proxy pattern?

The proxy pattern is where one object acts as an interface to another object. The difference between proxy and facade patterns are that we can proxies create interfaces for one object. But facades can be interfaces for anything. We use the proxy to call both foo and bar in obj .

What design patterns does a Facade follow?

Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.


1 Answers

The book, Architect's Guide to PHP Design Patterns notes,

The purpose of the Facade is to present a simpler interface to an entire sub-system composed of many objects.

At my understanding, design patterns are not a "priori" obligation but rather a commitment to implement a general reusable solution to a commonly occurring problem.

Laravel 4 takes advantage of the Facade Design Pattern, which allows it to provide an expressive syntax through a static API but still keep it testable under the hood. The Facade hides the complexity / implementation of the code. Indeed, Laravel's Facades implement a single object. We shouldn't neglect though that Laravel's App Facade (an IoC container) encapsulates the whole application .

A Proxy pattern intercepts a request and does extra work, for instance: filtering, ACL, transformation... The "consumer" Object is blissfully unaware of that extra work provided by the Proxy layer.

like image 135
JohnD Avatar answered Sep 19 '22 07:09

JohnD