📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Video Tutorial
Table of contents |
---|
Intent/Definition |
Problem |
Forces |
Solution |
Explanation |
Structure - Class Diagram, Sequence Diagram |
Implementation |
Consequences |
Applicability |
References |
Intent/Definition
Problem
Forces
- You want clients to access components in other tiers to retrieve and update data.
- You want to reduce remote requests across the network.
- You want to avoid network performance degradation caused by chattier applications that have high network traffic.
Solution
Explanation
- The Data Transfer Object pattern is a design pattern in which a data transfer object is used to serve related information together to avoid multiple calls for each piece of information.
- Transfer Object is a simple POJO class having getter/setter methods and is serializable so that it can be transferred over the network. It does not have any behavior.
- Server Side business class normally fetches data from the database and fills the POJO and send it to the client or pass it by value. For the client, transfer object is read-only.
- Client can create its own transfer object and pass it to a server to update values in a database in one shot.
Structure
Class Diagram
Sequence Diagram
Implementation
public class CustomerDto {
private final String id;
private final String firstName;
private final String lastName;
/**
* @param id customer id
* @param firstName customer first name
* @param lastName customer last name
*/
public CustomerDto(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
public class CustomerResource {
private List < CustomerDto > customers;
/**
* @param customers initialize resource with existing customers. Act as database.
*/
public CustomerResource(List < CustomerDto > customers) {
this.customers = customers;
}
/**
* @return : all customers in list.
*/
public List < CustomerDto > getAllCustomers() {
return customers;
}
/**
* @param customer save new customer to list.
*/
public void save(CustomerDto customer) {
customers.add(customer);
}
/**
* @param customerId delete customer with id {@code customerId}
*/
public void delete(String customerId) {
customers.removeIf(customer - > customer.getId().equals(customerId));
}
}
- CustomerResource act as a server to serve customer information.
- The CustomerDto is data transfer object to share customer information.
public class CustomerClientApp {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomerClientApp.class);
/**
* Method as act client and request to server for details.
*
* @param args program argument.
*/
public static void main(String[] args) {
List < CustomerDto > customers = new ArrayList < > ();
CustomerDto customerOne = new CustomerDto("1", "Kelly", "Brown");
CustomerDto customerTwo = new CustomerDto("2", "Alfonso", "Bass");
customers.add(customerOne);
customers.add(customerTwo);
CustomerResource customerResource = new CustomerResource(customers);
LOGGER.info("All customers:-");
List < CustomerDto > allCustomers = customerResource.getAllCustomers();
printCustomerDetails(allCustomers);
LOGGER.info("----------------------------------------------------------");
LOGGER.info("Deleting customer with id {1}");
customerResource.delete(customerOne.getId());
allCustomers = customerResource.getAllCustomers();
printCustomerDetails(allCustomers);
LOGGER.info("----------------------------------------------------------");
LOGGER.info("Adding customer three}");
CustomerDto customerThree = new CustomerDto("3", "Lynda", "Blair");
customerResource.save(customerThree);
allCustomers = customerResource.getAllCustomers();
printCustomerDetails(allCustomers);
}
private static void printCustomerDetails(List < CustomerDto > allCustomers) {
allCustomers.forEach(customer - > LOGGER.info(customer.getFirstName()));
}
}
Consequences
- Reduces network traffic
- Simplifies remote object and remote interface
- Transfers more data in fewer remote calls
- Reduces code duplication
- Introduces stale transfer objects
- Increases complexity due to synchronization and version control.
Applicability
- The client is asking for multiple information. And the information is related.
- When you want to boost the performance to get resources.
- You want reduced number of remote calls.
Merci :-)
ReplyDelete