Python 设计模式——模板方法模式

2020-12-29  本文已影响0人  rollingstarky

行为模式主要关注对象的响应性,处理对象之间的交互以实现更强大的功能。模板方法模式即为一种行为设计模式。
比如可以将制作饮料的步骤定义为模板方法中的算法,子类就能使用模板方法来实现沏茶的步骤。且步骤的改变(即子类的具体实现)并不会影响原始算法的结构。这样模板方法模式中的子类就可以通过覆盖来创建不同的行为。

模板方法模式适用于以下场景:

模板方法模式的主要意图:

UML
from abc import ABCMeta, abstractmethod

class Compiler(metaclass=ABCMeta):
    @abstractmethod
    def collectSource(self):
        pass

    @abstractmethod
    def compileToObject(self):
        pass

    @abstractmethod
    def run(self):
        pass

    def compileAndRun(self):
        self.collectSource()
        self.compileToObject()
        self.run()


class iOSCompiler(Compiler):
    def collectSource(self):
        print("Collecting Swift Source Code")

    def compileToObject(self):
        print("Compiling Swift code to LLVM bitcode")

    def run(self):
        print("Program runing on runtime environment")


iOS = iOSCompiler()
iOS.compileAndRun()
# => Collecting Swift Source Code
# => Compiling Swift code to LLVM bitcode
# => Program runing on runtime environment

现实中的模板方法模式

from abc import abstractmethod, ABCMeta

class Trip(metaclass=ABCMeta):
    @abstractmethod
    def setTransport(self):
        pass

    @abstractmethod
    def day1(self):
        pass

    @abstractmethod
    def day2(self):
        pass

    @abstractmethod
    def day3(self):
        pass

    @abstractmethod
    def returnHome(self):
        pass

    def itinerary(self):
        self.setTransport()
        self.day1()
        self.day2()
        self.day3()
        self.returnHome()


class VeniceTrip(Trip):
    def setTransport(self):
        print("Take a boat and find your way in the Grand Canal")

    def day1(self):
        print("Visit St Mark's Basilica in St Mark's Square")

    def day2(self):
        print("Appreciate Doge's Palace")

    def day3(self):
        print("Enjoy the food near the Rialto Bridge")

    def returnHome(self):
        print("Get souovenirs for friends and get back")


class MaldivesTrip(Trip):
    def setTransport(self):
        print("On foot, on any island, Wow!")

    def day1(self):
        print("Enjoy the marine life of Banana Reef")

    def day2(self):
        print("Go for the water sports and snorkelling")

    def day3(self):
        print("Relax on the beach and enjoy the sun")

    def returnHome(self):
        print("Don't feel like leaving the beach..")


class TravelAgency:
    def arrange_trip(self):
        choice = input("What kind of place you'd like to go historical or to a beach? ")
        if choice == 'historical':
            self.trip = VeniceTrip()
            self.trip.itinerary()
        if choice == 'beach':
            self.trip = MaldivesTrip()
            self.trip.itinerary()


TravelAgency().arrange_trip()

# => What kind of place you'd like to go historical or to a beach? beach
# => On foot, on any island, Wow!
# => Enjoy the marine life of Banana Reef
# => Go for the water sports and snorkelling
# => Relax on the beach and enjoy the sun
# => Don't feel like leaving the beach..

模板方法的优点和缺点

优点:

缺点:

上一篇 下一篇

猜你喜欢

热点阅读