十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍了Java中转换器设计模式的作用是什么,创新互联小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随创新互联小编来看看吧!
创新互联专注于监利网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供监利营销型网站建设,监利网站制作、监利网页设计、监利网站官网定制、成都微信小程序服务,打造监利网络公司原创品牌,更为您提供监利网站排名全网营销落地服务。
Java主要应用于:1. web开发;2. Android开发;3. 客户端开发;4. 网页开发;5. 企业级应用开发;6. Java大数据开发;7.游戏开发等。
目的
转换器设计模式的目的是为相应类型之间的双向转换提供一种通用的方式,允许类型无需彼此了解的简洁的实现。此外,转换器设计模式引入了双向收集映射,将样板代码减少到最小。
源代码
转换器设计模式是一种行为设计模式,允许在相应类型(如DTO和逻辑同构类型的域表示)之间进行双向转换。此外,该模式还引入了一种在类型之间转换对象集合的通用方法。
类图
让我们根据上面的类图编写源代码。
在本例中,我们将把customerd转换为customer实体,反之亦然,我们还将在类型之间转换对象集合。
步骤1:让我们创建一个通用转换器。
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:让我们创建一个简单客户转换器的实现。
public class CustomerConverter extends Converter{ 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: 创建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: 创建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: 现在,让我们通过创建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", "Ramesh2", "Fadatare", true)); customers.add(new Customer("200", "Ramesh3", "Fadatare", true)); customers.add(new Customer("300", "Ramesh4", "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
适用性
在以下情况下 使用转换器模式:
当您拥有逻辑上与其他类型相对应的类型时,您需要在它们之间转换实体
如果要根据上下文提供不同类型的转换方式
每当您引入DTO(数据传输对象)时,您可能需要将其转换为域等效。
以上就是创新互联小编为大家收集整理的Java中转换器设计模式的作用是什么,如何觉得创新互联网站的内容还不错,欢迎将创新互联网站推荐给身边好友。