• Java 建造者Builder


    import java.util.ArrayList;
    import java.util.List;
    import java.util.function.Consumer;
    import java.util.function.Supplier;
    
    public class Builder<T> {
    
        private final Supplier<T> instantiator;
    
        private List<Consumer<T>> modifiers = new ArrayList<>();
    
        public Builder(Supplier<T> instantiator) {
            this.instantiator = instantiator;
        }
    
        public static <T> Builder<T> of(Supplier<T> instantiator){
            return new Builder<>(instantiator);
        }
    
        public <P1> Builder<T> with(Consumer1<T,P1> consumer1,P1 p1){
            Consumer<T> c = instance -> consumer1.accept(instance,p1);
            modifiers.add(c);
            return this;
        }
    
        public interface Consumer1<T,P1> {
            void accept(T t,P1 p1);
        }
    
        public static void main(String[] args) {
    
            Builder.of(TestVo::new).with(TestVo::setId,"id值");
            Builder.of(TestVo::new).with((t,p1) -> t.setId(p1),"id值");
    
        }
    
    }

    多余的就不说了,我相信代码Debug以下都能看懂

    jdk8新特性

    复制请注明出处,在世界中挣扎的灰太狼
  • 相关阅读:
    ACdream 1069 无耻的出题人
    ACdream 1064 完美数
    ACdream 1028 Path
    ACdream 1020 The Game about KILL
    ACdream 1015 Double Kings
    CodeForces
    Codeforces 390A( 模拟题)
    Codeforces 389B(十字模拟)
    Codeforces 389A (最大公约数)
    Codeforces 417 C
  • 原文地址:https://www.cnblogs.com/XingXiaoMeng/p/13927915.html
Copyright © 2020-2023  润新知