Understanding Factory Patterns: Simple Factory, Factory Method, and Abstract Factory with Spring Dependency Injection
This article explains the three common factory design patterns—simple factory, factory method, and abstract factory—illustrates their Java implementations with product and factory classes, demonstrates client usage, and shows how Spring's dependency injection can replace manual factory instantiation, providing a comprehensive guide for developers.
The term “factory pattern” collectively refers to three widely used design patterns: Simple Factory , Factory Method , and Abstract Factory . The previous comic introduced the first two; this article focuses on the Abstract Factory pattern and its application in the Spring framework.
First, the product interfaces and concrete implementations are defined. Masks and protective suits each have high‑end and low‑end variants, represented by IMask , LowEndMask , HighEndMask , IProtectiveSuit , LowEndProtectiveSuit , and HighEndProtectiveSuit classes.
public interface IMask {
void showMask();
}
public class LowEndMask implements IMask {
@Override
public void showMask() {
System.out.println("我的低端口罩");
}
}
public class HighEndMask implements IMask {
@Override
public void showMask() {
System.out.println("我是高端口罩");
}
}
public interface IProtectiveSuit {
void showSuit();
}
public class LowEndProtectiveSuit implements IProtectiveSuit {
@Override
public void showSuit() {
System.out.println("我是低端防护服");
}
}
public class HighEndProtectiveSuit implements IProtectiveSuit {
@Override
public void showSuit() {
System.out.println("我是高端防护服");
}
}Next, the factory hierarchy is introduced. An IFactory interface declares creation methods for masks and suits, while LowEndFactory and HighEndFactory implement these methods to instantiate the corresponding product families.
public interface IFactory {
// 创建口罩
IMask createMask();
// 创建防护服
IProtectiveSuit createSuit();
}
public class LowEndFactory implements IFactory {
@Override
public IMask createMask() {
IMask mask = new LowEndMask();
// ... 低端口罩的初始化代码
return mask;
}
@Override
public IProtectiveSuit createSuit() {
IProtectiveSuit suit = new LowEndProtectiveSuit();
// ... 低端防护服的初始化代码
return suit;
}
}
public class HighEndFactory implements IFactory {
@Override
public IMask createMask() {
IMask mask = new HighEndMask();
// ... 高端口罩的初始化代码
return mask;
}
@Override
public IProtectiveSuit createSuit() {
IProtectiveSuit suit = new HighEndProtectiveSuit();
// ... 高端防护服的初始化代码
return suit;
}
}The client code demonstrates how to use the factories: by creating instances of LowEndFactory and HighEndFactory , the program obtains low‑end and high‑end masks and suits without directly referencing concrete product classes.
public class Test {
public static void main(String[] args) {
IFactory factoryA = new LowEndFactory();
IFactory factoryB = new HighEndFactory();
// 创建低端口罩
IMask maskA = factoryA.createMask();
// 创建高端口罩
IMask maskB = factoryB.createMask();
// 创建低端防护服
IProtectiveSuit suitA = factoryA.createSuit();
// 创建高端防护服
IProtectiveSuit suitB = factoryB.createSuit();
maskA.showMask();
maskB.showMask();
suitA.showSuit();
suitB.showSuit();
}
}Finally, the article connects these concepts to Spring’s Dependency Injection (DI). Spring manages bean creation and wiring via XML or annotation configuration, allowing developers to obtain fully constructed objects without using new or explicit factories. Changing an implementation (e.g., swapping UserService ) only requires updating the bean definition, illustrating the power of DI and the underlying BeanFactory mechanism.
<bean id="userController" class="com.xiaohui.controller.UserController">
<constructor-arg name="userService" ref="userService"/>
</bean>
<bean id="userService" class="com.xiaohui.service.UserService"/>Through this example, readers gain a clear understanding of how abstract factories group related products, reduce the number of factory classes, and how modern frameworks like Spring automate object creation, embodying the principles of the factory pattern in real‑world applications.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.