Backend Development 21 min read

Eliminating Code Duplication in Java Backend Development with Design Patterns, Annotations, and Bean Mapping

This article explains how to reduce repetitive Java backend code by extracting common logic into abstract classes, applying the Template Method and Factory patterns, using custom annotations with reflection for API serialization, and leveraging bean‑mapping utilities to copy properties between DTOs and DOs.

Architect's Guide
Architect's Guide
Architect's Guide
Eliminating Code Duplication in Java Backend Development with Design Patterns, Annotations, and Bean Mapping

Software engineers often write duplicated code, especially in business logic that handles similar workflows for different user types. The article first demonstrates three shopping‑cart implementations (normal, VIP, internal) that repeat about 70% of the code, leading to maintenance risks such as bugs that appear only in one variant.

To eliminate this duplication, the author introduces an AbstractCart abstract class that implements the common workflow (initialising the cart, converting input maps to Item objects, calculating totals) and declares two abstract methods processCouponPrice and processDeliveryPrice . Sub‑classes ( NormalUserCart , VipUserCart , InternalUserCart ) only implement the specific discount or delivery logic, dramatically reducing repeated code. The article also shows a Spring‑based factory method that selects the appropriate cart bean by name, illustrating the Factory pattern combined with the Template Method pattern and the Open‑Closed Principle.

Next, the article tackles another source of duplication: hard‑coded parameter formatting for external bank APIs. By defining a @BankAPI annotation for the endpoint URL and description, and a @BankAPIField annotation for each field (order, type, length), the same reflection‑driven remoteCall method can serialize any request object, apply the correct padding (underscores for strings, zeros for numbers, monetary rounding for amounts), compute an MD5 signature, and send the HTTP request. Sample POJOs ( CreateUserAPI and PayAPI ) illustrate how the annotations replace repetitive string‑building code.

Finally, the article discusses the frequent manual copying between DTO/DO/VO objects, which is error‑prone when many fields exist. It shows a buggy example where fields are swapped or copied from the wrong object, then recommends using a bean‑mapping utility such as BeanUtils.copyProperties(source, target, "id") to perform the copy safely while allowing ignored properties.

The conclusion summarises three patterns for removing duplication: (1) extract common logic into a superclass using the Template Method pattern; (2) replace hard‑coded algorithms with metadata‑driven annotations and reflection; (3) use automated bean‑mapping tools for object conversion. Together, these techniques improve maintainability, reduce bugs, and adhere to solid design principles in Java backend projects.

design patternsJavaReflectioncode refactoringSpringAnnotationsBean Mapping
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.