python中工厂方法实现demo

工厂方法实现放在factory_method.py模块中:
class BenBen:
    def __init__(self, money):
        """
        奔奔支付
        :param money:支付金额
        """
        self.money = money

    def pay(self):
        print("收到奔奔金融支付金额{0}".format(self.money))


class WeChat:
    def __init__(self, money):
        """
        微信支付
        :param money:支付金额 
        """
        self.money = money

    def pay(self):
        print("收到微信支付金额{0}".format(self.money))


class ZhiFuBao:
    def __init__(self, money):
        """
        支付宝支付
        :param money:支付金额 
        """
        self.money = money

    def pay(self):
        print("收到支付宝支付金额{0}".format(self.money))


class BenBenFactory:
    
    def create(self, money):
        """
        奔奔支付工厂
        :param money:支付金额 
        :return: 
        """
        return BenBen(money)


class ZhiFuBaoFactory:

    def create(self, money):
        """
        支付宝支付工厂
        :param money:支付金额 
        :return: 
        """
        return ZhiFuBao(money)


class WeChatFactory:

    def create(self, money):
        """
        微信支付工厂
        :param money:支付金额 
        :return: 
        """
        return WeChat(money)

调用工厂方法:

from factory_method import BenBenFactory, WeChatFactory, ZhiFuBaoFactory


facotry = BenBenFactory()
benben = facotry.create(100)
benben.pay()

facotry = WeChatFactory()
we_chat = facotry.create(200)
we_chat.pay()
#
facotry = ZhiFuBaoFactory()
zhi_fu_bao = facotry.create(300)
zhi_fu_bao.pay()
posted @ 2020-01-06 20:19  奔奔-武  阅读(347)  评论(0编辑  收藏  举报