Skip to content

MultiverseLearningProducts/payment-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Unit 2: Refactoring a Payment Processing System with Design Patterns

Overview

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.

Learning Objectives

  • 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

Context

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

Project Structure

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

Requirements

1. Apply Singleton Pattern

  • Refactor PaymentGateway to use Singleton pattern
  • Ensure only one gateway connection exists throughout the application
  • Make it thread-safe

2. Apply Factory Pattern

  • Create a PaymentProcessor interface
  • Implement CreditCardProcessor and PayPalProcessor
  • Create PaymentProcessorFactory to create processors based on type
  • Make it easy to add new payment processor types

3. Apply Builder Pattern

  • Refactor PaymentRequest to use Builder pattern
  • Support optional parameters (amount, currency, description, etc.)
  • Provide fluent interface for object construction
  • Include validation in the build method

4. Maintain Functionality

  • All existing functionality must be preserved
  • Test cases must pass after refactoring
  • No breaking changes to public API (if possible)

Implementation Steps

  1. Analyze existing code - Identify design problems and code smells
  2. Apply Singleton - Refactor PaymentGateway to use Singleton pattern
  3. Apply Factory - Create processor interface and factory for creation
  4. Apply Builder - Refactor PaymentRequest to use Builder pattern
  5. Test thoroughly - Ensure all tests pass and functionality is preserved
  6. Evaluate impact - Assess improvements in maintainability and extensibility

Testing

Run the provided test cases:

mvn test

Resources

  • Original code with design issues provided
  • Refactoring requirements document
  • Pattern reference guide
  • Test cases for validation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages