• ChapterOne


    learning ...

    #ChapterOne

    RuleOne

    package chapter01.rule01;
    
    import lombok.Data;
    
    /**
     * 静态工厂
     * @author 82341
     */
    @Data
    public class staticFactoryMethod {
        private String name;
        private Integer age;
        private staticFactoryMethod(){}
    
        public static staticFactoryMethod factoryMethod(){
            return new staticFactoryMethod();
        }
    
        public staticFactoryMethod name(String name){
            this.name = name;
            return this;
        }
        public staticFactoryMethod age(Integer age){
            this.age = age;
            return this;
        }
    
        public static void main(String[] args) {
            //流式API
            new staticFactoryMethod().name("chz").age(23);
        }
    }
    
    

    RuleTwo

    package chapter01.rule02;
    
    import lombok.Data;
    
    @Data
    public class Person {
        private String name;
        private Integer age;
        private Boolean gender;
        private Double salary;
    
        private Person(Builder builder) {
            name = builder.name;
            age = builder.age;
            gender = builder.gender;
            salary = builder.salary;
        }
    
        @Data
        public static class Builder {
            private String name;
            private Integer age;
            private Boolean gender;
            private Double salary;
    
            public Builder(String name, Integer age) {
                //这里的this指的时静态内部类
                this.name = name;
                this.age = age;
            }
    
            public Builder gender(Boolean gender) {
                this.gender = gender;
                return this;
            }
    
            public Builder salary(Double salary) {
                this.salary = salary;
                return this;
            }
    
            /**
             * 关键的一步, 将Builder获取到的值传给Person
             */
            public Person build() {
                return new Person(this);
            }
        }
        public static void main(String[] args) {
            Person person = new Builder("chz", 23)
                    .gender(Boolean.TRUE).salary(200D)
                    .build();
            System.out.println(person);
        }
    }
    

    RuleThree

    package chapter01.rule03;
    
    
    /**
     * 单例模式一
     */
    class Elvis {
        //使用公开的类变量
        public static final Elvis elvis = new Elvis();
    
        private Elvis() {
            System.out.println("Elvis is unique");
        }
    
        public void sing() {
            System.out.println("sing out of tune");
        }
    }
    
    /**
     * 单例模式二
     */
    class Bowie {
        private static final Bowie bowie = new Bowie();
    
        private Bowie() {
            System.out.println("rain man");
        }
    
        public static Bowie getInstance() {
            return bowie;
        }
    
        public void sing() {
            System.out.println("sing out of tune");
        }
    
    }
    
    /**
     * 单例模式三, 推荐
     * enum无偿提供序列化
     * 反编译的Enum对象自动实现Serializable
     */
    public enum Teacher {
        INSTANCE("zs", 22);
        private final String name;
        private final Integer age;
    
        Teacher(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void say() {
            System.out.println("hello world");
        }
    
    
        public static void main(String[] args) {
            Elvis.elvis.sing();
            Bowie.getInstance().sing();
            Teacher.INSTANCE.say();
        }
    }
    
    

    #RuleFour

    对于工具类, 因该要私有化构造器, 避免工具类实例化, 同时工具类将不能被实例化

    #RuleFive

    当创建一个新的实例时, 就将该资源传入到构造器中. 这就是依赖注入(dependency injection)

    As follows:

    class SpellChecker{
        private final Lexicon dictionary;
        public SpellChecker(Lexicon dictionary){
            this.dictionary = Objects.requireNonNull(dictionary);
        }
    }
    

    #RuleSix

    • 对于创建成本高的对象, 要将对象缓存下来

      As follows:

    public class RomanNumberals {
        private static final Pattern ROMAN  =  Pattern.compile("");
        static boolean isRoman(String s){
            //创建matcher并判断是否匹配对应的正则
            return  ROMAN.matcher(s).matches();
        }
    }
    

    这里的final作用是将对象存入常量池中, 以免被gabage collector回收, 造成不必要的性能浪费

    • 要优先使用基本数据类型, 而不是装箱基本类型, 当心无意识的自动装箱.

      创建多余的对象会降低性能

      As follows:

    String s = new String("jetbrain");
    Integer i = new Integer(23);
    

    但是对于domain object 应该使用装箱的基本类型, 防止基本类型的初始化赋值

    #RuleSeven

  • 相关阅读:
    [原创]SQL 把表中某一个列按照逗号拼接成一行
    [原创]SQL 把表中字段存储的逗号隔开内容转换成列表形式
    JQuery DataTables相关
    Jquery相关
    json字串主从表无法转成对像
    FastReport.net 使用 WebForm 实现打印 最简单版
    c# .netframwork 4.0 调用 2.0时报错 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
    FastReport.net 使用 Winform WebForm打印
    asp.net dev xtraReporting(一)静态页面
    C# DataTable转json 时间格式化
  • 原文地址:https://www.cnblogs.com/kikochz/p/12941334.html
Copyright © 2020-2023  润新知