Blog by Leven

Facade-Pattern

What is Facade pattern

在软件工程中,隐藏系统的复杂性,并向客户端提供了可以访问系统的接口。这种类型的设计模式属于结构型模式
In the software engineering, Facade pattern can hide the complexities of the larger system and provide a simple interface to the client. This kind of design pattern is included in structural patterns.

The usage of Facade pattern

  1. advantage: easy for client; improve the safety

  2. disadvantage: not conform open-close principle; hard to modify.

Scene and example

门面模式的应用非常广泛。当需要屏蔽掉内部的细节,只给出简单接口的时候,你就会想到门面模式。它对客户非常友好。下面一个例子,是模拟计算机处理程序的过程。

Facade pattern is widely used in many applications. When you need to hide the internal details and give only simple interfaces, you will consider facade pattern. It is very friendly to client. The simple example shown below is simulating how computers deal with programs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

"""
Facade pattern example.
"""

# Complex computer parts
class CPU(object):
"""
Simple CPU representation.
"""
def freeze(self):
print("Freezing processor.")

def jump(self, position):
print("Jumping to:", position)

def execute(self):
print("Executing.")


class Memory(object):
"""
Simple memory representation.
"""
def load(self, position, data):
print("Loading from {0} data: '{1}'.".format(position, data))


class SolidStateDrive(object):
"""
Simple solid state drive representation.
"""
def read(self, lba, size):
return "Some data from sector {0} with size {1}".format(lba, size)


class ComputerFacade(object):
"""
Represents a facade for various computer parts.
"""
def __init__(self):
self.cpu = CPU()
self.memory = Memory()
self.ssd = SolidStateDrive()

def start(self):
self.cpu.freeze()
self.memory.load("0x00", self.ssd.read("100", "1024"))
self.cpu.jump("0x00")
self.cpu.execute()


computer_facade = ComputerFacade()
computer_facade.start()

Facade in Laravel

Laravel is a widely used php framework. In the framework, the facade pattern is a very important feature. This article (Understanding Laravel Facades) introduces how facade is used in Laravel. And if you want to create a new facade , you should follow for steps below,

  1. Create your own service YourService.php , it may look like this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    namespace App\Services\Api;

    class YourService {

    function __construct(){
    echo "function __constructed";
    }
    public function doSomething(){
    echo "function __doSomething";
    }
  2. Create a Facade class YourFacade.php,

    1
    2
    3
    4
    5
    6
    7
    8
    9
    namespace App\Facades;

    use Illuminate\Support\Facades\Facade;

    class YourFacade extends Facade{
    protected static function getFacadeAccessor(){
    return 'your';
    }
    }
  3. Register your object into ServiceProvider YourServiceProvider.php,

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    namespace App\Providers;

    use Illuminate\Support\ServiceProvider;
    use App\Services\Api\YourService;

    class YourServiceProvider extends ServiceProvider
    {
    /**
    * Bootstrap the application services.
    *
    * @return void
    */
    public function boot()
    {
    //
    }
    /**
    * Register the application services.
    *
    * @return void
    */
    public function register()
    {
    $this->app->singleton('your', function () {
    return new YourService();
    });
    }
    }
  4. Register your provider and alias name in config file \config\app.php(with this step you can use use AnyWord instead of referencing its location use App\Services\Api and this is the advantage of facade pattern ),

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ... 
    'providers' => [
    ...
    App\Providers\YourServiceProvider::class
    ],
    ...

    'aliases' => [
    ...
    'AnyWord' => App\Facades\YourFacade::class
    ],
    ...

Reference document

wiki - Facade_pattern
Understanding Laravel Facades