forked from Yetiowner/Increasing-code-complexity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helloworld_builder.py
39 lines (34 loc) · 1.13 KB
/
helloworld_builder.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
from __future__ import annotations
from typing import List
from singleton import Singleton
@Singleton
class HelloWorldBuilder:
"""
A class that follows the builder pattern, designed to assemble a hello world string
"""
def __init__(self):
"""
Instantiates the builder class, creating an empty list for characters
"""
self._chars: List[str] = []
def add_char(self, c: str) -> HelloWorldBuilder:
"""
Will append a new character to the builder's internal list of characters
:rtype: HelloWorldBuilder
:param c: The character to add
:type c: str
:return: Returns the builder
"""
if type(c) != str:
raise TypeError("Character provided must be of type <class='str'>")
char = c[0]
self._chars.append(char)
return self
def build(self) -> str:
"""
Builds the internal list of characters into a readable string
:rtype: str
:return: Returns the built string
"""
built_string = ''.join(self._chars)
return built_string