Python 设计模式之原型模式

2019-11-25 / Java Python Design Pattern

意图: 创建一些类的模型,比如说模版之类的东西。

适用性: 当类里面要创建的结构具有一定结构,但是内容会有所不同的时候。比如:电子账单、SMS 短信、Email等。

prototype

# coding:utf-8


class EventTemplate(object):
    event_subject = ''
    event_content = ''

    def __init__(self, event_subject, event_content):
        self.event_subject = event_subject
        self.event_content = event_content


class Mail(object):
    receiver = ''
    subject = ''
    content = ''
    tail = ''

    def __init__(self, event_template: EventTemplate):
        self.tail = event_template.event_content
        self.subject = event_template.event_subject


def send_mail(mail: Mail):
    mail.content = '%s,先生(女士):你的信用卡账单...' % mail.receiver
    print(mail.receiver)
    print(mail.subject)
    print(mail.content)


def main():
    et = EventTemplate("9月份信用卡账单", "国庆抽奖活动...")
    mail = Mail(et)
    mail.receiver = 'Del Cooper'
    send_mail(mail)


if __name__ == '__main__':
    main()