Creational Design Patterns
Creational Design Patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
1. Builder
Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
HTMLComponent
Represents a component of HTML code.
Source code in src/creational/builder.py
__init__(content)
Initializes an HTMLComponent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content |
str
|
The content of the HTML component. |
required |
render()
Renders the HTML component.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The HTML code representation of the component. |
CSSComponent
Represents a component of CSS code.
Source code in src/creational/builder.py
__init__(style)
Initializes a CSSComponent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style |
str
|
The CSS style of the component. |
required |
render()
Renders the CSS component.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The CSS code representation of the component. |
CodeBuilder
Builds HTML and CSS code by assembling components.
Source code in src/creational/builder.py
__init__()
add_html(content)
Adds an HTML component to the builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content |
str
|
The content of the HTML component. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CodeBuilder |
CodeBuilder
|
The updated CodeBuilder instance. |
Source code in src/creational/builder.py
add_css(style)
Adds a CSS component to the builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style |
str
|
The CSS style of the component. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CodeBuilder |
CodeBuilder
|
The updated CodeBuilder instance. |
Source code in src/creational/builder.py
build_html()
Builds the HTML code from added HTML components.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated HTML code. |
build_css()
Builds the CSS code from added CSS components.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated CSS code. |
main()
Steps to implement:
1. Create Code Builder's Instance
2. Add HTML and CSS code
3. Finally build the HTML & CSS code using Code Builder's Instance
Source code in src/creational/builder.py
Source Code
Another Example
| src/creational/builder_example.py | |
|---|---|
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 | |
Class Diagram
classDiagram
class Button {
display()
}
class TextField {
display()
}
class Checkbox {
display()
}
class DesignTool {
create_button()
create_text_field()
create_checkbox()
}
class Figma {
create_button()
create_text_field()
create_checkbox()
render_design()
}
class Designer {
build_basic_ui()
}
class UI {
TextField
Checkbox
Button
}
Button <|-- Figma
TextField <|-- Figma
Checkbox <|-- Figma
DesignTool --|> Figma
DesignTool <|-- Designer
Button --|> UI
TextField --|> UI
Checkbox --|> UI