zoukankan      html  css  js  c++  java
  • 设计模式之外观模式

    # 外观模式
    # 为子系统中的一组接口提供一个一致的界面成为外观模式,外观模式定义了一个高层接口,这个接口使得这一子系统更容易使用
    # 如下压缩模块、ORM等
    from os import path
    import logging
    
    
    class ZIPModel:
        """ZIP模块,负责ZIP文件的压缩与解压缩"""
        def compress(self):
            pass
    
        def decompress(self):
            pass
    
    
    class RARModel:
        """RAR模块,负责RAR文件的压缩与解压缩"""
        def compress(self):
            pass
    
        def decompress(self):
            pass
    
    
    class ZModel:
        """7Z模块,负责7Z文件的压缩与解压缩"""
        def compress(self):
            pass
    
        def decompress(self):
            pass
    
    class CompressModel:
        """压缩模块的外观类"""
        def __init(self):
            self.__zipModel = ZIPModel()
            self.__rarModel = RARModel()
            self.__zModel = ZModel()
    
        def compress(self, srcFilePath, dstFilePath, type):
            """根据不同的压缩类型,压缩成不同的格式"""
            extName = "." + type
            fullName = dstFilePath + extName
            if(type.lower() == "zip"):
                self.__zipModel.compress(srcFilePath, dstFilePath)
            elif(type.lower() == "rar"):
                self.__rarModel.compress(srcFilePath, dstFilePath)
            elif(type.lower() == "7z"):
                self.__zModel.compress(srcFilePath, dstFilePath)
            else:
                logging.error("Not support this format:" + str(type))
                return False
            return True
    
        def decompress(self, srcFilePath, dstFilePath):
            baseName = path.baseName(srcFilePath)
            extName = baseName.split(".")[-1]
            type = extName
            if(type.lower() == "zip"):
                self.__zipModel.decompress(srcFilePath, dstFilePath)
            elif(type.lower() == "rar"):
                self.__rarModel.decompress(srcFilePath, dstFilePath)
            elif(type.lower() == "7z"):
                self.__zModel.decompress(srcFilePath, dstFilePath)
            else:
                logging.error("Not support this format:" + str(type))
                return False
            return True
  • 相关阅读:
    /bin/bash^M: bad interpreter: No such file or dire
    ****LINUX命令(含GIT命令)个人总结
    创建和编辑 crontab 文件
    Linux下用于查看系统当前登录用户信息的4种方法
    linux下cat命令详解
    crontab 指定执行用户
    crontab定时运行git命令 更新代码库
    ubuntu添加环境变量【原创】
    ubuntu下设置环境变量的三种方法【转】
    笔记三、apache搭建gitweb【转】
  • 原文地址:https://www.cnblogs.com/loveprogramme/p/13040924.html
Copyright © 2011-2022 走看看