zoukankan      html  css  js  c++  java
  • Separate Query From Modify(将查询函数和修改函数分离)

    //重构前    
    String foundMiscreant(String[] people){
            for (int i = 0; i<people.length; i++){
                if (people[i].equals("Dom")){
                    sendAlert();
                    return "Dom";
                }
    
                if (people[i].equals("John")){
                   sendAlert();
                   return "John";
                }
            }
            return "";
        }
    
        private void sendAlert() {
            System.out.println("发出一条警告");
        }
    
        public static void main(String[] args) {
            Before before = new Before();
            String[] people = new String[3];
            people[0] = "Dom";
            people[1] = "Dom";
            people[2] = "Dom";
            System.out.println(before.foundMiscreant(people));
        }
    
     String foundPeople(String[] people){
            //1.先建立一个适当的查询函数,使其与修改函数返回相同的值,但不造成任何副作用
            for (int i = 0; i<people.length; i++){
                if (people[i].equals("Dom")){
                    return "Dom";
                }
    
                if (people[i].equals("John")){
                    return "John";
                }
            }
            return "";
        }
    
        String foundMiscreant(String[] people){
            //2.把原函数内所有的return语句,改调用新建的查询函数
            for (int i = 0; i<people.length; i++){
                if (people[i].equals("Dom")){
                    sendAlert();
                    return foundPeople(people);
                }
    
                if (people[i].equals("John")){
                    sendAlert();
                    return foundPeople(people);
                }
            }
            return foundPeople(people);
        }
    
        private void sendAlert() {
            System.out.println("发出一条警告");
        }
    
        public static void main(String[] args) {
            //3.修改调用函数,将原本单一调用,修改为两个调用
            After1 after1 = new After1();
            String[] people = new String[3];
            people[0] = "Dom";
            people[1] = "Dom";
            people[2] = "Dom";
            after1.foundMiscreant(people);
            System.out.println(after1.foundPeople(people));
        }
    
    String foundPeople(String[] people){
            for (int i = 0; i<people.length; i++){
                if (people[i].equals("Dom")){
                    return "Dom";
                }
    
                if (people[i].equals("John")){
                    return "John";
                }
            }
            return "";
        }
    
        void foundMiscreant(String[] people){
            //4.将原函数改为返回void,删除所有return语句
            for (int i = 0; i<people.length; i++){
                if (people[i].equals("Dom")){
                    sendAlert();
                    return;
                }
    
                if (people[i].equals("John")){
                    sendAlert();
                    return;
                }
            }
        }
    
        private void sendAlert() {
            System.out.println("发出一条警告");
        }
    
        public static void main(String[] args) {
            After2 after2 = new After2();
            String[] people = new String[3];
            people[0] = "Dom";
            people[1] = "Dom";
            people[2] = "Dom";
            after2.foundMiscreant(people);
            System.out.println(after2.foundPeople(people));
        }
    
  • 相关阅读:
    特征选择常用算法综述
    干货:结合Scikit-learn介绍几种常用的特征选择方法
    机器学习中,有哪些特征选择的工程方法?
    牛逼的博客地址
    Discover Feature Engineering, How to Engineer Features and How to Get Good at It
    机器学习中的Bias(偏差),Error(误差),和Variance(方差)有什么区别和联系?
    机器学习中使用「正则化来防止过拟合」到底是一个什么原理?为什么正则化项就可以防止过拟合?
    Libsvm和Liblinear的使用经验谈
    Python 由list转为dictionary
    使用 numpy.random.choice随机采样
  • 原文地址:https://www.cnblogs.com/haocang/p/12693636.html
Copyright © 2011-2022 走看看