Backend Development 20 min read

Eliminating Code Duplication in Business Logic with Design Patterns, Annotations, Reflection, and Bean Mapping

This article explains how to reduce repetitive business‑level Java code by applying the Template Method and Factory patterns, leveraging custom annotations with reflection, and using bean‑mapping utilities, thereby improving maintainability, extensibility, and overall code quality.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Eliminating Code Duplication in Business Logic with Design Patterns, Annotations, Reflection, and Bean Mapping

Many developers consider business‑level code to be low‑value because it often consists of repetitive CRUD logic, but the author argues that applying proven design principles can make such code both elegant and technically rich.

1. Factory + Template Method pattern – By extracting the common cart‑processing steps into an abstract class ( public abstract class AbstractCart { ... } ) and letting concrete subclasses implement only the parts that differ (e.g., discount and delivery calculations), the duplicated 70% of code across NormalUserCart , VipUserCart , and InternalUserCart is removed. The factory logic is then simplified with Spring’s IoC container, turning a series of if…else statements into a bean lookup ( AbstractCart cart = (AbstractCart) applicationContext.getBean(userCategory + "UserCart"); ), illustrating the Open‑Closed Principle.

2. Annotations + Reflection – The article shows how to describe API parameters using custom annotations ( @BankAPI and @BankAPIField ) and then, via a single remoteCall(AbstractAPI api) method, automatically serialize fields in the correct order, apply string or numeric padding, perform MD5 signing, and send the request. This eliminates hard‑coded formatting logic and makes the implementation data‑driven.

3. Property‑copy utilities – Manual field‑by‑field copying between DTO, DO, and VO objects is error‑prone, especially with dozens of attributes. The author recommends using a mapping tool such as BeanUtils.copyProperties(source, target, "id") to copy matching properties while ignoring specified ones, reducing bugs caused by typos or mismatched assignments.

The conclusion reiterates that extracting common logic into abstract classes, using metadata‑driven reflection, and employing bean‑mapping tools are three effective strategies to combat code duplication, improve maintainability, and support future extensions without modifying existing code.

Finally, the author adds a promotional note encouraging readers to like, share, and follow the "码猿技术专栏" public account for additional resources.

design patternsJavareflectioncode refactoringSpringAnnotationsBean Mapping
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.