设计模式之外观模式

# 外观模式
# 为子系统中的一组接口提供一个一致的界面成为外观模式,外观模式定义了一个高层接口,这个接口使得这一子系统更容易使用
# 如下压缩模块、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
原文地址:https://www.cnblogs.com/loveprogramme/p/13040924.html