0% found this document useful (0 votes)
5 views2 pages

Factory Method Output

Uploaded by

Sudais Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Factory Method Output

Uploaded by

Sudais Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Code:

from abc import ABC, abstractmethod

# Product Interface
class Button(ABC):
@abstractmethod
def render(self):
pass

# Concrete Product 1
class WindowsButton(Button):
def render(self):
return "Rendering a Windows button."

# Concrete Product 2
class HTMLButton(Button):
def render(self):
return "Rendering an HTML button."

# Creator Interface
class Dialog(ABC):
@abstractmethod
def create_button(self) -> Button:
pass

def render_window(self):
button = self.create_button()
return button.render()

# Concrete Creator 1
class WindowsDialog(Dialog):
def create_button(self) -> Button:
return WindowsButton()

# Concrete Creator 2
class WebDialog(Dialog):
def create_button(self) -> Button:
return HTMLButton()

# Client Code
def client_code(dialog: Dialog):
result = dialog.render_window()
return result

# Test the factory method


windows_dialog = WindowsDialog()
web_dialog = WebDialog()

output1 = client_code(windows_dialog)
output2 = client_code(web_dialog)

output1, output2
Output:

Output 1: Rendering a Windows button.

Output 2: Rendering an HTML button.

You might also like