Photo by Taryn Elliott on Pexels

DESIGN PATTERNS IN JAVA SERIES

Design Patterns in Java/Android — Factory

Explanations, Caveats, Examples from Android Framework & Java Libraries, Writing our own Factory

Published in
4 min readMar 13, 2022

--

This series on Software Design Patterns will introduce some common design patterns that we use in app development.

  1. Singleton
  2. Builder
  3. Factory ← we are here
  4. Prototype
  5. Adapter
  6. Facade
  7. Observer
  8. Command

Here, we will take examples of existing design patterns in Android framework & Java Libraries and also write our own patterns in Java.

Introduction to Design Patterns

Software Design Patterns are time-tested, reusable solutions to recurring software problems. They define a common language that helps our team communicate more efficiently.

Design patterns usually deals with objects. The patterns differ by their complexity, reusability, purpose and scale of application in the system. All patterns an be categorized mainly into 3 categories based on their purpose:

  • Creational patterns: How the objects are created.
    Eg. Singleton pattern, Builder pattern, Factory pattern
  • Structural patterns: How the objects are composed (assembling objects into larger structures).
    Eg. Adapter pattern, Facade pattern, Decorator pattern
  • Behavioral patterns: How the objects coordinate (communication and assignment of responsibilities between objects).
    Eg. Observer pattern, Strategy pattern, Command pattern

Factory Design Patterns

The Factory is a common creational design pattern. And most of us already know that there are 3 common types of Factory design patterns — Factory, Factory Method and Abstract Factory. All these patterns helps to create new objects without exposing the creation logic to client. All these patterns have a factory class responsible for creating new objects.

Different types of transports (Trucks & Boats) are being created using Factory Pattern (Image from refactoring.guru)

Examples of Factory from Android Framework & Java Libraries

In a standard Android app, many times we may need to create multiple objects without specifying the exact class of object that will be created. Factory pattern can be used when we might not want to specify a concrete class while dealing with many common objects. This is done by implementing a common interface in classes and providing access to the class objects through a Factory method.

Examples of Factory Method pattern in Android Framework and Libraries are: Calendar getInstance(), NumberFormat getInstance(), ResourceBundle getBundle(), Integer valueOf(), Boolean valueOf(), Charset forName(), URL openConnection(), Runtime getRuntime(), ViewModelProvider.Factory, etc.

Calendar getInstance()

Calendar getInstance() is an example from Java 7

NumberFormat getInstance()

NumberFormat getInstance() is an example from Java 7

ResourceBundle getBundle()

ResourceBundle getBundle() is an example from Java 7

Writing our own Factory in Java

Now, let us look at basic construction of all 3 factory design patterns. Let us suppose we want to write factories for “Cars” and “Trucks” creation.

Factory

In Factory, we have just one implementation with no sub-classing.

Factory Method

Factory Method provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

We use Factory method, when we have some generic processing in a class, but want to vary in which vehicle we actually create.

Abstract Factory

We use Abstract factory generally for things like dependency injection or strategy pattern — when we want to be able to create a whole family of objects that need to be of “the same kind”, and have some common base classes.

The use case here is that we want to make sure that we don’t accidentally use an “TruckSelector” on a “Car”. As long as we get our “Vehicle” and “Selector” from the same factory, they will match.

Writing Factory for Android ViewModel

Our ViewModel Class

Our Factory Class

Getting the ViewModel in Activity/Fragment

This article is supposed to serve as a beginner’s guide for the above design pattern.

Give a clap if you liked the article or a leave a comment for any suggestion/discussion/dispute. Thank you!

--

--

Android Developer (Kotlin, Java, OOPS, Data Structures, Algorithms, Design Patterns) github.com/Suryakant-Bharti