Mastering the Factory Method Pattern with Nacos: Code Walkthrough and Best Practices
This article explains the Factory Method design pattern, contrasts it with Simple Factory, presents its structural diagram, details core roles, provides a complete Java implementation using Nacos services, and discusses its advantages, drawbacks, and suitable application scenarios.
Factory Method Pattern Overview
The Factory Method pattern, also known as the virtual constructor pattern, extends the Simple Factory by abstracting the creator class into an interface, allowing concrete factories to instantiate specific product objects while keeping client code decoupled from concrete implementations.
Core Roles
Abstract Product (Product) : Defines the common interface or abstract class for all products.
Concrete Product : Implements the specific product.
Abstract Factory (AbstractFactory) : Declares the method for creating a product.
Concrete Factory (ConcreteFactory) : Implements the creation method to return a concrete product.
Structure Diagram
Below are the simplified diagrams for Simple Factory and the extended Factory Method pattern.
Simple Factory diagram
Factory Method diagram
Java Implementation Using Nacos
First, define the abstract product and its concrete implementations:
public interface NacosService {
/** Register an instance */
void register(Object object);
}
public class ConfigService implements NacosService {
@Override
public void register(Object object) {
System.out.println("配置中心实例注册成功");
}
}
public class NamingService implements NacosService {
@Override
public void register(Object object) {
System.out.println("注册命名服务成功");
}
}Next, define the abstract factory and concrete factories:
public interface NacosFactory {
NacosService getService();
}
public class ConfigNacosFactory implements NacosFactory {
@Override
public NacosService getService() {
return new ConfigService();
}
}
public class NamingNacosFactory implements NacosFactory {
@Override
public NacosService getService() {
return new NamingService();
}
}Client code can now obtain the desired service without knowing its concrete class:
public class Client {
public static void main(String[] args) {
// Configuration service
NacosFactory configFactory = new ConfigNacosFactory();
NacosService configService = configFactory.getService();
configService.register(new Object());
// Naming service
NacosFactory namingFactory = new NamingNacosFactory();
NacosService namingService = namingFactory.getService();
namingService.register(new Object());
}
}Advantages
Clients depend only on the abstract factory, not on concrete product creation details.
Adding new products requires only a new concrete factory, enhancing flexibility.
Supports the Open/Closed Principle and Single Responsibility Principle, and resolves inheritance limitations of Simple Factory.
Disadvantages
Increases the number of classes; each new product adds a corresponding factory class.
Introduces additional abstraction layers, making the system harder to understand.
Abstract product can create only one type of product; switching to another product may still need modifications, which can be mitigated by using the Abstract Factory pattern.
Typical Application Scenarios
Clients know only the factory name, not the concrete product name.
Object creation responsibilities are distributed among multiple concrete factories, while the abstract factory provides a unified creation interface.
Clients are interested in product branding rather than creation details.
Conclusion
The Factory Method pattern deepens the understanding of design patterns by illustrating how to abstract product creation, achieve decoupling, and adhere to core OOP principles. Mastering this pattern helps developers write more maintainable and extensible code, especially in large‑scale Java projects such as Nacos.
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.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
