设计模式之适配模式(变压器模式、包装模式)

from abc import ABCMeta, abstractmethod
import os

# 适配模式的三个角色:目标、源对象、适配器
# 适配模式的实现方式:组合、多继承
class Target(metaclass=ABCMeta):
    """目标类"""
    @abstractmethod
    def function(self):
        pass


class Adaptee:
    """源对象类"""
    def speciaficFunction(self):
        print("被适配对象的特殊功能")


class Adapter(Adaptee, Target):
    """适配器"""
    def function(self):
        print(进行功能的转换)


class Page:
    """电子书的一页功能"""
    def __init(self, pageNum):
        self.__pageNum = pageNum

    def getContent(self):
        return "" + str(self.__pageNum) + "页的内容..."


class Catalogue:
    """目录结构"""
    def __init__(self, title):
        self.__title = title
        self.__chapters = []

    def addChapter(self, title):
        self.__chapters.append(title)

    def showInfo(self):
        print("书名:" + self.__title)
        print("目录:")
        for chapter in self.__chapters:
            print("    " + chapter)


class IBook(metaclass=Adaptee):
    """电子书文档类的接口"""
    @abstractmethod
    def parseFile(self, filePath):
        """解析文档"""
        pass

    @abstractmethod
    def getCatalogue(self):
        """获取目录"""
        pass

    @abstractmethod
    def getPageCount(self):
        """获取页数"""
        pass

    @abstractmethod
    def getPage(self, pageNum):
        """获取第pageNum页的内容"""
        pass


class TxtBook(IBook):
    """TXT解析类"""
    def parseFile(self, filePath):
        print(filePath + " 文件解析成功")
        self.__title = os.path.splitext(filePath)[0]
        self.__pageCount = 500
        return True

    def getCatalogue(self):
        catalogue = Catalogue(self.__title)
        catalogue.addChapter("第一章 标题")
        catalogue.addChapter("第二章 标题")
        return catalogue

    def getPageCount(self):
        return self.__pageCount

    def getPage(self, pageNum):
        return Page(pageNum)


class EpubBook(IBook):
    """Epub解析类"""
    def parseFile(self, filePath):
        print(filePath + " 文件解析成功")
        self.__title = os.path.splitext(filePath)[0]
        self.__pageCount = 800
        return True

    def getCatalogue(self):
        catalogue = Catalogue(self.__title)
        catalogue.addChapter("第一章 标题")
        catalogue.addChapter("第二章 标题")
        return catalogue

    def getPageCount(self):
        return self.__pageCount

    def getPage(self, pageNum):
        return Page(pageNum)


class Outline:
    """第三方PDF解析库的目录"""
    def __init__(self):
        self.__outlines = []

    def addOutline(self, title):
        self.__outlines.append(title)

    def getOutlines(self):
        return self.__outlines


class PdfPage:
    """PDF页"""
    def __init_(self, pageNum):
        self.__pageNum = pageNum

    def getPageNum(self):
        return self.__pageNum


class ThirdPdf:
    """第三方PDF解析库"""
    def __init__(self):
        self.__pageSize = 0
        self.__title = ""
    
    def open(self, filePath):
        print("第三方库解析PDF文件:" + filePath)
        self.__title = os.path.splitext(filePath)
        self.__pageSize = 1000
        return True

    def getTitle(self):
        return self.__title

    def getOutlines(self):
        outline = Outline()
        outline.addOutline("第一章 PDF电子书标题")
        outline.addOutline("第二章 PDF电子书标题")
        return outline

    def pageSize(self):
        return self.__pageSize

    def page(self, index):
        return PdfPage(index)


class PdfAdapterBook(ThirdPdf, IBook):
    """对第三方的PDF解析库重新进行包装"""
    def __init__(self, thirdPdf):
        self.__thirdPdf = thirdPdf

    def parseFile(self, filePath):
        rtn = self.__thirdPdf.open(filePath)
        if(rtn):
            print(filePath + "文件解析成功")

    def getCatalogue(self):
        outline = self.getOutlines()
        print("将Outline结构的目录转换成Catalogue结构的目录")
        catalogue = Catalogue(self.thirdPdf.getTitle)
        for title in outline.getOutlines():
            catalogue.append(title)
        return catalogue

    def getPageCount(self):
        return self.__thirdPdf.pageSize()

    def getPage(self, pageNum):
        page = self.page(pageNum)
        print("将PDFPage的面对象转换成Page的对象")


class Reader:
    """阅读器"""
    def __init__(self, name):
        self.__name = name
        self.__filePath = ""
        self.__curBook = None
        self.__curPageNum = -1

    def __initBook(self, filePath):
        self.__filePath = filePath
        extName = os.path.splitext(filePath)[1]
        if(extName.toLower() == ".epub"):
            self.__curBook = EpubBook()
        elif(extName.toLower() == ".txt"):
            self.__curBook = TxtBook()
        elif(extName.toLower() == ".pdf"):
            self.__curBook = PdfAdapterBook(ThirdPdf())
        else:
            self.__curBook = None

    def openFile(self, filePath):
        self.__initBook(filePath)
        if(self.__curBook):
            rtn = self.__curBook.parseFile(filePath)
            if rtn:
                self.__curPageNum = 1
            return rtn
        return False

    def close(self):
        print("关闭 " + self.__filePath + " 文件")
        return True
    
    def showCatalogue(self):
        catalogue = self.__curBook.getCatalogue()
        catalogue.showInfo()

    def prePage(self):
        print("往前翻一页: ", end="")
        return self.gotoPage(self.__curPageNum - 1)

    def nextPage(self):
        print("往后翻一页: ", end="")
        return self.gotoPage(self.__curPageNum + 1)

    def gotoPage(self, pageNum):
        if(pageNum > 1 and pageNum < self.__curBook.getPageCount() - 1):
            self.__curPageNum = pageNum
        print("显示第" + str(self.__curPageNum) + "")
        page = self.__curBook.getPage(self.__curPageNum)
        page.getContent()
        return page



    



    
原文地址:https://www.cnblogs.com/loveprogramme/p/13057608.html