├── .gitignore ├── README.md ├── main.py └── patterns ├── abstract_factory ├── README.md ├── __pycache__ │ ├── abc_classes.cpython-312.pyc │ ├── classes_for_abs_factory.cpython-312.pyc │ ├── gui_abs_classes.cpython-312.pyc │ └── gui_clasesses.cpython-312.pyc ├── abc_classes.py ├── abs_factory_clone_v01.py ├── ads_factoty_clone.py ├── classes_for_abs_factory.py ├── gui_abs_classes.py ├── gui_clasesses.py └── window_button.py └── factory ├── README.md ├── __pycache__ ├── hero.cpython-312.pyc ├── hero_Kratos.cpython-312.pyc ├── hero_Tanos.cpython-312.pyc ├── hero_Wooki.cpython-312.pyc └── hero_Yoda.cpython-312.pyc ├── diagram_of_factory_pattern.png ├── hero.py ├── hero_Kratos.py ├── hero_Tanos.py ├── hero_Wooki.py ├── hero_Yoda.py └── my_factory_of_hero.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Patterns in Object-Oriented Programming (OOP): 2 | #### In OOP, a pattern is a reusable solution to a common problem or architectural challenge. They are not specific implementations but rather high-level descriptions that can be adapted to different contexts. 3 | 4 | Benefits of using patterns: 5 | 6 | Promote code reusability: Patterns provide a way to reuse proven solutions, reducing code duplication and development time. 7 | Improve code maintainability: Patterns often make code easier to understand and maintain due to their well-defined structure. 8 | Enhance communication: Using established patterns facilitates communication between developers as they share a common understanding of the solution. 9 | Types of patterns: 10 | 11 | There are many different OOP patterns, categorized into three main groups: 12 | 13 | Creational patterns: Focus on object creation mechanisms. Examples include: 14 | Singleton: Ensures a class has only one instance and provides a global access point to it. 15 | Factory Method: Provides an interface for creating objects without specifying the exact class. 16 | Structural patterns: Deal with the composition of classes and objects to achieve desired functionalities. Examples include: 17 | Adapter: Allows incompatible interfaces to work together. 18 | Decorator: Adds new functionalities to an object dynamically without altering its structure. 19 | Behavioral patterns: Describe communication and interaction between objects. Examples include: 20 | Observer: Defines a one-to-many dependency between objects, where changes in one object trigger updates in others. 21 | Strategy: Allows dynamic selection of an algorithm at runtime. 22 | Learning patterns: 23 | 24 | While there are many patterns, it's not necessary to memorize them all. Instead, understanding the core concepts and principles behind patterns enables you to identify opportunities to apply them in your code and learn new patterns as needed. 25 | 26 | Resources: 27 | 28 | "Design Patterns: Elements of Reusable Object-Oriented Software" by Erich Gamma et al. (also known as the "Gang of Four" book) 29 | https://refactoring.guru/ 30 | I hope this explanation provides a good starting point for understanding patterns in OOP. Feel free to ask if you have any further questions or would like to explore specific patterns in more detail. 31 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # This is a sample Python script. 2 | 3 | # Press Shift+F10 to execute it or replace it with your code. 4 | # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. 5 | 6 | 7 | def print_hi(name): 8 | # Use a breakpoint in the code line below to debug your script. 9 | ---------- print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. 10 | 11 | 12 | # Press the green button in the gutter to run the script. 13 | if __name__ == '__main__': 14 | print_hi('PyCharm') 15 | 16 | # See PyCharm help at https://www.jetbrains.com/help/pycharm/ 17 | -------------------------------------------------------------------------------- /patterns/abstract_factory/README.md: -------------------------------------------------------------------------------- 1 | # The ABS Factory method 2 | 3 | #### The ABC (Abstract Base Classes) Factory Method is a design pattern used in object-oriented programming (OOP) to create objects without specifying the exact class of object that will be created. It falls under the creational design patterns category. This pattern provides an interface for creating objects, but allows subclasses to alter the type of objects that will be created. 4 | 5 | Here's how the ABC Factory Method pattern typically works: 6 | 7 | Abstract Factory Interface: An abstract class or interface defines the method(s) to create objects. This interface does not provide the implementation of these methods, but only declares their signatures. 8 | 9 | Concrete Factories: Concrete classes implement the abstract factory interface. Each concrete factory provides a specific implementation for creating objects. 10 | 11 | Product Interface: An abstract class or interface defines the common interface for the objects created by the factory methods. 12 | 13 | Concrete Products: Concrete classes implement the product interface. These are the actual objects that are created by the factory methods. 14 | 15 | Client: The client code interacts with the factory methods through the abstract factory interface. It does not need to know the exact class of objects being created, only the factory interface. -------------------------------------------------------------------------------- /patterns/abstract_factory/__pycache__/abc_classes.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/abstract_factory/__pycache__/abc_classes.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/abstract_factory/__pycache__/classes_for_abs_factory.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/abstract_factory/__pycache__/classes_for_abs_factory.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/abstract_factory/__pycache__/gui_abs_classes.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/abstract_factory/__pycache__/gui_abs_classes.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/abstract_factory/__pycache__/gui_clasesses.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/abstract_factory/__pycache__/gui_clasesses.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/abstract_factory/abc_classes.py: -------------------------------------------------------------------------------- 1 | import abc 2 | 3 | 4 | class Window(abc.ABC): 5 | def __init__(self, title: str, width: int, height: int) -> None: 6 | self.title = title 7 | self.width = width 8 | self.height = height 9 | 10 | @abc.abstractmethod 11 | def draw(self) -> None: 12 | pass 13 | 14 | @abc.abstractmethod 15 | def set_size(self, width: int, height: int) -> None: 16 | pass 17 | 18 | def set_title(self, title: str) -> None: 19 | self.title = title 20 | 21 | @abc.abstractmethod 22 | def add(self, text_label) -> None: 23 | pass 24 | 25 | 26 | class TextLabel(abc.ABC): 27 | def __init__(self, text: str) -> None: 28 | self.text = text 29 | 30 | @abc.abstractmethod 31 | def set_text(self, text: str) -> None: 32 | pass 33 | 34 | 35 | class TextInput(abc.ABC): 36 | def __init__(self) -> None: 37 | self.text = "" 38 | 39 | @abc.abstractmethod 40 | def get_text(self) -> str: 41 | pass 42 | 43 | @abc.abstractmethod 44 | def set_text(self, text: str) -> None: 45 | pass 46 | 47 | 48 | class Button(abc.ABC): 49 | @abc.abstractmethod 50 | def click(self) -> None: 51 | pass 52 | 53 | 54 | -------------------------------------------------------------------------------- /patterns/abstract_factory/abs_factory_clone_v01.py: -------------------------------------------------------------------------------- 1 | import platform 2 | from gui_clasesses import WindowsGUI, GnomeGUI, MacGUI 3 | from gui_abs_classes import GUI 4 | 5 | 6 | def draw_greetings_screen(username: str, gui: GUI) -> None: 7 | window = gui.create_window(title="HELLO", width=100, height=100) 8 | text_label = gui.create_text_label(text=f"Hi, {username}! Welcome to the new project",) 9 | window.add(text_label) 10 | window.draw() 11 | 12 | 13 | def main(): 14 | os = platform.system() 15 | if os == "Windows": 16 | gui = WindowsGUI() 17 | elif os == "Linux": 18 | gui = GnomeGUI() 19 | elif os == "Darwin": 20 | gui = MacGUI() 21 | else: 22 | raise NotImplementedError(f"GUI for '{os}' is not implemented.") 23 | draw_greetings_screen(os, gui) 24 | 25 | 26 | if __name__ == "__main__": 27 | main() 28 | 29 | 30 | -------------------------------------------------------------------------------- /patterns/abstract_factory/ads_factoty_clone.py: -------------------------------------------------------------------------------- 1 | #cross-platform GUI app 2 | 3 | import abc 4 | import platform 5 | from typing import Protocol 6 | 7 | 8 | class Window(Protocol): 9 | def __init__(self, title: str, width: int, height: int) -> None: 10 | self.title = title 11 | self.width = width 12 | self.height = height 13 | 14 | @abc.abstractmethod 15 | def draw(self) -> None: 16 | pass 17 | 18 | @abc.abstractmethod 19 | def set_size(self, width: int, height: int) -> None: 20 | pass 21 | 22 | def set_title(self, title: str) -> None: 23 | self.title = title 24 | 25 | def add(self, text_label): 26 | pass 27 | 28 | 29 | class TextLabel(Protocol): 30 | def __init__(self, text: str) -> None: 31 | self.text = text 32 | 33 | @abc.abstractmethod 34 | def set_text(self, text: str) -> None: 35 | self.text = text 36 | 37 | 38 | class TextInput(Protocol): 39 | def __init__(self) -> None: 40 | self.text = "" 41 | 42 | @abc.abstractmethod 43 | def get_text(self) -> str: 44 | return self.text 45 | 46 | @abc.abstractmethod 47 | def set_text(self, text: str) -> None: 48 | self.text = text 49 | 50 | 51 | class Button(Protocol): 52 | @abc.abstractmethod 53 | def click(self) -> None: 54 | pass 55 | 56 | 57 | class WindowsWindow(Window): 58 | def draw(self) -> None: 59 | print("Drawing Windows window...") 60 | 61 | def set_size(self, width: int, height: int) -> None: 62 | self.width = width 63 | self.height = height 64 | 65 | 66 | class WindowsTextLabel(TextLabel): 67 | def set_text(self, text: str) -> None: 68 | self.text = text 69 | 70 | 71 | class WindowsTextInput(TextInput): 72 | def get_text(self) -> str: 73 | return self.text 74 | 75 | def set_text(self, text: str) -> None: 76 | self.text = text 77 | 78 | 79 | class WindowsButton(Button): 80 | def click(self) -> None: 81 | print("Windows button clicked!") 82 | 83 | 84 | class GnomeWindow(Window): 85 | def draw(self) -> None: 86 | print("Drawing Gnome window...") 87 | 88 | def set_size(self, width: int, height: int) -> None: 89 | self.width = width 90 | self.height = height 91 | 92 | 93 | class GnomeTextLabel(TextLabel): 94 | def set_text(self, text: str) -> None: 95 | self.text = text 96 | 97 | 98 | class GnomeTextInput(TextInput): 99 | def get_text(self) -> str: 100 | return self.text 101 | 102 | def set_text(self, text: str) -> None: 103 | self.text = text 104 | 105 | 106 | class GnomeButton(Button): 107 | def click(self) -> None: 108 | print("Gnome button clicked!") 109 | 110 | 111 | class MacWindow(Window): 112 | def draw(self) -> None: 113 | print("Drawing Mac window...") 114 | 115 | def set_size(self, width: int, height: int) -> None: 116 | self.width = width 117 | self.height = height 118 | 119 | 120 | class MacTextLabel(TextLabel): 121 | def set_text(self, text: str) -> None: 122 | self.text = text 123 | 124 | 125 | class MacTextInput(TextInput): 126 | def get_text(self) -> str: 127 | return self.text 128 | 129 | def set_text(self, text: str) -> None: 130 | self.text = text 131 | 132 | 133 | class MacButton(Button): 134 | def click(self) -> None: 135 | print("Mac button clicked!") 136 | 137 | 138 | class GUI(Protocol): 139 | def create_window(self, title: str, width: int, height: int) -> Window: 140 | ... 141 | 142 | def create_text_label(self, text: str) -> TextLabel: 143 | ... 144 | 145 | def create_text_input(self, placeholder: str) -> TextInput: 146 | ... 147 | 148 | def create_button(self, text: str) -> Button: 149 | ... 150 | 151 | 152 | class WindowsGUI: 153 | def create_window(self, title: str, width: int, height: int) -> Window: 154 | """initializing window creation""" 155 | def create_text_label(self, text: str) -> TextLabel: 156 | """initializing the creation of a text label in Windows""" 157 | def create_text_input(self, placeholder: str) -> TextInput: 158 | """implementations and creation of an input field in Windows""" 159 | def create_button(self, text: str) -> Button: 160 | """creating a button in windows""" 161 | 162 | 163 | class GnomeGUI: 164 | def create_window(self, title: str, width: int, height: int) -> Window: 165 | """initializing Gnome creation""" 166 | def create_text_label(self, text: str) -> TextLabel: 167 | """initializing the creation of a text label in Gnome""" 168 | def create_text_input(self, placeholder: str) -> TextInput: 169 | """implementations and creation of an input field in Gnome""" 170 | def create_button(self, text: str) -> Button: 171 | """creating a button in Gnome""" 172 | 173 | 174 | class MacGUI: 175 | def create_window(self, title: str, width: int, height: int) -> Window: 176 | """initializing mac creation""" 177 | def create_text_label(self, text: str) -> TextLabel: 178 | """initializing the creation of a text label in Mac""" 179 | def create_text_input(self, placeholder: str) -> TextInput: 180 | """implementations and creation of an input field in Mac""" 181 | def create_button(self, text: str) -> Button: 182 | """creating a button in Mac""" 183 | 184 | 185 | def draw_greetings_screen(username: str, gui: GUI) -> None: 186 | window = gui.create_window(title="HELLOW", width=100, height=100) 187 | text_label = gui.create_text_label(text=f"Hi, {username}! Welcome to the new project",) 188 | window.add(text_label) 189 | window.draw() 190 | 191 | 192 | def __main__(): 193 | global gui 194 | os = platform.system() 195 | if os == "Windows": 196 | gui = WindowsGUI() 197 | elif os == "linux": 198 | gui = GnomeGUI() 199 | elif os == "Darwin": 200 | gui = MacGUI 201 | draw_greetings_screen(os.getlogin(), gui) 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | -------------------------------------------------------------------------------- /patterns/abstract_factory/classes_for_abs_factory.py: -------------------------------------------------------------------------------- 1 | from abc_classes import Window, TextInput, TextLabel, Button 2 | 3 | 4 | class WindowsWindow(Window): 5 | def draw(self) -> None: 6 | print("Drawing Windows window...") 7 | 8 | def set_size(self, width: int, height: int) -> None: 9 | self.width = width 10 | self.height = height 11 | 12 | def add(self, text_label) -> None: 13 | print("Adding Text Label to Windows Window...") 14 | 15 | 16 | class WindowsTextLabel(TextLabel): 17 | def set_text(self, text: str) -> None: 18 | self.text = text 19 | 20 | 21 | class WindowsTextInput(TextInput): 22 | def get_text(self) -> str: 23 | return self.text 24 | 25 | def set_text(self, text: str) -> None: 26 | self.text = text 27 | 28 | 29 | class WindowsButton(Button): 30 | def click(self) -> None: 31 | print("Windows button clicked!") 32 | 33 | 34 | class GnomeWindow(Window): 35 | def draw(self) -> None: 36 | print("Drawing Gnome window...") 37 | 38 | def set_size(self, width: int, height: int) -> None: 39 | self.width = width 40 | self.height = height 41 | 42 | def add(self, text_label) -> None: 43 | print("Adding Text Label to Gnome Window...") 44 | 45 | 46 | class GnomeTextLabel(TextLabel): 47 | def set_text(self, text: str) -> None: 48 | self.text = text 49 | 50 | 51 | class GnomeTextInput(TextInput): 52 | def get_text(self) -> str: 53 | return self.text 54 | 55 | def set_text(self, text: str) -> None: 56 | self.text = text 57 | 58 | 59 | class GnomeButton(Button): 60 | def click(self) -> None: 61 | print("Gnome button clicked!") 62 | 63 | 64 | class MacWindow(Window): 65 | def draw(self) -> None: 66 | print("Drawing Mac window...") 67 | 68 | def set_size(self, width: int, height: int) -> None: 69 | self.width = width 70 | self.height = height 71 | 72 | def add(self, text_label) -> None: 73 | print("Adding Text Label to Mac Window...") 74 | 75 | 76 | class MacTextLabel(TextLabel): 77 | def set_text(self, text: str) -> None: 78 | self.text = text 79 | 80 | 81 | class MacTextInput(TextInput): 82 | def get_text(self) -> str: 83 | return self.text 84 | 85 | def set_text(self, text: str) -> None: 86 | self.text = text 87 | 88 | 89 | class MacButton(Button): 90 | def click(self) -> None: 91 | print("Mac button clicked!") 92 | 93 | 94 | -------------------------------------------------------------------------------- /patterns/abstract_factory/gui_abs_classes.py: -------------------------------------------------------------------------------- 1 | import abc 2 | from abc_classes import Window, TextInput, TextLabel, Button 3 | 4 | 5 | class GUI(abc.ABC): 6 | @abc.abstractmethod 7 | def create_window(self, title: str, width: int, height: int) -> Window: 8 | pass 9 | 10 | @abc.abstractmethod 11 | def create_text_label(self, text: str) -> TextLabel: 12 | pass 13 | 14 | @abc.abstractmethod 15 | def create_text_input(self, placeholder: str) -> TextInput: 16 | pass 17 | 18 | @abc.abstractmethod 19 | def create_button(self, text: str) -> Button: 20 | pass 21 | 22 | 23 | -------------------------------------------------------------------------------- /patterns/abstract_factory/gui_clasesses.py: -------------------------------------------------------------------------------- 1 | from gui_abs_classes import GUI 2 | from abc_classes import TextInput, TextLabel, Button 3 | from classes_for_abs_factory import Window, WindowsWindow, WindowsButton, WindowsTextLabel, WindowsTextInput 4 | from classes_for_abs_factory import GnomeWindow, GnomeButton, GnomeTextLabel, GnomeTextInput 5 | from classes_for_abs_factory import MacWindow, MacButton, MacTextLabel, MacTextInput 6 | 7 | 8 | class WindowsGUI(GUI): 9 | def create_window(self, title: str, width: int, height: int) -> Window: 10 | return WindowsWindow(title, width, height) 11 | 12 | def create_text_label(self, text: str) -> TextLabel: 13 | return WindowsTextLabel(text) 14 | 15 | def create_text_input(self, placeholder: str) -> TextInput: 16 | return WindowsTextInput() 17 | 18 | def create_button(self, text: str) -> Button: 19 | return WindowsButton() 20 | 21 | 22 | class GnomeGUI(GUI): 23 | def create_window(self, title: str, width: int, height: int) -> Window: 24 | return GnomeWindow(title, width, height) 25 | 26 | def create_text_label(self, text: str) -> TextLabel: 27 | return GnomeTextLabel(text) 28 | 29 | def create_text_input(self, placeholder: str) -> TextInput: 30 | return GnomeTextInput() 31 | 32 | def create_button(self, text: str) -> Button: 33 | return GnomeButton() 34 | 35 | 36 | class MacGUI(GUI): 37 | def create_window(self, title: str, width: int, height: int) -> Window: 38 | return MacWindow(title, width, height) 39 | 40 | def create_text_label(self, text: str) -> TextLabel: 41 | return MacTextLabel(text) 42 | 43 | def create_text_input(self, placeholder: str) -> TextInput: 44 | return MacTextInput() 45 | 46 | def create_button(self, text: str) -> Button: 47 | return MacButton() 48 | 49 | 50 | -------------------------------------------------------------------------------- /patterns/abstract_factory/window_button.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import messagebox 3 | 4 | 5 | class CrossPlatformApp: 6 | def __init__(self, root): 7 | self.root = root 8 | self.root.title("Cross-Platform App") 9 | 10 | self.label = tk.Label(root, text="Hello") 11 | self.label.pack(pady=10) 12 | 13 | self.button = tk.Button(root, text="Click Me, FAST", command=self.show_message) 14 | self.button.pack(pady=30) 15 | 16 | def show_message(self): 17 | messagebox.showinfo("Message", "You clicked the button") 18 | 19 | 20 | def main(): 21 | root = tk.Tk() 22 | app = CrossPlatformApp(root) 23 | root.mainloop() 24 | 25 | 26 | if __name__ == "__main__": 27 | main() -------------------------------------------------------------------------------- /patterns/factory/README.md: -------------------------------------------------------------------------------- 1 | # The Factory Method 2 | 3 | #### The Factory Method is a creational design pattern. It provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. This flexibility allows for dynamic object creation based on different contexts or situations. 4 | 5 | Here are some key characteristics of the Factory Method: 6 | 7 | Defines an interface for creating objects: This interface establishes the steps involved in object creation, ensuring consistency and reusability. 8 | Delegates object creation to subclasses: Subclasses can override the factory method to specify the concrete class of object to be created, allowing for customization based on specific needs. 9 | Promotes loose coupling: By decoupling the creation of objects from their usage, the Factory Method reduces dependencies and improves code maintainability. 10 | Here are some common applications of the Factory Method: 11 | 12 | Creating different types of products: A food ordering system might use a Factory Method to create different types of pizza based on the chosen toppings. 13 | Generating documents: A document management system might use a Factory Method to create different types of documents (resumes, reports, etc.) based on user selection. 14 | Simulating different character types: A video game might use a Factory Method to create different types of enemies or NPCs based on the game level or player location. 15 | 16 | ![Image of your PNG](https://github.com/GLskill/Pattern-Method/blob/factory_pattern/patterns/factory/diagram_of_factory_pattern.png) 17 | 18 | -------------------------------------------------------------------------------- /patterns/factory/__pycache__/hero.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/factory/__pycache__/hero.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/factory/__pycache__/hero_Kratos.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/factory/__pycache__/hero_Kratos.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/factory/__pycache__/hero_Tanos.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/factory/__pycache__/hero_Tanos.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/factory/__pycache__/hero_Wooki.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/factory/__pycache__/hero_Wooki.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/factory/__pycache__/hero_Yoda.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/factory/__pycache__/hero_Yoda.cpython-312.pyc -------------------------------------------------------------------------------- /patterns/factory/diagram_of_factory_pattern.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GLskill/Pattern-Method/91e286e6190c65d9e5cf363ac84a35518191cf6b/patterns/factory/diagram_of_factory_pattern.png -------------------------------------------------------------------------------- /patterns/factory/hero.py: -------------------------------------------------------------------------------- 1 | class Hero: 2 | def __init__(self, race: str, clas: str, level: int, gender: str): 3 | self.race = race 4 | self.clas = clas 5 | self.level = level 6 | self.gender = gender 7 | 8 | def hero_say(self): 9 | print(f"HI, am {self.race} and {self.clas} and I am have a {self.level} level") 10 | 11 | def life_position(self): 12 | self.hero_say() 13 | print(f"And you can see that I am {self.gender}") 14 | 15 | -------------------------------------------------------------------------------- /patterns/factory/hero_Kratos.py: -------------------------------------------------------------------------------- 1 | from hero import Hero 2 | 3 | 4 | class Kratos(Hero): 5 | 6 | def __init__(self, race: str, clas: str, level: int, gender: str = "God of War"): 7 | super().__init__(race, clas, level, gender) 8 | 9 | -------------------------------------------------------------------------------- /patterns/factory/hero_Tanos.py: -------------------------------------------------------------------------------- 1 | from hero import Hero 2 | 3 | 4 | class Tanos(Hero): 5 | 6 | def __init__(self, race: str, clas: str, level: int, gender: str = "A supervillain"): 7 | super().__init__(race, clas, level, gender) 8 | 9 | -------------------------------------------------------------------------------- /patterns/factory/hero_Wooki.py: -------------------------------------------------------------------------------- 1 | from hero import Hero 2 | 3 | 4 | class Wooki(Hero): 5 | 6 | def __init__(self, race: str, clas: str, level: int, gender: str = "Do not jump to conclusions"): 7 | super().__init__(race, clas, level, gender) 8 | 9 | -------------------------------------------------------------------------------- /patterns/factory/hero_Yoda.py: -------------------------------------------------------------------------------- 1 | from hero import Hero 2 | 3 | 4 | class Yoda(Hero): 5 | 6 | def __init__(self, race: str, clas: str, level: int, gender: str = "The Jedi Master"): 7 | super().__init__(race, clas, level, gender) 8 | 9 | -------------------------------------------------------------------------------- /patterns/factory/my_factory_of_hero.py: -------------------------------------------------------------------------------- 1 | from hero import Hero 2 | from hero_Kratos import Kratos 3 | from hero_Tanos import Tanos 4 | from hero_Wooki import Wooki 5 | from hero_Yoda import Yoda 6 | 7 | 8 | class HeroFactory: 9 | 10 | def hero_by_type(self, hero_type: str): 11 | if hero_type == "Kratos": 12 | return Kratos(race="human", clas="warior", level=65) 13 | if hero_type == "Tanos": 14 | return Tanos(race="titan", clas="manipulator", level=99) 15 | if hero_type == "Wooki": 16 | return Wooki(race="beast", clas="archer", level=38) 17 | if hero_type == "Yoda": 18 | return Yoda(race="humanoid", clas="jedi master", level=105) 19 | else: 20 | print("There is no such hero in this universe") 21 | print("-----Choose another hero or another universe-----") 22 | return Hero(gender="null", race="null", clas="null", level=0) 23 | 24 | 25 | if __name__ == "__main__": 26 | 27 | hero_factory = HeroFactory() 28 | 29 | null_hero = hero_factory.hero_by_type("null") 30 | kratos_hero = hero_factory.hero_by_type("Kratos") 31 | tanos_hero = hero_factory.hero_by_type("Tanos") 32 | wooki_hero = hero_factory.hero_by_type("Wooki") 33 | yoda_hero = hero_factory.hero_by_type("Yoda") 34 | 35 | print("________________________________________________________") 36 | kratos_hero.life_position() 37 | print("________________________________________________________") 38 | tanos_hero.life_position() 39 | print("________________________________________________________") 40 | wooki_hero.life_position() 41 | print("________________________________________________________") 42 | yoda_hero.life_position() 43 | print("________________________________________________________") 44 | null_hero.life_position() 45 | print("________________________________________________________") 46 | 47 | --------------------------------------------------------------------------------