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<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类。
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", "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()));
}
}