factory/README.md
The Factory Design Pattern in Java is a creational pattern that defines an interface for creating an object but allows subclasses to alter the type of objects that will be created. This pattern promotes flexibility and scalability in your codebase.
Real-world example
Imagine a scenario in a bakery where different types of cakes are made using a Factory Design Pattern. The bakery's
CakeFactoryhandles the creation process, allowing easy addition of new cake types without altering the core cake-making process. TheCakeFactorycan produce various types of cakes such as chocolate cake, vanilla cake, and strawberry cake. Instead of the bakery staff manually selecting ingredients and following specific recipes for each type of cake, they use theCakeFactoryto handle the process. The customer simply requests a cake type, and theCakeFactorydetermines the appropriate ingredients and recipe to use, then creates the specific type of cake. This setup allows the bakery to easily add new cake types without modifying the core cake-making process, promoting flexibility and scalability.
Wikipedia says
Factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class.
Sequence diagram
Imagine an alchemist who is about to manufacture coins. The alchemist must be able to create both gold and copper coins and switching between them must be possible without modifying the existing source code. The factory pattern makes it possible by providing a static construction method which can be called with relevant parameters.
In Java, you can implement the Factory Pattern by defining an interface Coin and its implementations GoldCoin and CopperCoin. The CoinFactory class provides a static method getCoin to create coin objects based on the type.
public interface Coin {
String getDescription();
}
public class GoldCoin implements Coin {
static final String DESCRIPTION = "This is a gold coin.";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
public class CopperCoin implements Coin {
static final String DESCRIPTION = "This is a copper coin.";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
Enumeration below represents types of coins that we support (GoldCoin and CopperCoin).
@RequiredArgsConstructor
@Getter
public enum CoinType {
COPPER(CopperCoin::new),
GOLD(GoldCoin::new);
private final Supplier<Coin> constructor;
}
Then we have the static method getCoin to create coin objects encapsulated in the factory class CoinFactory.
public class CoinFactory {
public static Coin getCoin(CoinType type) {
return type.getConstructor().get();
}
}
Now, in the client code, we can generate various types of coins using the factory class.
public static void main(String[] args) {
LOGGER.info("The alchemist begins his work.");
var coin1 = CoinFactory.getCoin(CoinType.COPPER);
var coin2 = CoinFactory.getCoin(CoinType.GOLD);
LOGGER.info(coin1.getDescription());
LOGGER.info(coin2.getDescription());
}
Program output:
06:19:53.530 [main] INFO com.iluwatar.factory.App -- The alchemist begins his work.
06:19:53.533 [main] INFO com.iluwatar.factory.App -- This is a copper coin.
06:19:53.533 [main] INFO com.iluwatar.factory.App -- This is a gold coin.
Benefits:
Trade-offs: