Introduce Java service or helper classes when a non-domain abstraction improves cohesion and reduces coupling.
In the realm of object-oriented design, the GRASP (General Responsibility Assignment Software Patterns) principles serve as a guide to assigning responsibilities to classes and objects. Among these principles, Pure Fabrication stands out as a strategic approach to creating classes that do not directly represent a domain concept. Instead, these classes are designed to achieve low coupling and high cohesion, which are essential for building robust and maintainable software systems.
Pure Fabrication is a design principle that involves creating classes that are not part of the problem domain but are introduced to fulfill specific design requirements. These classes are often utility or service classes that encapsulate operations or behaviors that do not naturally belong to any existing domain class.
The primary purpose of Pure Fabrication is to improve the design of a system by:
Pure Fabrication is particularly useful when:
Consider a scenario where you have a Customer class that handles customer data. If this class also manages the persistence of customer data to a database, it may become too complex. By introducing a CustomerRepository class, you can separate the concerns of data management from the core customer logic.
Service classes are a common example of Pure Fabrication. They encapsulate business logic that does not fit neatly into domain classes.
1public class OrderService {
2 // Handles operations related to orders
3 public void processOrder(Order order) {
4 // Business logic for processing an order
5 }
6}
In this example, OrderService is a Pure Fabrication class that manages order-related operations, keeping the Order class focused on representing the order data.
Utility classes provide static methods for common operations that do not belong to any specific domain class.
1public class StringUtils {
2 // Utility method for string manipulation
3 public static String capitalize(String input) {
4 if (input == null || input.isEmpty()) {
5 return input;
6 }
7 return input.substring(0, 1).toUpperCase() + input.substring(1);
8 }
9}
StringUtils is a Pure Fabrication class that offers utility methods for string manipulation, enhancing code reusability and organization.
By adhering to the Pure Fabrication principle, developers can avoid violating other design principles such as:
Let’s consider a scenario where we need to send notifications to users. Instead of embedding notification logic within domain classes, we can create a NotificationService as a Pure Fabrication class.
1public class NotificationService {
2 // Sends a notification to a user
3 public void sendNotification(User user, String message) {
4 // Logic to send notification
5 System.out.println("Sending notification to " + user.getName() + ": " + message);
6 }
7}
Logger class that handles logging across the application is a Pure Fabrication class.DataTransformer class that converts data formats or structures is another example.Service and Repository annotations in Spring are often used to define Pure Fabrication classes.StringUtils class in Apache Commons Lang is a well-known example of a utility class.Pure Fabrication is a powerful design principle that enhances the structure and maintainability of software systems. By introducing classes that encapsulate specific functionalities, developers can achieve low coupling and high cohesion, leading to more robust and flexible applications. As you design your next Java application, consider how Pure Fabrication can help you organize your codebase and improve overall system design.
Consider how Pure Fabrication can be applied to your current projects. Are there areas where responsibilities are not clearly defined? How can you refactor your code to improve cohesion and reduce coupling?