Created
September 13, 2011 15:34
-
-
Save alecmunro/1214125 to your computer and use it in GitHub Desktop.
Template Method example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEFAULT_EMAIL_TYPE = "text" | |
DEFAULT_RECIPIENT = "alecmunro@gmale.com" | |
DEFAULT_SUBJECT = "Good news, everyone!" | |
class EmailReporter(object): | |
email_type = DEFAULT_EMAIL_TYPE | |
def send_report(self, settings): | |
data = self.collect_data() | |
body = self.create_body(data) | |
self.send_email(data["recipient"], settings["sender"], | |
self.email_type, data["subject"], body) | |
def send_email(self, to, from, subject, body, | |
type=DEFAULT_EMAIL_TYPE): | |
... | |
def collect_data(self) | |
return {"recipient": DEFAULT_RECIPIENT, | |
"subject": DEFAULT_SUBJECT} | |
def create_body(self, data): | |
return "Bad news, everyone." | |
class HTMLEmailReporter(EmailReporter): | |
email_type = "html" | |
def collect_data(self): | |
base_data = super(HTMLEmailReporter, self)\ | |
.collect_data() | |
base_data.update({ | |
"analysis": "complete", | |
"recommendation": "immediate termination" | |
} | |
return base_data | |
def create_body(self, data): | |
return """<html><body> | |
Our analysis process is <em>{analysis}</em>.<br /> | |
Based on this, we are recommending {recommendation} | |
of the project.</body></html>""".format(**data) | |
class TextEmailReporter(EmailReporter): | |
def collect_data(self): | |
base_data = super(TextEmailReporter, self)\ | |
.collect_data() | |
base_data.update({ | |
"analysis": "complete", | |
"steps": ["Buy Ferret Ranch", "???", "Profit!"] | |
} | |
return base_data | |
def create_body(self, data): | |
body = "Our analysis process is {analysis}.\n"\ | |
"We determined that the process was as follows:\n"\ | |
.format(**data) | |
for i, step in enumerate(data["steps"]): | |
body += "{0}. {1}".format(i+1, step) | |
return body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment