举例分析Python中设计模式之外观模式的运用

作者:像风一样的自由 时间:2021-11-24 01:54:48 

应用特性:
在很多复杂而小功能需要调用需求时,而且这些调用往往还有一定相关性,即一调用就是一系列的。
结构特性:
把原本复杂而繁多的调用,规划统一到一个入口类中,从此只通过这一个入口调用就可以了。

代码结构示例:


class ModuleOne(object):
 def Create(self):
   print 'create module one instance'

def Delete(self):
   print 'delete module one instance'

class ModuleTwo(object):
 def Create(self):
   print 'create module two instance'

def Delete(self):
   print 'delete module two instance'

class Facade(object):
 def __init__(self):
   self.module_one = ModuleOne()
   self.module_two = ModuleTwo()

def create_module_one(self):
   self.module_one.Create()

def create_module_two(self):
   self.module_two.Create()

def create_both(self):
   self.module_one.Create()
   self.module_two.Create()

def delete_module_one(self):
   self.module_one.Delete()

def delete_module_two(self):
   self.module_two.Delete()

def delete_both(self):
   self.module_one.Delete()
   self.module_two.Delete()

有点类似代理模式,不同之处是,外观模式不仅代理了一个子系统的各个模块的功能,同时站在子系统的角度,通过组合子系统各模块的功能,对外提供更加高层的接口,从而在语义上更加满足子系统层面的需求。

随着系统功能的不断扩张,当需要将系统划分成多个子系统或子模块,以减少耦合、降低系统代码复杂度、提高可维护性时,代理模式通常会有用武之地。

再来看一个例子:


class small_or_piece1:
 def __init__(self):
   pass

def do_small1(self):
   print 'do small 1'

class small_or_piece_2:
 def __init__(self):
   pass

def do_small2(self):
   print 'do small 2'

class small_or_piece_3:
 def __init__(self):
   pass

def do_small3(self):
   print 'do small 3'

class outside:
 def __init__(self):
   self.__small1 = small_or_piece1()
   self.__small2 = small_or_piece_2()
   self.__small3 = small_or_piece_3()

def method1(self):
   self.__small1.do_small1()  ##如果这里调用的不只2两函数,作用就显示出来了,可以把原本复杂的函数调用关系清楚化,统一化
   self.__small2.do_small2()

def method2(self):
   self.__small2.do_small2()
   self.__small3.do_small3()

if __name__ == '__main__':
 osd = outside()
 osd.method1()
 osd.method2()

结果:


do small 1
do small 2
do small 2
do small 3

标签:Python,设计模式
0
投稿

猜你喜欢

  • C#中使用ADOMD.NET查询多维数据集的实现方法

    2023-10-27 05:38:15
  • 详解Java如何进行Base64的编码(Encode)与解码(Decode)

    2023-01-31 18:53:34
  • Android工具类Toast自定义图片和文字

    2021-11-15 08:22:44
  • 教你怎么用java一键自动生成数据库文档

    2021-08-01 02:34:36
  • 利用C#实现网络爬虫

    2022-03-26 18:40:25
  • 本地jvm执行flink程序带web ui的操作

    2022-09-03 20:49:00
  • C# WinForm实现图片浏览器

    2022-04-12 23:28:00
  • Spring Security权限想要细化到按钮实现示例

    2022-10-14 20:52:38
  • 一个进程间通讯同步的C#框架引荐

    2023-01-18 17:55:03
  • C#创建临时文件的方法

    2023-06-16 14:32:36
  • 完美解决gson将Integer默认转换成Double的问题

    2022-06-06 00:53:36
  • spring MVC中接口参数解析的过程详解

    2023-11-28 09:17:50
  • c# wpf如何使用Blend工具绘制Control样式

    2022-10-26 09:35:25
  • eclipse+maven+spring mvc项目基本搭建过程

    2022-12-18 03:50:52
  • Android实现带描边的圆角图片

    2021-09-06 19:00:04
  • spring cloud将spring boot服务注册到Eureka Server上的方法

    2023-12-08 19:42:09
  • 详解C#开发Android应用程序的流程

    2021-08-11 21:32:59
  • c#如何用好垃圾回收机制GC

    2023-03-21 08:48:22
  • Spring Security入门demo案例

    2023-07-01 18:57:30
  • 常用json与javabean互转的方法实现

    2023-01-07 14:24:19
  • asp之家 软件编程 m.aspxhome.com