Design Principles - Java
Design principles are fundamental guidelines and best practices that help software developers create well-structured, maintainable, and efficient software systems. These principles guide the design and architecture of software, making it easier to understand, extend, and adapt.
1. SOLID Principles:
// Before SRP
class PaymentProcessor {
public void processPayment(Payment payment) {
//processing the payment
}
public void generatePaymentReceipt(Payment payment) {
// generating a payment receipt
}
}
// After SRP
class PaymentProcessor {
public void processPayment(Payment payment) {
// processing the payment
}
}
class ReceiptGenerator {
public void generatePaymentReceipt(Payment payment) {
// generating a payment receipt
}
}
// Before OCP
class PaymentValidator {
public boolean validatePayment(Payment payment) {
// validating the payment
}
}
// After OCP
interface PaymentValidator {
boolean validatePayment(Payment payment);
}
class CreditCardPaymentValidator implements PaymentValidator {
@Override
public boolean validatePayment(Payment payment) {
// validating credit card payments
}
}
class PayPalPaymentValidator implements PaymentValidator {
@Override
public boolean validatePayment(Payment payment) {
// validating PayPal payments
}
}
class PaymentProcessor {
public void processPayment(Payment payment) {
// processing the payment
}
}
class CreditCardPaymentProcessor extends PaymentProcessor {
@Override
public void processPayment(Payment payment) {
// credit card payment processing
}
}
class PayPalPaymentProcessor extends PaymentProcessor {
@Override
public void processPayment(Payment payment) {
// PayPal payment processing
}
}
interface OnlinePayment {
void processOnlinePayment();
}
interface OfflinePayment {
void processOfflinePayment();
}
class CreditCardPayment implements OnlinePayment {
@Override
public void processOnlinePayment() {
// processing credit card payment online
}
}
class CashPayment implements OfflinePayment {
@Override
public void processOfflinePayment() {
// processing cash payment offline
}
}
class PaymentGateway {
private PaymentProcessor processor;
public PaymentGateway(PaymentProcessor processor) {
this.processor = processor;
}
public void processPayment(Payment payment) {
processor.processPayment(payment);
}
}
2. DRY (Don't Repeat Yourself):
//Before applying DRY
class Payment {
private double amount;
private String paymentMethod;
// others
public boolean isValid() {
if (amount <= 0) {
System.out.println("Invalid payment amount.");
return false;
}
if (paymentMethod == null || paymentMethod.isEmpty()) {
System.out.println("Invalid payment method.");
return false;
}
// Additional validation for this payment method
if (paymentMethod.equals("CreditCard")) {
// Credit card validation
if (!validateCreditCard()) {
System.out.println("Credit card validation failed.");
return false;
}
} else if (paymentMethod.equals("PayPal")) {
// PayPal validation
if (!validatePayPal()) {
System.out.println("PayPal validation failed.");
return false;
}
}
return true;
}
// Validation methods for specific payment methods
private boolean validateCreditCard() {
// Validation for credit card payments
return true;
}
private boolean validatePayPal() {
// Validation for PayPal payments
return true;
}
}
class PaymentProcessor {
public void processPayment(Payment payment) {
if (payment.isValid()) {
// processing the payment
System.out.println("Payment processed successfully.");
} else {
System.out.println("Payment validation failed. Unable to process payment.");
}
}
}
After Applying DRY
class Payment {
private double amount;
private String paymentMethod;
// others
public boolean isValid() {
// Common payment validation
if (amount <= 0) {
System.out.println("Invalid payment amount.");
return false;
}
if (paymentMethod == null || paymentMethod.isEmpty()) {
System.out.println("Invalid payment method.");
return false;
}
return true;
}
}
class PaymentProcessor {
public void processPayment(Payment payment) {
if (payment.isValid()) {
// processing the payment
System.out.println("Payment processed successfully.");
} else {
System.out.println("Payment validation failed. Unable to process payment.");
}
}
}
3. KISS (Keep It Simple, Stupid):
class ComplexPaymentProcessor {
public void processPayment(Payment payment) {
if (payment == null) {
System.out.println("Invalid payment. Cannot process.");
return;
}
if (payment.getAmount() <= 0) {
System.out.println("Invalid payment amount.");
return;
}
if (payment.getPaymentMethod() == null || payment.getPaymentMethod().isEmpty()) {
System.out.println("Invalid payment method.");
return;
}
// Complex payment processing logic involving multiple if-else statements, loops, and conditional checks.
// Handling various edge cases and exceptions here.
System.out.println("Payment processed successfully.");
}
}
After KISS:
With the KISS principle applied, you simplify the code and remove unnecessary complexities:
class SimplePaymentProcessor {
public void processPayment(Payment payment) {
if (payment == null) {
System.out.println("Invalid payment. Cannot process.");
return;
}
if (!payment.isValid()) {
System.out.println("Invalid payment.");
return;
}
// Simplified payment processing without unnecessary complexities.
System.out.println("Payment processed successfully.");
}
}
4. YAGNI (You Ain't Gonna Need It):
Before Yagini
Recommended by LinkedIn
class PaymentProcessor {
public void processPayment(Payment payment) {
// Complex logic for payment processing
if (payment.isValid()) {
// Additional features that are not currently needed but were added "just in case."
// These features introduce unnecessary complexity.
if (payment.isFraudulent()) {
System.out.println("Potential fraudulent payment detected.");
// Take action to handle fraud (e.g., block payment).
}
if (payment.needsConfirmation()) {
System.out.println("Payment confirmation required.");
// Send confirmation request to the user.
}
// More unnecessary features and complexity...
} else {
System.out.println("Payment validation failed. Unable to process payment.");
}
}
}
After Applying Yagini
class SimplePaymentProcessor {
public void processPayment(Payment payment) {
if (payment == null || !payment.isValid()) {
System.out.println("Invalid payment. Cannot process.");
return;
}
// Simplified payment processing without adding unnecessary features.
System.out.println("Payment processed successfully.");
}
}
5. Composition Over Inheritance:
Before Composition Over Inheritance:
In situations where inheritance is used excessively, you might end up with a complex inheritance hierarchy that becomes challenging to manage:
class Payment {
// Common payment properties and methods
}
class CreditCardPayment extends Payment {
// Credit card payment-specific properties and methods
}
class PayPalPayment extends Payment {
// PayPal payment-specific properties and methods
}
class BitcoinPayment extends Payment {
// Bitcoin payment-specific properties and methods
}
After Composition Over Inheritance:
With the "Composition Over Inheritance" principle applied, you can simplify the design using composition:
class Payment {
// Common payment properties and methods
}
class PaymentMethod {
private Payment payment;
public PaymentMethod(Payment payment) {
this.payment = payment;
}
public void processPayment() {
// processing payment using the contained Payment instance
}
// Additional methods related to payment methods...
}
/* In this "after" example, you create a PaymentMethod class that contains an instance of the Payment class. Instead of relying on an extensive inheritance hierarchy, you use composition to represent different payment methods. Each payment method can have its own logic encapsulated within its processPayment method, and you can add more payment methods without altering the existing code. */
6. Separation of Concerns (SoC):
class PaymentProcessor {
public void processPayment(Payment payment) {
// Payment validation
if (payment == null || !payment.isValid()) {
System.out.println("Invalid payment. Cannot process.");
return;
}
// Payment processing
try {
// payment processing
System.out.println("Payment processed successfully.");
} catch (PaymentException e) {
System.out.println("Payment processing failed: " + e.getMessage());
}
}
}
After SOC
class PaymentValidator {
public boolean validatePayment(Payment payment) {
if (payment == null || !payment.isValid()) {
return false;
}
return true;
}
}
class PaymentProcessor {
public void processPayment(Payment payment) {
PaymentValidator validator = new PaymentValidator();
if (!validator.validatePayment(payment)) {
System.out.println("Invalid payment. Cannot process.");
return;
}
// Payment processing
try {
// payment processing
System.out.println("Payment processed successfully.");
} catch (PaymentException e) {
System.out.println("Payment processing failed: " + e.getMessage());
}
}
}
7. Law of Demeter (LoD) - "Don't Talk to Strangers":
class PaymentProcessor {
public void processPayment(Customer customer, Order order) {
// Tight coupling: Accessing properties of nested objects
String customerName = customer.getName();
String orderStatus = order.getStatus();
// Complex payment processing using customerName and orderStatus
// ...
}
}
After LOD
class Customer {
private String name;
public String getName() {
return name;
}
}
class Order {
private String status;
public String getStatus() {
return status;
}
}
class PaymentProcessor {
public void processPayment(Customer customer, Order order) {
// Reduced coupling: Accessing properties via methods
String customerName = customer.getName();
String orderStatus = order.getStatus();
// Simplified payment processing using customerName and orderStatus
// ...
}
}
8. Fail-Fast Principle:
class PaymentProcessor {
public void processPayment(Payment payment) {
// Payment validation
if (payment == null || !payment.isValid()) {
// Continue processing even though payment is invalid
System.out.println("Payment validation failed, but processing continues.");
}
// Payment processing
try {
// Complex payment processing
System.out.println("Payment processed successfully.");
} catch (PaymentException e) {
System.out.println("Payment processing failed: " + e.getMessage());
}
}
}
After Applying Principle
class PaymentProcessor {
public void processPayment(Payment payment) {
// Payment validation
if (payment == null || !payment.isValid()) {
System.out.println("Payment validation failed. Cannot process.");
return; // Fail-fast: Stop processing immediately
}
// Payment processing
try {
// Complex payment processing
System.out.println("Payment processed successfully.");
} catch (PaymentException e) {
System.out.println("Payment processing failed: " + e.getMessage());
}
}
}
Well written article RamiReddy P. Insightful and also highly beneficial for junior engineers looking to expand their knowledge.