程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python design mode - structural mode - appearance mode

編輯:Python

Catalog

List of articles

  • Catalog
  • Appearance mode (Facade)
  • Application scenarios
  • Code example

Appearance mode (Facade)

Appearance mode (Facade), Provides a consistent interface for a set of interfaces in a subsystem . The facade pattern defines a high-level interface , This interface makes this subsystem easier to use .

The difference between appearance mode and adapter mode is : Appearance mode is to design a unified interface for small systems under large systems , The adapter mode is designed for various interface calls of different systems .

advantage

  1. Subsystem and Client The loose coupling relationship between .
  2. Client Shielding subsystem components , Less Client Number of objects to process , And make it easier to use the subsystem .

Application scenarios

  • Early stage of design , There should be a conscious separation of different layers , Create appearance patterns between layers .
  • The development phase , Subsystems are becoming more and more complex , Add appearance mode to provide a simple calling interface .
  • When maintaining a large legacy system , Maybe this system is very difficult to maintain and expand , But it also contains very important functions , Develop an appearance class for it , So that the new system can interact with it .

Code example

class API1:
def Save(self):
print(' Save the data A')
def Del(self):
print(' Delete data A')
class API2:
def Save(self):
print(' Save the data B')
def Del(self):
print(' Delete data B')
class Facade:
def __init__(self):
self._api1 = API1()
self._api2 = API2()
def SaveAll(self):
[obj.Save() for obj in [self._api1, self._api2]]
def DelAll(self):
[obj.Save() for obj in [self._api1, self._api2]]
if __name__ == '__main__':
test = Facade()
test.SaveAll()
test.DelAll()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved