• 转换器模式【其他模式】


    转换器模式

    public class Converter {
        /**
         *  转换器模式:允许两种类型的对象进行双向的装换,适合于 domain、dto 之间的转换
         */
        @Test
        public void all() {
            final UserConverter userConverter = UserConverter.get();
            User user = new User("kristy", "1565194365@qq.com");
            final UserDTO userDTO = userConverter.from(user);
            fieldEquals(user, userDTO);
            user = userConverter.to(userDTO);
            fieldEquals(user, userDTO);
        }
    
        private void fieldEquals(User user, UserDTO userDTO) {
            assertEquals(user.getName(), userDTO.getName());
            assertEquals(user.getEmail(), userDTO.getIdentifier());
        }
    }
    
    /**
     * 转换器核心实现
     * @param <F> 类型 F
     * @param <T> 类型 T
     */
    abstract class AConverter<F, T> {
        private final Function<F, T> from;
        private final Function<T, F> to;
    
        /**
         * @param from
         * @param to
         */
        public AConverter(Function<F, T> from, Function<T, F> to) {
            super();
            this.from = from;
            this.to = to;
        }
    
        /**
         * 单个对象间的转换
         */
    
        public T from(F f) {
            Objects.requireNonNull(f);
            return from.apply(f);
        }
    
        public F to(T t) {
            Objects.requireNonNull(t);
            return to.apply(t);
        }
    
        /**
         * 集合对象间的转换
         */
        public Collection<T> fromCollection(Collection<F> fs) {
            Objects.requireNonNull(fs);
            return fs.stream().map(from).collect(Collectors.toList());
        }
    
        public Collection<F> toCollection(Collection<T> fs) {
            Objects.requireNonNull(fs);
            return fs.stream().map(to).collect(Collectors.toList());
        }
    }
    
    @Data
    class User {
        private final String name;
        private final String email;
    }
    
    @Data
    class UserDTO {
        private final String name;
        private final String identifier;
    }
    
    class UserConverter extends AConverter<User, UserDTO> {
        private static class Holder {
            private static UserConverter INSTANCE = new UserConverter();
        }
    
        private UserConverter() {
            this(user -> {
                return new UserDTO(user.getName(), user.getEmail());
            }, userDTO -> {
                return new User(userDTO.getName(), userDTO.getIdentifier());
            });
        }
    
        public static final UserConverter get() {
            return Holder.INSTANCE;
        }
    
        private UserConverter(Function<User, UserDTO> from, Function<UserDTO, User> to) {
            super(from, to);
        }
    }
    
  • 相关阅读:
    EXTJS 基本使用
    EXTJS 常用控件的使用
    EXTJS 验证与表单提交
    EXTJS 常用方法
    禁用USB移动盘的方法
    常用sql 函数练习示例
    .Net 中的反射(反射特性) Part.3
    Delphi調用.NET的WebService
    c#写的串口通讯
    打印控制
  • 原文地址:https://www.cnblogs.com/zhuxudong/p/10192731.html
Copyright © 2020-2023  润新知