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
  • 相关阅读:
    MYSQL数据库设计规范与原则
    PHP-CI框架数据库连接默认是长连接,需要注意应用场景
    mysql性能调优与架构设计笔记
    JavaScript学习笔记
    PHP一维数组和二维数字排序整理
    MYSQL常用的Show命令笔记
    设计模式之建造者模式
    设计模式之工厂模式
    设计模式前提篇二(C++编程原则)
    设计模式前提篇一(C++/基础)
  • 原文地址:https://www.cnblogs.com/loveprogramme/p/13040924.html
Copyright © 2011-2022 走看看