How Nacos Client Implements the Simple Factory Pattern
This article explains the simple factory pattern, compares its standard definition with Nacos's implementation, and walks through concrete Java examples showing how NacosFactory, NamingFactory, and related services create instances in the Nacos client.
Simple Factory Pattern Overview
The simple (static) factory creates concrete instances based on input parameters while hiding construction details from the client. Its typical structure includes:
Factory : decides which product to instantiate.
AbstractProduct : common interface or abstract class for all products.
ConcreteProduct : concrete implementations of the product.
Standard Example
public interface NacosService {
void register(Object object);
}
public class NamingService implements NacosService {
@Override
public void register(Object object) {
System.out.println("注册命名服务成功");
}
}
public class ConfigService implements NacosService {
@Override
public void register(Object object) {
System.out.println("配置中心实例注册成功");
}
}
public class NacosFactory {
public static NacosService getService(String name) {
if ("naming".equals(name)) {
return new NamingService();
} else {
return new ConfigService();
}
}
}Clients obtain a NacosService via NacosFactory.getService("naming") without knowing the concrete class.
Pros and Cons
Encapsulates object creation and separates responsibilities.
Clients supply only a parameter, not class names.
Configuration‑driven extensions allow adding new products without changing client code.
Drawbacks: factory can become large, increases system complexity, may violate the Open/Closed Principle, and lacks an inheritance hierarchy for the factory itself.
Nacos Client’s Factory Design
The Nacos client provides a NacosFactory that aggregates static creation methods for ConfigService, NamingService, and NamingMaintainService:
NamingService namingService = NacosFactory.createNamingService(properties); public class NacosFactory {
public static ConfigService createConfigService(Properties properties) throws NacosException {
return ConfigFactory.createConfigService(properties);
}
public static NamingService createNamingService(Properties properties) throws NacosException {
return NamingFactory.createNamingService(properties);
}
// other methods omitted
}Instead of a single method with a parameter, NacosFactory offers separate static methods. Rationale:
Each service already has its own dedicated factory ( ConfigFactory, NamingFactory), so direct calls are possible. NacosFactory acts as an aggregation layer, providing a unified entry point for external integrations (e.g., Spring Cloud).
The current Nacos version only needs three services, making strict adherence to the Open/Closed Principle less critical.
Nested NamingFactory – Classic Simple Factory
public class NamingFactory {
public static NamingService createNamingService(Properties properties) throws NacosException {
try {
Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingService");
Constructor<?> constructor = driverImplClass.getConstructor(Properties.class);
return (NamingService) constructor.newInstance(properties);
} catch (Throwable e) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
}
}
// ...
}This method returns the abstract NamingService interface while internally instantiating the concrete implementation, matching the classic simple factory definition.
Key Takeaways
When reading source code, actively identify applied design patterns and their intent.
Adapt patterns to the concrete context rather than following them dogmatically; flexibility leads to clearer, more maintainable designs.
Source repository: https://github.com/secbr/nacos-learn
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.
