This Skillable lab activity requires you to refactor an existing payment processing system by applying creational design patterns (Singleton, Factory, Builder) to improve code maintainability, extensibility, and quality.
- Analyze existing code for design problems and code smells
- Identify opportunities to apply creational design patterns
- Refactor code using Singleton, Factory, and Builder patterns
- Evaluate the impact of patterns on code maintainability
- Ensure functionality is preserved after refactoring
You're working with a payment processing system that has several design issues:
- Multiple payment gateway connections are created unnecessarily
- Payment processor creation logic is scattered and hard to extend
- Payment request objects have too many constructor parameters
- Code is difficult to test and maintain
src/main/java/com/jse/payment/
├── before/ # Original code with design issues
│ ├── PaymentGateway.java
│ ├── PaymentProcessor.java
│ └── PaymentRequest.java
└── after/ # Refactored code structure
├── gateway/
│ └── PaymentGateway.java (Singleton)
├── processor/
│ ├── PaymentProcessor.java (interface)
│ ├── CreditCardProcessor.java
│ ├── PayPalProcessor.java
│ └── PaymentProcessorFactory.java
└── request/
├── PaymentRequest.java
└── PaymentRequestBuilder.java
src/test/java/com/jse/payment/
└── PaymentSystemTest.java
- Refactor
PaymentGatewayto use Singleton pattern - Ensure only one gateway connection exists throughout the application
- Make it thread-safe
- Create a
PaymentProcessorinterface - Implement
CreditCardProcessorandPayPalProcessor - Create
PaymentProcessorFactoryto create processors based on type - Make it easy to add new payment processor types
- Refactor
PaymentRequestto use Builder pattern - Support optional parameters (amount, currency, description, etc.)
- Provide fluent interface for object construction
- Include validation in the build method
- All existing functionality must be preserved
- Test cases must pass after refactoring
- No breaking changes to public API (if possible)
- Analyze existing code - Identify design problems and code smells
- Apply Singleton - Refactor PaymentGateway to use Singleton pattern
- Apply Factory - Create processor interface and factory for creation
- Apply Builder - Refactor PaymentRequest to use Builder pattern
- Test thoroughly - Ensure all tests pass and functionality is preserved
- Evaluate impact - Assess improvements in maintainability and extensibility
Run the provided test cases:
mvn test- Original code with design issues provided
- Refactoring requirements document
- Pattern reference guide
- Test cases for validation