File tree 2 files changed +49
-0
lines changed
2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 21
21
* Мост (Bridge) `Python <structural/bridge.py >`_
22
22
* Компоновщик (Composite) `Python <structural/composite.py >`_
23
23
* Декоратор (Decorator) `Python <structural/decorator.py >`_
24
+ * Фасад (Facade) `Python <structural/facade.py >`_
24
25
25
26
26
27
Паттерны поведения
Original file line number Diff line number Diff line change
1
+ # coding: utf-8
2
+
3
+ """
4
+ Фасад (Facade) - паттерн, структурирующий объекты.
5
+
6
+ Предоставляет унифицированный интерфейс вместо набора интерфейсов некоторой подсистемы.
7
+ Фасад определяет интерфейс более высокого уровня, который упрощает использование подсистемы.
8
+ """
9
+
10
+
11
+ class Paper (object ):
12
+ """Бумага"""
13
+ def __init__ (self , count ):
14
+ self ._count = count
15
+
16
+ def get_count (self ):
17
+ return self ._count
18
+
19
+ def draw (self , text ):
20
+ if self ._count > 0 :
21
+ self ._count -= 1
22
+ print text
23
+
24
+
25
+ class Printer (object ):
26
+ """Принтер"""
27
+ def error (self , msg ):
28
+ print 'Ошибка: %s' % msg
29
+
30
+ def print_ (self , paper , text ):
31
+ if paper .get_count () > 0 :
32
+ paper .draw (text )
33
+ else :
34
+ self .error ('Бумага закончилась' )
35
+
36
+
37
+ class Facade (object ):
38
+ def __init__ (self ):
39
+ self ._printer = Printer ()
40
+ self ._paper = Paper (1 )
41
+
42
+ def write (self , text ):
43
+ self ._printer .print_ (self ._paper , text )
44
+
45
+
46
+ f = Facade ()
47
+ f .write ('Hello world!' ) # Hello world!
48
+ f .write ('Hello world!' ) # Ошибка: Бумага закончилась
You can’t perform that action at this time.
0 commit comments