Python-Design-Patterns abstract_factory

  1. abstract_factory Model
  2. Python-Design-Patterns abstract_factory
  3. abstract_factory Test

abstract_factory Model

Python-Design-Patterns abstract_factory

# file: ''
#!/usr/bin/env python
# -*- coding: utf-8 -*-

*What is this pattern about?

In Java and other languages, the Abstract Factory Pattern serves to provide an interface for
creating related/dependent objects without need to specify their
actual class.

The idea is to abstract the creation of objects depending on business
logic, platform choice, etc.

In Python, the interface we use is simply a callable, which is "builtin" interface
in Python, and in normal circumstances we can simply use the class itself as
that callable, because classes are first class objects in Python.

*What does this example do?
This particular implementation abstracts the creation of a pet and
does so depending on the factory we chose (Dog or Cat, or random_animal)
This works because both Dog/Cat and random_animal respect a common
interface (callable for creation and .speak()).
Now my application can create pets abstractly and decide later,
based on my own criteria, dogs over cats.

*Where is the pattern used practically?


Provides a way to encapsulate a group of individual factories.

import random

class PetShop(object):

    """A pet shop"""

    def __init__(self, animal_factory=None):
        """pet_factory is our abstract factory.  We can set it at will."""

        self.pet_factory = animal_factory

    def show_pet(self):
        """Creates and shows a pet using the abstract factory"""

        pet = self.pet_factory()
        print("We have a lovely {}".format(pet))
        print("It says {}".format(pet.speak()))

class Dog(object):
    def speak(self):
        return "woof"

    def __str__(self):
        return "Dog"

class Cat(object):
    def speak(self):
        return "meow"

    def __str__(self):
        return "Cat"

# Additional factories:

# Create a random animal
def random_animal():
    """Let's be dynamic!"""
    return random.choice([Dog, Cat])()

# Show pets with various factories
if __name__ == "__main__":

    # A Shop that sells only cats
    cat_shop = PetShop(Cat)

    # A shop that sells random animals
    shop = PetShop(random_animal)
    for i in range(3):
        print("=" * 20)

### OUTPUT ###
# We have a lovely Cat
# It says meow
# We have a lovely Dog
# It says woof
# ====================
# We have a lovely Cat
# It says meow
# ====================
# We have a lovely Cat
# It says meow
# ====================

abstract_factory Test

# file: ''
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from creational.abstract_factory import PetShop, Dog

    from unittest.mock import patch
except ImportError:
    from mock import patch

class TestPetShop(unittest.TestCase):
    def test_dog_pet_shop_shall_show_dog_instance(self):
        dog_pet_shop = PetShop(Dog)
        with patch.object(Dog, 'speak') as mock_Dog_speak:
            self.assertEqual(mock_Dog_speak.call_count, 1)

Comments 💬