Hope this page could elucidate this pattern in a bit explicit manner.

Pattern overview

Pattern Name and Classification

A descriptive and unique name that helps in identifying and referring to the pattern.

Intent

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Also Known As

kit

Motivation (Forces)

A scenario consisting of a problem and a context in which this pattern can be used.

In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client does not know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products.

Applicability

Situations in which this pattern is usable; the context for the pattern.

Structure

A graphical representation of the pattern. Class diagrams and Interaction diagrams may be used for this purpose.

Participants

A listing of the classes and objects used in the pattern and their roles in the design.

Collaboration

A description of how classes and objects used in the pattern interact with each other.

Implementation

A description of an implementation of the pattern; the solution part of the pattern.

Consequences

A description of the results, side effects, and trade offs caused by using the pattern.

Known Uses

Examples of real usages of the pattern.

Related Patterns

Other patterns that have some relationship with the pattern; discussion of the differences between the pattern and similar patterns.

Samples

For educational purposes it would be priceless to look at the working code sample. Exactly to achieve a stunning clarity it might be helpful to compose a test suite for testing the pattern features and dispose it nearby the pattern source code.

Pet factory

Creates some particular animal out from arbitrary number preliminary defined pets.

Pattern code Test suite
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A pet shop abstraction
"""


class Dog(object):
    """One of the objects to be returned"""

    def speak(self):
        return "Woof!"

    def __str__(self):
        return "Dog"


class DogFactory(object):
    """Concrete Factory"""

    def get_pet(self):
        """Returns a Dog object"""
        return Dog()

    def get_food(self):
        """Returns a Dog Food object"""
        return "Dog Food!"


class PetStore(object):
    """ PetStore houses our Abstract Factory """

    def __init__(self, pet_factory=None):
        """ pet_factory is our Abstract Factory """

        self._pet_factory = pet_factory

    def show_pet(self):
        """ Utility method to display the details of the objects retured by the DogFactory """

        pet = self._pet_factory.get_pet()
        pet_food = self._pet_factory.get_food()

        print("Our pet is '{}'!".format(pet))
        print("Our pet says hello by '{}'".format(pet.speak()))
        print("Its food is '{}'!".format(pet_food))


# Create a Concrete Factory
factory = DogFactory()

# Create a pet store housing our Abstract Factory
shop = PetStore(factory)

# Invoke the utility method to show the details of our pet
shop.show_pet()
Our pet is 'Dog'!
Our pet says hello by 'Woof!'
Its food is 'Dog Food!'!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
An attempt to illustrate how pet shop works
"""
import unittest


class TestSimpleBehavior(unittest.TestCase):

    def setUp(self):
        pass

    def test_primitive_TravisCI(self):
        self.assertTrue(3 + 1 == 4)

    def tearDown(self):
        pass


blog comments powered by Disqus

Published

15 March 2017

Categories

literate programming emacs Python3

Tags