最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中轉(zhuǎn)換器設(shè)計(jì)模式深入講解

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

前言

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

目的

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

源代碼

轉(zhuǎn)換器設(shè)計(jì)模式是一種行為設(shè)計(jì)模式,允許在相應(yīng)類型(如DTO和邏輯同構(gòu)類型的域表示)之間進(jìn)行雙向轉(zhuǎn)換。此外,該模式還引入了一種在類型之間轉(zhuǎn)換對(duì)象集合的通用方法。

類圖

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

在本例中,我們將把customerd轉(zhuǎn)換為customer實(shí)體,反之亦然,我們還將在類型之間轉(zhuǎn)換對(duì)象集合。

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

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)建一個(gè)簡單客戶轉(zhuǎn)換器的實(shí)現(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實(shí)體類。

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類來測(cè)試這個(gè)模式。

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

適用性

在以下情況下 使用轉(zhuǎn)換器模式:

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

總結(jié)

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

相關(guān)文章

  • Java之Class.forName()用法案例詳解

    Java之Class.forName()用法案例詳解

    這篇文章主要介紹了Java之Class.forName()用法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 用java實(shí)現(xiàn)的獲取優(yōu)酷等視頻縮略圖的實(shí)現(xiàn)代碼

    用java實(shí)現(xiàn)的獲取優(yōu)酷等視頻縮略圖的實(shí)現(xiàn)代碼

    想獲取優(yōu)酷等視頻縮略圖,在網(wǎng)上沒有找到滿意的資料,參考了huangdijia的PHP版工具一些思路,寫了下面的JAVA版代碼。。其實(shí)也可以做成JS版的
    2013-05-05
  • JavaEE idea的smart tomcat插件使用

    JavaEE idea的smart tomcat插件使用

    這篇文章主要介紹了JavaEE idea的smart tomcat插件使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • 詳解Java設(shè)計(jì)模式——迭代器模式

    詳解Java設(shè)計(jì)模式——迭代器模式

    這篇文章主要介紹了Java設(shè)計(jì)模式——迭代器模式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java實(shí)現(xiàn)中國象棋游戲

    Java實(shí)現(xiàn)中國象棋游戲

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)中國象棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • IDEA中JetBrains Mono字體的正確安裝姿勢(shì)

    IDEA中JetBrains Mono字體的正確安裝姿勢(shì)

    在 JetBrains Mono 的設(shè)計(jì)階段,它就充分考慮到了長時(shí)間工作可能導(dǎo)致的眼睛疲勞問題,比如字母的大小和形狀、空間量、自然等寬平衡、不必要的細(xì)節(jié)、連字、以及難以區(qū)分的符號(hào)等,從而最終設(shè)計(jì)出了這么一款字體
    2021-06-06
  • 一文帶你深入了解Java中延時(shí)任務(wù)的實(shí)現(xiàn)

    一文帶你深入了解Java中延時(shí)任務(wù)的實(shí)現(xiàn)

    延時(shí)任務(wù)相信大家都不陌生,在現(xiàn)實(shí)的業(yè)務(wù)中應(yīng)用場(chǎng)景可以說是比比皆是。這篇文章主要為大家介紹幾種實(shí)現(xiàn)延時(shí)任務(wù)的辦法,感興趣的可以了解一下
    2022-11-11
  • 詳解spring切面使用傳遞給被通知方法的參數(shù)

    詳解spring切面使用傳遞給被通知方法的參數(shù)

    本篇文章主要介紹了詳解spring切面使用傳遞給被通知方法的參數(shù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • maven 使用assembly 進(jìn)行打包的方法

    maven 使用assembly 進(jìn)行打包的方法

    這篇文章主要介紹了maven 使用assembly 進(jìn)行打包的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringCloud微服務(wù)架構(gòu)升級(jí)匯總

    SpringCloud微服務(wù)架構(gòu)升級(jí)匯總

    這篇文章主要介紹了SpringCloud微服務(wù)架構(gòu)升級(jí)匯總,它提倡將單一應(yīng)用程序劃分成一組小的服務(wù),服務(wù)之間互相協(xié)調(diào)、互相配合,為用戶提供最終價(jià)值,需要的朋友可以參考下
    2019-06-06

最新評(píng)論

湖口县| 尚义县| 连云港市| 重庆市| 长汀县| 克东县| 鱼台县| 上栗县| 洪泽县| 外汇| 黑山县| 泗阳县| 新宾| 大化| 胶南市| 柞水县| 安丘市| 灵川县| 平远县| 无锡市| 英山县| 新巴尔虎右旗| 石狮市| 定州市| 西乡县| 灵宝市| 澳门| 宝坻区| 陈巴尔虎旗| 通渭县| 日喀则市| 札达县| 邮箱| 伊吾县| 闽清县| 琼海市| 朔州市| 芮城县| 景东| 沈阳市| 饶阳县|