实体类转换方式

package com.iluwatar.datatransfer.customer;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 * {@link CustomerDto} is a data transfer object POJO. Instead of sending individual information to
 * client We can send related information together in POJO.
 *
 * <p>Dto will not have any business logic in it.
 */
@Getter
@RequiredArgsConstructor
public class CustomerDto {
  private final String id;
  private final String firstName;
  private final String lastName;

}

package com.iluwatar.datatransfer.customer;

import java.util.List;
import lombok.RequiredArgsConstructor;

/**
 * The resource class which serves customer information. This class act as server in the demo. Which
 * has all customer details.
 */
@RequiredArgsConstructor
public class CustomerResource {
  private final List<CustomerDto> customers;

  /**
   * Get all customers.
   *
   * @return : all customers in list.
   */
  public List<CustomerDto> getAllCustomers() {
    return customers;
  }

  /**
   * Save new customer.
   *
   * @param customer save new customer to list.
   */
  public void save(CustomerDto customer) {
    customers.add(customer);
  }

  /**
   * Delete customer with given id.
   *
   * @param customerId delete customer with id {@code customerId}
   */
  public void delete(String customerId) {
    customers.removeIf(customer -> customer.getId().equals(customerId));
  }
}

package com.iluwatar.datatransfer.product;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * {@link Product} is a entity class for product entity. This class act as entity in the demo.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public final class Product {
  private Long id;
  private String name;
  private Double price;
  private Double cost;
  private String supplier;

  @Override
  public String toString() {
    return "Product{"
            + "id=" + id
            + ", name='" + name + '\''
            + ", price=" + price
            + ", cost=" + cost
            + ", supplier='" + supplier + '\''
            + '}';
  }
}

/*
 * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
 *
 * The MIT License
 * Copyright © 2014-2022 Ilkka Seppälä
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.iluwatar.datatransfer.product;

/**
 * {@link ProductDto} is a data transfer object POJO.
 * Instead of sending individual information to
 * client We can send related information together in POJO.
 *
 * <p>Dto will not have any business logic in it.
 */
public enum ProductDto {
  ;

  /**
   * This is Request class which consist of Create or any other request DTO's
   * you might want to use in your API.
   */
  public enum Request {
    ;

    /**
     * This is Create dto class for requesting create new product.
     */
    public static final class Create implements Name, Price, Cost, Supplier {
      private String name;
      private Double price;
      private Double cost;
      private String supplier;

      @Override
      public String getName() {
        return name;
      }

      public Create setName(String name) {
        this.name = name;
        return this;
      }

      @Override
      public Double getPrice() {
        return price;
      }

      public Create setPrice(Double price) {
        this.price = price;
        return this;
      }

      @Override
      public Double getCost() {
        return cost;
      }

      public Create setCost(Double cost) {
        this.cost = cost;
        return this;
      }

      @Override
      public String getSupplier() {
        return supplier;
      }

      public Create setSupplier(String supplier) {
        this.supplier = supplier;
        return this;
      }
    }
  }

  /**
   * This is Response class which consist of any response DTO's
   * you might want to provide to your clients.
   */
  public enum Response {
    ;

    /**
     * This is Public dto class for API response with the lowest data security.
     */
    public static final class Public implements Id, Name, Price {
      private Long id;
      private String name;
      private Double price;

      @Override
      public Long getId() {
        return id;
      }

      public Public setId(Long id) {
        this.id = id;
        return this;
      }

      @Override
      public String getName() {
        return name;
      }

      public Public setName(String name) {
        this.name = name;
        return this;
      }

      @Override
      public Double getPrice() {
        return price;
      }

      public Public setPrice(Double price) {
        this.price = price;
        return this;
      }

      @Override
      public String toString() {
        return "Public{"
            + "id="
            + id
            + ", name='"
            + name
            + '\''
            + ", price="
            + price
            + '}';
      }
    }

    /**
     * This is Private dto class for API response with the highest data security.
     */
    public static final class Private implements Id, Name, Price, Cost {
      private Long id;
      private String name;
      private Double price;
      private Double cost;

      @Override
      public Long getId() {
        return id;
      }

      public Private setId(Long id) {
        this.id = id;
        return this;
      }

      @Override
      public String getName() {
        return name;
      }

      public Private setName(String name) {
        this.name = name;
        return this;
      }

      @Override
      public Double getPrice() {
        return price;
      }

      public Private setPrice(Double price) {
        this.price = price;
        return this;
      }

      @Override
      public Double getCost() {
        return cost;
      }

      public Private setCost(Double cost) {
        this.cost = cost;
        return this;
      }

      @Override
      public String toString() {
        return "Private{"
            +
            "id="
            + id
            +
            ", name='"
            + name
            + '\''
            +
            ", price="
            + price
            +
            ", cost="
            + cost
            +
            '}';
      }
    }
  }

  /**
   * Use this interface whenever you want to provide the product Id in your DTO.
   */
  private interface Id {
    /**
     * Unique identifier of the product.
     *
     * @return : id of the product.
     */
    Long getId();
  }

  /**
   * Use this interface whenever you want to provide the product Name in your DTO.
   */
  private interface Name {
    /**
     * The name of the product.
     *
     * @return : name of the product.
     */
    String getName();
  }

  /**
   * Use this interface whenever you want to provide the product Price in your DTO.
   */
  private interface Price {
    /**
     * The amount we sell a product for.
     * <b>This data is not confidential</b>
     *
     * @return : price of the product.
     */
    Double getPrice();
  }

  /**
   * Use this interface whenever you want to provide the product Cost in your DTO.
   */
  private interface Cost {
    /**
     * The amount that it costs us to purchase this product
     * For the amount we sell a product for, see the {@link Price Price} parameter.
     * <b>This data is confidential</b>
     *
     * @return : cost of the product.
     */
    Double getCost();
  }

  /**
   * Use this interface whenever you want to provide the product Supplier in your DTO.
   */
  private interface Supplier {
    /**
     * The name of supplier of the product or its manufacturer.
     * <b>This data is highly confidential</b>
     *
     * @return : supplier of the product.
     */
    String getSupplier();
  }
}

/*
 * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
 *
 * The MIT License
 * Copyright © 2014-2022 Ilkka Seppälä
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.iluwatar.datatransfer.product;

import java.util.List;
import java.util.stream.Collectors;

/**
 * The resource class which serves product information. This class act as server in the demo. Which
 * has all product details.
 */
public class ProductResource {
  private final List<Product> products;

  /**
   * Initialise resource with existing products.
   *
   * @param products initialize resource with existing products. Act as database.
   */
  public ProductResource(final List<Product> products) {
    this.products = products;
  }

  /**
   * Get all products.
   *
   * @return : all products in list but in the scheme of private dto.
   */
  public List<ProductDto.Response.Private> getAllProductsForAdmin() {
    return products
            .stream()
            .map(p -> new ProductDto.Response.Private().setId(p.getId()).setName(p.getName())
                    .setCost(p.getCost())
                    .setPrice(p.getPrice()))
            .collect(Collectors.toList());
  }

  /**
   * Get all products.
   *
   * @return : all products in list but in the scheme of public dto.
   */
  public List<ProductDto.Response.Public> getAllProductsForCustomer() {
    return products
            .stream()
            .map(p -> new ProductDto.Response.Public().setId(p.getId()).setName(p.getName())
                    .setPrice(p.getPrice()))
            .collect(Collectors.toList());
  }

  /**
   * Save new product.
   *
   * @param createProductDto save new product to list.
   */
  public void save(ProductDto.Request.Create createProductDto) {
    products.add(Product.builder()
            .id((long) (products.size() + 1))
            .name(createProductDto.getName())
            .supplier(createProductDto.getSupplier())
            .price(createProductDto.getPrice())
            .cost(createProductDto.getCost())
            .build());
  }

  /**
   * List of all products in an entity representation.
   *
   * @return : all the products entity that stored in the products list
   */
  public List<Product> getProducts() {
    return products;
  }
}

/*
 * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
 *
 * The MIT License
 * Copyright © 2014-2022 Ilkka Seppälä
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.iluwatar.datatransfer;

import com.iluwatar.datatransfer.customer.CustomerDto;
import com.iluwatar.datatransfer.customer.CustomerResource;
import com.iluwatar.datatransfer.product.Product;
import com.iluwatar.datatransfer.product.ProductDto;
import com.iluwatar.datatransfer.product.ProductResource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

/**
 * The Data Transfer Object pattern is a design pattern in which an data transfer object is used to
 * serve related information together to avoid multiple call for each piece of information.
 *
 * <p>In the first example, {@link App} is a customer details consumer i.e. client to
 * request for customer details to server. {@link CustomerResource} act as server to serve customer
 * information. {@link CustomerDto} is data transfer object to share customer information.
 *
 * <p>In the second example, {@link App} is a product details consumer i.e. client to
 * request for product details to server. {@link ProductResource} acts as server to serve
 * product information. {@link ProductDto} is data transfer object to share product information.
 *
 * <p>The pattern implementation is a bit different in each of the examples. The first can be
 * thought as a traditional example and the second is an enum based implementation.
 *
 */
@Slf4j
public class App {

  /**
   * Method as act client and request to server for details.
   *
   * @param args program argument.
   */
  public static void main(String[] args) {

    // Example 1: Customer DTO
    var customerOne = new CustomerDto("1", "Kelly", "Brown");
    var customerTwo = new CustomerDto("2", "Alfonso", "Bass");
    var customers = new ArrayList<>(List.of(customerOne, customerTwo));

    var customerResource = new CustomerResource(customers);

    LOGGER.info("All customers:-");
    var allCustomers = customerResource.getAllCustomers();
    printCustomerDetails(allCustomers);

    LOGGER.info("----------------------------------------------------------");

    LOGGER.info("Deleting customer with id {1}");
    customerResource.delete(customerOne.getId());
    allCustomers = customerResource.getAllCustomers();
    printCustomerDetails(allCustomers);

    LOGGER.info("----------------------------------------------------------");

    LOGGER.info("Adding customer three}");
    var customerThree = new CustomerDto("3", "Lynda", "Blair");
    customerResource.save(customerThree);
    allCustomers = customerResource.getAllCustomers();
    printCustomerDetails(allCustomers);

    // Example 2: Product DTO

    Product tv = Product.builder().id(1L).name("TV").supplier("Sony").price(1000D).cost(1090D).build();
    Product microwave =
        Product.builder()
            .id(2L)
            .name("microwave")
            .supplier("Delonghi")
            .price(1000D)
            .cost(1090D).build();
    Product refrigerator =
        Product.builder()
            .id(3L)
            .name("refrigerator")
            .supplier("Botsch")
            .price(1000D)
            .cost(1090D).build();
    Product airConditioner =
        Product.builder()
            .id(4L)
            .name("airConditioner")
            .supplier("LG")
            .price(1000D)
            .cost(1090D).build();
    List<Product> products =
        new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
    ProductResource productResource = new ProductResource(products);

    LOGGER.info(
        "####### List of products including sensitive data just for admins: \n {}",
        Arrays.toString(productResource.getAllProductsForAdmin().toArray()));
    LOGGER.info(
        "####### List of products for customers: \n {}",
        Arrays.toString(productResource.getAllProductsForCustomer().toArray()));

    LOGGER.info("####### Going to save Sony PS5 ...");
    ProductDto.Request.Create createProductRequestDto =
        new ProductDto.Request.Create()
            .setName("PS5")
            .setCost(1000D)
            .setPrice(1220D)
            .setSupplier("Sony");
    productResource.save(createProductRequestDto);
    LOGGER.info(
        "####### List of products after adding PS5: {}",
        Arrays.toString(productResource.getProducts().toArray()));
  }

  private static void printCustomerDetails(List<CustomerDto> allCustomers) {
    allCustomers.forEach(customer -> LOGGER.info(customer.getFirstName()));
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值