• 设计模式之外观模式


    # 外观模式
    # 为子系统中的一组接口提供一个一致的界面成为外观模式,外观模式定义了一个高层接口,这个接口使得这一子系统更容易使用
    # 如下压缩模块、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
  • 相关阅读:
    django--模型层(orm)-查询补充及cookie
    django-模型层(model)-多表相关操作(图书管理练习)
    Linux-正则表达式与三剑客
    网络配置命令优先级和元字符
    Linux-Nginx和NFS
    Linux-内存进程和软件安装
    乌龟棋
    倍增 Tarjan 求LCA
    切蛋糕
    HDU1505 City Game/玉蟾宫/全0子矩阵 悬线法
  • 原文地址:https://www.cnblogs.com/loveprogramme/p/13040924.html
Copyright © 2020-2023  润新知