mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from abc import ABC, abstractmethod
|
|
|
|
|
|
class ICreator(ABC):
|
|
"""
|
|
The Creator class declares the factory method that is supposed to return an
|
|
object of a Product class. The Creator's subclasses usually provide the
|
|
implementation of this method.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def factory_method(self):
|
|
"""
|
|
Note that the Creator may also provide some default implementation of
|
|
the factory method.
|
|
"""
|
|
pass
|
|
|
|
def some_operation(self) -> str:
|
|
"""
|
|
Also note that, despite its name, the Creator's primary responsibility
|
|
is not creating products. Usually, it contains some core business logic
|
|
that relies on Product objects, returned by the factory method.
|
|
Subclasses can indirectly change that business logic by overriding the
|
|
factory method and returning a different type of product from it.
|
|
"""
|
|
|
|
# Call the factory_method to create a Product object
|
|
product = self.factory_method()
|
|
|
|
# Now, use the product.
|
|
result = f"""
|
|
Creator: The same creator's code has just worked with
|
|
{product.operation()}
|
|
"""
|
|
|
|
return result
|