You are on page 1of 12

31/03/2024, 11:55 Abstract Factory Pattern.

Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

Open in app Sign up Sign in

Search

Abstract Factory Pattern


Budhdi Sharma · Follow
Published in AndroidPub
3 min read · Dec 11, 2019

Listen Share

Abstract Factory is a creational design pattern, which solves the problem of creating
entire product families without specifying their concrete classes.

Abstract Factory defines an interface for creating all distinct products but leaves the
actual product creation to concrete factory classes. Each factory type corresponds to
a certain product variety.

The client code calls the creation methods of a factory object instead of creating
products directly with a constructor call ( new operator). Since a factory corresponds
to a single product variant, all its products will be compatible.

Client code works with factories and products only through their abstract interfaces.
It allows the same client code to work with different products. You just create a new
concrete factory class and pass it to the client code.

Define an interface or abstract class for creating families of related (or dependent)
objects but without specifying their concrete sub-classes.
That means Abstract Factory lets a class returns a factory of classes. This is why?
Abstract Factory Pattern is one level higher than the Factory Pattern.
An Abstract Factory Pattern is also known as Kit.

Advantage

https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 1/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

Isolates the client code from concrete (implementation) classes.

It eases the exchanging of object families.

It promotes consistency among objects.

Usage

when System needs to be independent of how its object is created, composed,


and represented.

When the family of related objects has to be used together, then this constraint
needs to be enforced.

When you want to provide a library of objects that do not show implementations
and only reveal interfaces.

When the system needs to be configured with one of a multiple-family of


objects.

Example

package com.budh.abstractfactory.example;

public interface Fruits {

public String getFruitsName();


public String getFruitType();

}
********************************************************************

package com.budh.abstractfactory.example;

public interface FruitsFactory {

public Fruits getFruitDetail();

}
https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 2/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

********************************************************************
package com.budh.abstractfactory.example;

public class ConsumeFruitFactory {

public ConsumeFruitFactory(FruitsFactory fruitFactory) {


if(fruitFactory != null) {
Fruits mfruit = fruitFactory.getFruitDetail();
System.out.println(mfruit.getFruitsName());
System.out.println(mfruit.getFruitType());
}else {
System.out.println("No such a Fruit. Please add it.");
}

}
********************************************************************

package com.budh.abstractfactory.example;

public class AbstractFactoryClient {

public static void main(String[] args) {

new ConsumeFruitFactory(getFruitDetail("Orange"));
}

public static FruitsFactory getFruitDetail(String type) {


if("Orange".equalsIgnoreCase(type)) {
return new OrangeFactory();
}else if("Mango".equalsIgnoreCase(type)) {
return new MangoFactory();
}else if("Apple".equalsIgnoreCase(type)) {
return new AppleFactory();
}else {
return null;
}
}
}

********************************************************************
package com.budh.abstractfactory.example;

public class Apple implements Fruits {

@Override
public String getFruitsName() {

return "Apple";
}

@Override
public String getFruitType() {
https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 3/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

return "An apple a day";


}

Disadvantage

Difficult to support new kinds of products: Extending abstract factories to


produce new kinds of Products isn’t easy. That’s because the AbstractFactory
interface fixes the set of products that can be created. Supporting new kinds of
products requires extending the factory interface, which involves changing the
AbstractFactory class and all of its subclasses.

Difference between factory and abstract factory

The main difference between a “factory method” and an “abstract factory” is


that the factory method is a single method, and an abstract factory is an object.

The factory method is just a method, it can be overridden in a subclass, whereas


the abstract factory is an object that has multiple factory methods on it.

The Factory Method pattern uses inheritance and relies on a subclass to handle
the desired object instantiation.

Java Design Patterns Android Design Patterns Abstract Factory Pattern

Basic Concept

https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 4/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

Follow

Written by Budhdi Sharma


485 Followers · Writer for AndroidPub

As an AOSP developer, I specialize in creating robust framework and system applications that seamlessly
integrate with embedded systems on various SOCs

More from Budhdi Sharma and AndroidPub

Budhdi Sharma

AIDL and it’s uses in android


AIDL (Android Interface Definition Language) is similar to other IDLs.

9 min read · Dec 1, 2019

169 3

https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 5/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

Ankit Sinhal in AndroidPub

Android Activity Launch Mode


Launch mode is an instruction for Android OS which specifies how the activity should be
launched. It instructs how any new activity should…

5 min read · Jan 14, 2017

3.3K 19

Anitaa Murthy in AndroidPub

Android Interview Questions Cheat Sheet — Part I


https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 6/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

A set of questions that I have accumulated over the years in preparation for my many Android
Interviews Prep.

29 min read · May 30, 2018

15.3K 71

Budhdi Sharma in AndroidPub

System App In Android


Android’s underlying kernel is based on Linux, but it has been customized to suit Google’s
directions. There is no support for the GNU…

7 min read · Dec 30, 2019

728 10

See all from Budhdi Sharma

See all from AndroidPub

https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 7/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

Recommended from Medium

Sahil Gupta

Singleton Design Pattern


One of the most favorite design pattern ask in interview and use in our programming life is
singleton pattern. i am going to start my blog…

5 min read · Feb 13, 2024

51 1

https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 8/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

Sreenath

Singleton Design Pattern


The Singleton Design Pattern ensures a class has only one instance and provides a global
access point to it. This instance can then be used…

3 min read · Dec 12, 2023

Lists

General Coding Knowledge


20 stories · 1069 saves

data science and AI


40 stories · 121 saves

https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 9/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

The Code Bean

Decorator Design Pattern: Implementation in Java


The Decorator pattern is a structural design pattern that allows you to enhance or modify the
behavior of objects at runtime. It achieves…

3 min read · Oct 6, 2023

15

Priya Salvi

Java Aggregation and Composition Explained with Examples


https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 10/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

Aggregation:

4 min read · Oct 10, 2023

33

Mehar Chand

Decorator Design Pattern Use Case: Text Formatting in UI


In a UI application, text formatting is a common requirement, and the Decorator pattern can be
applied to dynamically add formatting…

2 min read · Nov 24, 2023

50

https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 11/12
31/03/2024, 11:55 Abstract Factory Pattern. Abstract Factory is a creational design… | by Budhdi Sharma | AndroidPub | Medium

Kamini Kamal in Level Up Coding

Design Pattern | Adapter Pattern


In the realm of software design, interoperability is often a significant challenge. Different
components or classes may have incompatible…

5 min read · Oct 5, 2023

51

See more recommendations

https://medium.com/android-news/abstract-factory-pattern-4ce0fe838d52 12/12

You might also like