Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Abstract Factory Design Pattern in Python

Abstract factory pattern design pattern explained in simple words with Python code example.

Ankush Kumar Singh
Python in Plain English
3 min readJan 2, 2022

--

The name abstract factory pattern sounds sublime and artistic, but it is one of the simplest patterns that I have come across. In this blog, we will discuss what is an abstract factory design pattern, its implementation in Python and when to use it.

Abstract factory pattern is a continuation of the Factory method design pattern. If you don’t know about the factory method design pattern then I highly recommend reading about it before proceeding with this blog.

By definition, abstract factory design pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. In other words, an abstract factory pattern is a collection of different factories.

To illustrate, let us assume that we are building a mobile app that users can use to share photos. This app will be supported in Android, iOS, and Symbian (10 points to Gryffindor if you have used this mobile OS) platforms. The app comes with 2 features:

  1. Users can create stories
  2. Users can upload photos

Following is the UML diagram for this,

As we can see in the UML diagram above, interface AbstractMobileUIFactory is implemented by AndroidFactory, IosFactory, and SymbianFactory. As per the definition of the abstract factory, AbstractMobileUIFactory provides interfaces create_story and upload_photos without specifying their concrete classes. Depending on the platform type, a specific factory returns the concrete class object for creating story and upload photo functions.

On the right side of the UML, we have the interfaces AbstractCreateStory and AbstractUploadPhoto to create story and upload photos respectively. Interface AbstractCreateStory is implemented by AndroidCreateStory, IosCreateStory and SymbianCreateStory for specific platforms. While interface AbstractUploadPhoto is implemented by AndroidUploadPhoto, IosUploadPhoto, and SymbianUploadPhoto.

Following is the code snippet for this:

Sample output for the code:

Running for Android platform...
Inside AndroidFactory class
[Android] Creating story on android platform.
Inside AndroidFactory class
[Android] Uploading photo on android platform.
Running for IOS platform...
Inside IosFactory class
[IOS] Creating story on IOS platform.
Inside IosFactory class
[IOS] Uploading photo on IOS platform.
Running for Symbian platform...
Inside SymbianFactory class
[Symbian] Ok boomer! Creating story on Symbian platform, while we can!!
Inside SymbianFactory class
[Symbian] So you want us to upload petroglyph? Since we are backward compatible, why not! Uploading photo on Symbian platform

As we can see in the code, the get_factory function in class Application takes platform type as an argument and returns the factory object of the platform type. Returned factory object is used to create story and upload photos.

Abstract factory pattern can be used when code needs to work with various families of related products without being dependent on the concrete classes of these products.

I hope you found this blog useful and I hope you had a great time reading it!

Following are the links to other design pattern articles:

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response