欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中轉換器設計模式深入講解

 更新時間:2019年04月10日 11:14:20   作者:jdon  
這篇文章主要給大家介紹了關于Java中轉換器設計模式的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

在這篇文章中,我們將討論 Java / J2EE項目中最常用的  Converter Design Pattern。由于Java8 功能不僅提供了相應類型之間的通用雙向轉換方式,而且還提供了轉換相同類型對象集合的常用方法,從而將樣板代碼減少到絕對最小值。我們使用Java8 功能編寫了此模式的源代碼。

目的

轉換器設計模式的目的是為相應類型之間的雙向轉換提供一種通用的方式,允許類型無需彼此了解的簡潔的實現(xiàn)。此外,轉換器設計模式引入了雙向收集映射,將樣板代碼減少到最小。

源代碼

轉換器設計模式是一種行為設計模式,允許在相應類型(如DTO和邏輯同構類型的域表示)之間進行雙向轉換。此外,該模式還引入了一種在類型之間轉換對象集合的通用方法。

類圖

讓我們根據(jù)上面的類圖編寫源代碼。

在本例中,我們將把customerd轉換為customer實體,反之亦然,我們還將在類型之間轉換對象集合。

步驟1:讓我們創(chuàng)建一個通用轉換器。

public abstract class Converter < T, C > {

 private final Function < T,
 C > fromDto;
 private final Function < C,
 T > fromEntity;

 /**
  * @param fromDto
  *   Function that converts given dto entity into the domain
  *   entity.
  * @param fromEntity
  *   Function that converts given domain entity into the dto
  *   entity.
  */
 public Converter(final Function < T, C > fromDto, final Function < C, T > fromEntity) {
  this.fromDto = fromDto;
  this.fromEntity = fromEntity;
 }

 /**
  * @param customerDto
  *   DTO entity
  * @return The domain representation - the result of the converting function
  *   application on dto entity.
  */
 public final C convertFromDto(final T customerDto) {
  return fromDto.apply(customerDto);
 }

 /**
  * @param customer
  *   domain entity
  * @return The DTO representation - the result of the converting function
  *   application on domain entity.
  */
 public final T convertFromEntity(final C customer) {
  return fromEntity.apply(customer);
 }

 /**
  * @param dtoCustomers
  *   collection of DTO entities
  * @return List of domain representation of provided entities retrieved by
  *   mapping each of them with the conversion function
  */
 public final List < C > createFromDtos(final Collection < T > dtoCustomers) {
  return dtoCustomers.stream().map(this::convertFromDto).collect(Collectors.toList());
 }

 /**
  * @param customers
  *   collection of domain entities
  * @return List of domain representation of provided entities retrieved by
  *   mapping each of them with the conversion function
  */
 public final List < T > createFromEntities(final Collection < C > customers) {
  return customers.stream().map(this::convertFromEntity).collect(Collectors.toList());
 }
}

步驟2:讓我們創(chuàng)建一個簡單客戶轉換器的實現(xiàn)。

public class CustomerConverter extends Converter<CustomerDto, Customer> {

 public CustomerConverter() {
 super(customerDto -> new Customer(customerDto.getCustomerId(), customerDto.getCustomerName(),
 customerDto.getCustomerLastName(), customerDto.isStatus()),
 customer -> new CustomerDto(customer.getCustomerId(), customer.getCustomerName(),
  customer.getCustomerLastName(), customer.isStatus()));

 }

}

步驟3: 創(chuàng)建customerdto類。

public class CustomerDto {
 private String customerId;
 private String customerName;
 private String customerLastName;
 private boolean status;
 public CustomerDto(String customerId, String customerName, String customerLastName, boolean status) {
  super();
  this.customerId = customerId;
  this.customerName = customerName;
  this.customerLastName = customerLastName;
  this.status = status;
 }
 public String getCustomerId() {
  return customerId;
 }
 public void setCustomerId(String customerId) {
  this.customerId = customerId;
 }
 public String getCustomerName() {
  return customerName;
 }
 public void setCustomerName(String customerName) {
  this.customerName = customerName;
 }
 public String getCustomerLastName() {
  return customerLastName;
 }
 public void setCustomerLastName(String customerLastName) {
  this.customerLastName = customerLastName;
 }
 public boolean isStatus() {
  return status;
 }
 public void setStatus(boolean status) {
  this.status = status;
 }

}

步驟4: 創(chuàng)建Customer實體類。

public class Customer {
 private String customerId;
 private String customerName;
 private String customerLastName;
 private boolean status;
 public Customer(String customerId, String customerName, String customerLastName, boolean status) {
  super();
  this.customerId = customerId;
  this.customerName = customerName;
  this.customerLastName = customerLastName;
  this.status = status;
 }
 public String getCustomerId() {
  return customerId;
 }
 public void setCustomerId(String customerId) {
  this.customerId = customerId;
 }
 public String getCustomerName() {
  return customerName;
 }
 public void setCustomerName(String customerName) {
  this.customerName = customerName;
 }
 public String getCustomerLastName() {
  return customerLastName;
 }
 public void setCustomerLastName(String customerLastName) {
  this.customerLastName = customerLastName;
 }
 public boolean isStatus() {
  return status;
 }
 public void setStatus(boolean status) {
  this.status = status;
 }
}

步驟5:  現(xiàn)在,讓我們通過創(chuàng)建Client類來測試這個模式。

public class Client {
 /**
  * Program entry point
  *
  * @param args command line args
  */
 public static void main(String[] args) {
  Converter < CustomerDto, Customer > CustomerConverter = new CustomerConverter();

  CustomerDto dtoCustomer = new CustomerDto("100", "Ramesh", "Fadatare", true);
  Customer Customer = CustomerConverter.convertFromDto(dtoCustomer);
  System.out.println("Entity converted from DTO:" + Customer);

  List < Customer > customers = new ArrayList < > ();
  customers.add(new Customer("100", "Ramesh1", "Fadatare", true));
  customers.add(new Customer("200", "Ramesh2", "Fadatare", true));
  customers.add(new Customer("300", "Ramesh3", "Fadatare", true));

  customers.forEach(System.out::println);

  customers.forEach((customer) - > System.out.println(customer.getCustomerId()));

  System.out.println("DTO entities converted from domain:");
  List < CustomerDto > dtoEntities = CustomerConverter.createFromEntities(customers);
  dtoEntities.forEach(System.out::println);
  dtoEntities.forEach((customer) - > System.out.println(customer.getCustomerId()));

 }
}

輸出:

Entity converted from DTO:com.ramesh.j2ee.converter.Customer@87aac27
com.ramesh.j2ee.converter.Customer@1b28cdfa
com.ramesh.j2ee.converter.Customer@eed1f14
com.ramesh.j2ee.converter.Customer@7229724f
100
200
300
DTO entities converted from domain:
com.ramesh.j2ee.converter.CustomerDto@4dd8dc3
com.ramesh.j2ee.converter.CustomerDto@6d03e736
com.ramesh.j2ee.converter.CustomerDto@568db2f2
100
200
300

適用性

在以下情況下 使用轉換器模式:

  • 當您擁有邏輯上與其他類型相對應的類型時,您需要在它們之間轉換實體
  • 如果要根據(jù)上下文提供不同類型的轉換方式
  • 每當您引入DTO(數(shù)據(jù)傳輸對象)時,您可能需要將其轉換為域等效。

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

相關文章

  • 淺析RxJava處理復雜表單驗證問題的方法

    淺析RxJava處理復雜表單驗證問題的方法

    這篇文章主要介紹了RxJava處理復雜表單驗證問題的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Java中final關鍵字和final的四種用法實例

    Java中final關鍵字和final的四種用法實例

    final關鍵字代表最終的、不可改變的,下面這篇文章主要給大家介紹了關于Java中final關鍵字和final的四種用法實例,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • java網(wǎng)絡編程之群聊功能

    java網(wǎng)絡編程之群聊功能

    這篇文章主要為大家詳細介紹了java網(wǎng)絡編程之群聊功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Java動態(tài)代理簡單介紹

    Java動態(tài)代理簡單介紹

    動態(tài)代理指的是,代理類和目標類的關系在程序運行的時候確定的,客戶通過代理類來調(diào)用目標對象的方法,是在程序運行時根據(jù)需要動態(tài)的創(chuàng)建目標類的代理對象。本文將通過案例詳細講解一下Java動態(tài)代理的原理及實現(xiàn),需要的可以參考一下
    2022-08-08
  • JAVA中尋找最大的K個數(shù)解法

    JAVA中尋找最大的K個數(shù)解法

    尋找最大的K個數(shù),這個是面試中比較常見的一道題,網(wǎng)上也有很多例子,在這里是比較傳統(tǒng)的解法
    2014-04-04
  • 在idea中將創(chuàng)建的java web項目部署到Tomcat中的過程圖文詳解

    在idea中將創(chuàng)建的java web項目部署到Tomcat中的過程圖文詳解

    這篇文章主要介紹了在idea中將創(chuàng)建的java web項目部署到Tomcat中的過程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • spring強行注入和引用實例解析

    spring強行注入和引用實例解析

    這篇文章主要介紹了spring強行注入和引用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • IDEA配置Maven教程的超詳細講解版

    IDEA配置Maven教程的超詳細講解版

    IntelliJ IDEA是當前最流行的Java IDE(集成開發(fā)環(huán)境)之一,也是業(yè)界公認最好用的Java開發(fā)工具之一,這篇文章主要給大家介紹了關于IDEA配置Maven教程的超詳細講解版,需要的朋友可以參考下
    2023-11-11
  • java字符串反轉的7種方法

    java字符串反轉的7種方法

    本文主要介紹了java字符串反轉的7種方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 比較排序之快速排序(實例代碼)

    比較排序之快速排序(實例代碼)

    下面小編就為大家?guī)硪黄容^排序之快速排序(實例代碼)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論