Understanding Factory and Strategy Design Patterns in Java
This article explains the differences and similarities between the Factory and Strategy design patterns in Java, provides concrete code examples for each, and discusses how the Strategy pattern can offer more flexible behavior composition compared to the traditional Factory approach.
When first learning design patterns, the Singleton and Factory patterns are often the most used because many low‑level codes rely on them. After reading an article about Domain‑Driven Design (DDD), the author gradually adopted the Strategy pattern, finding that it reduces code redundancy and, combined with a richer domain model, makes service‑layer code cleaner.
The author notes that the Strategy pattern shares a similar mindset with micro‑services: both emphasize modular, interchangeable behavior. An example from DDD about bank transfers illustrates how clear logic and domain‑driven design can eliminate redundant code.
Although Factory and Strategy patterns may appear similar, their UML diagrams show only minor differences. The article proceeds with concrete Java code to illustrate both patterns.
public interface People {
void eat();
void run();
void wear();
}Two concrete implementations are provided:
public class Xiaoming implements People {
@Override
public void eat() { System.out.println("小明吃饭"); }
@Override
public void run() { System.out.println("小明跑步"); }
@Override
public void wear() { System.out.println("小明穿衣"); }
}
public class Xiaohong implements People {
@Override
public void eat() { System.out.println("小红吃饭"); }
@Override
public void run() { System.out.println("小红跑步"); }
@Override
public void wear() { System.out.println("小红穿衣"); }
}The simple factory creates instances based on a name:
public class PeopleFactory {
public People getPeople(String name) {
if (name.equals("Xiaoming")) {
return new Xiaoming();
} else if (name.equals("Xiaohong")) {
return new Xiaohong();
}
return null;
}
}The Strategy pattern encapsulates a People object and delegates calls:
public class StrategySign {
private People people;
public StrategySign(People people) { this.people = people; }
public StrategySign(String name) {
if (name.equals("Xiaoming")) {
this.people = new Xiaoming();
} else if (name.equals("Xiaohong")) {
this.people = new Xiaohong();
}
}
public void run() { people.run(); }
}A test demonstrates both patterns producing the same output:
@Test
public void testSign() {
PeopleFactory peopleFactory = new PeopleFactory();
People people = peopleFactory.getPeople("Xiaohong");
System.out.print("工厂模式-------------");
people.run();
StrategySign strategySign = new StrategySign("Xiaohong");
System.out.print("策略模式-------------");
strategySign.run();
}While both patterns can achieve the same result, the Factory pattern returns a concrete implementation and focuses on the final outcome, whereas the Strategy pattern emphasizes the process, allowing dynamic composition of behaviors without creating many separate methods.
For example, if one wants a sequence of actions (eat → run → eat), the Strategy pattern can encapsulate this sequence in a single method, whereas the Factory approach would require multiple test cases or additional methods, leading to redundancy.
Thus, the Strategy pattern resembles micro‑service architecture: each method (eat, run, wear) can be seen as an independent service, and the pattern connects them to form new composite behavior, reducing coupling and improving maintainability.
Thank you for reading, and hope this helps you understand the practical differences between Factory and Strategy patterns.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
