Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Unlocked

You're reading from   Python Unlocked Become more fluent in Python—learn strategies and techniques for smart and high-performance Python programming

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785885990
Length 172 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Arun Tigeraniya Arun Tigeraniya
Author Profile Icon Arun Tigeraniya
Arun Tigeraniya
Arrow right icon
View More author details
Toc

Abstract factory


This design pattern creates an interface to create a family of interrelated objects without specifying their concrete class. It is similar to a superfactory. Its advantage is that we can add further variants, and clients will not have to worry further about the interface or actual classes for the new variants. It is helpful in supporting various platforms, windowing systems, data types, and so on. In the following example, the Animal class is the interface that the client will know about for any animal instance. AnimalFactory is the abstract factory that DogFactory and CatFactory implement. Now, on the runtime by user input, or configuration file, or runtime environment check, we can decide whether we will have all Dog or Cat instances. It is very convenient to add a new class implementation, as follows:

import os
import abc
import six


class Animal(six.with_metaclass(abc.ABCMeta, object)):
    """ clients only need to know this interface for animals"""
    @abc.abstractmethod...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image