Home >Java >javaTutorial >Java design pattern factory pattern (three types)
Factory Pattern
1. Simple Factory
Understanding
Factory classes in the simple factory pattern generally use static methods to return different object instances based on the different parameters they receive.
It cannot be expanded without modifying the code.
Example
Product interface
//Product interface
public interface ProductI {
public void productName();
}
Product entity class
public class ProductA implements ProductI {
@Override
public void productName() {
System .out.println("product A");
}
}
public class ProductB implements ProductI {
@Override
public void productName() {
System.out.println("product B");
}
}
Factory class
//Simple factory pattern
public class Factory {
public ProductI create(String productName) {
switch (productName) {
case "A": use using ‐ ‐ use using through through through through ‐ ‐ ‐‐ ‐‐ ‐‐‐‐ to
return new ProductB();
return null; Factory();
ProductI productA = factory.create("A");
productA.productName();
ProductI productB = factory.create("B"); productB.productName();
}
}
Output
product A
product B
Factory method
Understanding
The factory method provides a factory class for each product. Different product instances are created through different factory instances.
In the same hierarchical structure, any product is supported.
Example
The product interface, product entity class and simple factory are the same.
Factory interface
public interface FactoryI {
// The purpose of the factory is to produce products
public ProductI create();
}
Factory entity class
public class FactoryA implements FactoryI {
@Override
public ProductI create() {
Return new ProductA();
public class Client {
public static void main(String[] args) {
FactoryI factoryA = new FactoryA();
ProductI productA = factoryA.create();
productA.productName();
//
FactoryI factoryB = new FactoryB();
ProductI productB = factoryB.create();
productB.productName();
}
}
Output
product A
product B
Abstract Factory
Understanding
Abstract factory is a response to product families Conceptual. For example, each automobile company may want to produce cars, trucks, and buses at the same time, so each factory must have a method for creating cars, trucks, and buses.
It was born in response to the concept of product family. It is easy to add new product lines, but it is impossible to add new products.
The difference from factory methods is that abstract factories often have multiple methods that can produce multiple products, that is, product clusters.
Example
Abstract factory class
// Drawing factory pattern
public interface AbstractFactory {
public ProductAI createProductA();
public ProductBI createProductB();
}
Factory entity class
public class Factory1 implements AbstractFactory {
@Override
public ProductAI createProductA() {
return new ProductA1();
}
@Override
public ProductBI createProductB() {
return new ProductB1();
}
}
public class Factory2 implements AbstractFactory {
@Override
public ProductAI createProductA() {
return new ProductA2();
}
@Override
public ProductBI createProductB() {
return new ProductB2();
}
}
产品接口
//产品接口
public interface ProductBI {
public void productName();
}
//产品接口
public interface ProductAI {
public void productName();
}
产品实体类
public class ProductA1 implements ProductAI {
@Override
public void productName() {
System.out.println("product A1");
}
}
更多Java设计模式之工厂模式(三种)相关文章请关注PHP中文网!