Metodi factory

Programmazione a oggetti intermedia in Python

Jake Roach

Data Engineer

Logica condizionale in un metodo

class Student:
    # Logica condizionale con comportamento simile per tutti i casi
    def explore_topic(self, resource_type, topic):
        if resource_type == "Textbook":
            print(f"Referencing {topic} using a textbook")
            self.reference_textbook(topic)

        elif resource_type == "Blog":
            print(f"Make sure to validate information provided in blogs!")
            self.reference_blog(topic)

        elif resource_type == "Video":
            self.reference_video(topic)
Programmazione a oggetti intermedia in Python

Metodi factory

Design pattern che usa metodi factory per creare oggetti usati in un altro metodo

  • Restituisce oggetti che implementano un'interfaccia
  • Riduce la complessità in un metodo
  • Riutilizzabile, modulare
  • _ per indicare un metodo factory

$$

Per prima cosa, vediamo prodotti e prodotti concreti!

...

  def _get_resource(self, resource_type):  
    if resource_type == "Textbook":
      # Textbook, Blog and Video are
      # all resources
      return Textbook()  

    elif resource_type == "Blog":
      return Blog()  

    elif resource_type == "Video":
      return Video()
Programmazione a oggetti intermedia in Python

Creare un'interfaccia Resource

class Resource(ABC):
    @abstractmethod
    def reference(self, topic):
        pass
class Textbook(Resource):
    def __init__(self):
        self.index = {"Object-oriented Programming": ["Inheritance", ...]}

    def reference(self, topic):
        print(f"Referencing {topic} using a textbook")
        return self.index.get(topic)

# Qualcosa di simile per Blog, Video
Programmazione a oggetti intermedia in Python

Rivedere explore_topic()

class Student:

    def explore_topic(self, resource_type, topic):
        if resource_type == "Textbook":
            texbook = Textbook()  # Creazione di un'istanza di una classe che implementa Resource
            texbook.reference(topic)  # Chiama il metodo reference()

        elif resource_type == "Blog":
            blog = Blog()
            blog.reference(topic)

        elif resource_type == "Video":
            video = Video()
            video.reference(topic)
Programmazione a oggetti intermedia in Python

Creare un metodo factory

class Student:
    # Metodo factory che restituisce Resource
    def _get_resource(self, resource_type):
        if resource_type == "Textbook":
            return Textbook()  
        elif resource_type == "Blog":
            return Blog()  
        elif resource_type == "Video":
            return Video()

    def explore_topic(self, resource_type, topic):
        resource = self._get_resource(resource_type)  # Recupera la risorsa
        return resource.reference(topic)
Programmazione a oggetti intermedia in Python

Usare i metodi factory

# Crea un oggetto Student, poi fagli consultare un textbook
lester = Student()
lester.explore_topic("Textbook", "Object-oriented Programming")

# Facile passare a un'altra risorsa
lester.explore_topic("Video", "Object-oriented Programming")
Referencing Object-oriented Programming using a textbook
["Inheritance", "Constructors", "Class-level methods"]

Video are helpful for visual or audatory learners
["Classes", "Methods", "Attributes", "self"]
Programmazione a oggetti intermedia in Python

Esercitiamoci!

Programmazione a oggetti intermedia in Python

Preparing Video For Download...