Java中转换器设计模式深入讲解

时间:2022-09-05 13:42:16

前言

在这篇文章中,我们将讨论 java / j2ee项目中最常用的  converter design pattern。由于java8 功能不仅提供了相应类型之间的通用双向转换方式,而且还提供了转换相同类型对象集合的常用方法,从而将样板代码减少到绝对最小值。我们使用java8 功能编写了此模式的源代码。

目的

转换器设计模式的目的是为相应类型之间的双向转换提供一种通用的方式,允许类型无需彼此了解的简洁的实现。此外,转换器设计模式引入了双向收集映射,将样板代码减少到最小。

源代码

转换器设计模式是一种行为设计模式,允许在相应类型(如dto和逻辑同构类型的域表示)之间进行双向转换。此外,该模式还引入了一种在类型之间转换对象集合的通用方法。

类图

Java中转换器设计模式深入讲解

让我们根据上面的类图编写源代码。

在本例中,我们将把customerd转换为customer实体,反之亦然,我们还将在类型之间转换对象集合。

步骤1:让我们创建一个通用转换器。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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:让我们创建一个简单客户转换器的实现。

?
1
2
3
4
5
6
7
8
9
10
11
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: 创建customerdto类。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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实体类。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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类来测试这个模式。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

适用性

在以下情况下 使用转换器模式:

  • 当您拥有逻辑上与其他类型相对应的类型时,您需要在它们之间转换实体
  • 如果要根据上下文提供不同类型的转换方式
  • 每当您引入dto(数据传输对象)时,您可能需要将其转换为域等效。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。

原文链接:https://www.jdon.com/52040