1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| from abc import ABC, abstractmethod from typing import List from dataclasses import dataclass, field
@dataclass class Document: """产品类:文档""" title: str = "" content: str = "" footer: str = "" sections: List[str] = field(default_factory=list)
class DocumentBuilder(ABC): """Builder 抽象类"""
@abstractmethod def build_title(self, title: str) -> None: pass
@abstractmethod def build_content(self, content: str) -> None: pass
@abstractmethod def build_footer(self, footer: str) -> None: pass
@abstractmethod def build_section(self, section: str) -> None: pass
@abstractmethod def get_document(self) -> Document: pass
class HtmlDocumentBuilder(DocumentBuilder): """HTML 文档建造者"""
def __init__(self): self.document = Document()
def build_title(self, title: str) -> None: self.document.title = f"<h1>{title}</h1>"
def build_content(self, content: str) -> None: self.document.content = f"<p>{content}</p>"
def build_footer(self, footer: str) -> None: self.document.footer = f"<footer>{footer}</footer>"
def build_section(self, section: str) -> None: self.document.sections.append(f"<section>{section}</section>")
def get_document(self) -> Document: return self.document
class MarkdownDocumentBuilder(DocumentBuilder): """Markdown 文档建造者"""
def __init__(self): self.document = Document()
def build_title(self, title: str) -> None: self.document.title = f"# {title}"
def build_content(self, content: str) -> None: self.document.content = content
def build_footer(self, footer: str) -> None: self.document.footer = f"---\n{footer}"
def build_section(self, section: str) -> None: self.document.sections.append(f"## {section}")
def get_document(self) -> Document: return self.document
class DocumentDirector: """Director:指挥构建过程"""
def __init__(self, builder: DocumentBuilder): self.builder = builder
def set_builder(self, builder: DocumentBuilder) -> None: self.builder = builder
def construct_simple_document(self, title: str, content: str) -> Document: """构建简单文档""" self.builder.build_title(title) self.builder.build_content(content) self.builder.build_footer("Generated by Builder Pattern") return self.builder.get_document()
def construct_complex_document(self, title: str, content: str, sections: List[str]) -> Document: """构建复杂文档""" self.builder.build_title(title) self.builder.build_content(content) for section in sections: self.builder.build_section(section) self.builder.build_footer("© 2025 Builder Pattern Demo") return self.builder.get_document()
def main(): director = DocumentDirector(HtmlDocumentBuilder())
html_builder = HtmlDocumentBuilder() director.set_builder(html_builder) html_doc = director.construct_complex_document( "Builder Pattern", "This is a design pattern for building complex objects.", ["Introduction", "Implementation", "Examples"] ) print(f"HTML Document: {html_doc}")
md_builder = MarkdownDocumentBuilder() director.set_builder(md_builder) md_doc = director.construct_complex_document( "Builder Pattern", "This is a design pattern for building complex objects.", ["Introduction", "Implementation", "Examples"] ) print(f"Markdown Document: {md_doc}")
if __name__ == "__main__": main()
|