[version is up to you] - 3-encapsulate and reuse if judgment logic, and modify it uniformly when changing

Posted by jdorma0 on Sun, 09 Jan 2022 11:33:57 +0100

0. Background

  usually, when we write business logic, we will encounter many similar judgment logic. Next, let's take giving gifts to users as an example to make a package for you.

1. Issue coupons to users

  suppose that our user id is a serial number with an int value. Written from 1, isn't it 2022 now? When our fast-food restaurant celebrated its anniversary, the boss said he would send a 1-cent coupon to all users whose id is a multiple of 20. So the programmer Xiao Sun wrote plainly:

public class OriginalTest {

    static class User {

        private int id;

        public int getId() {
            return id;
        }

        public User(int id) {
            this.id = id;
        }
    }

    public static void main(String[] args) {
        User user = new User(20);
        if (user.getId() % 20 == 0) {
            System.out.println("Congratulations on getting a one cent coupon");
        }
    }

}

   after a while, the boss who scratched his head said again: "I didn't think about it just now. Isn't this year the end of 22? Send a 2-cent coupon to all users whose id is a multiple of 22". Xiao Sun said helplessly, "well, I'll change it."

public class OriginalTest {

    static class User {

        private int id;

        public int getId() {
            return id;
        }

        public User(int id) {
            this.id = id;
        }
    }

    public static void main(String[] args) {
        User user = new User(22);
        if (user.getId() % 20 == 0) {
            System.out.println("Congratulations on getting a one cent coupon");
        }
        if (user.getId() % 22 == 0) {
            System.out.println("Congratulations on getting a 2-cent coupon");
        }
    }

}

   before it was over, the boss patted his head, and then said to Xiao Sun, "is it online? Add another rule. If it is a multiple of 20 and 22 at the same time, not only these coupons, but also a avatar pendant." Xiao Sun chuckled, "well, I'll change it."

public class OriginalTest {

    static class User {

        private int id;

        public int getId() {
            return id;
        }

        public User(int id) {
            this.id = id;
        }
    }

    public static void main(String[] args) {
        User user = new User(220);
        if (user.getId() % 20 == 0) {
            System.out.println("Congratulations on getting a one cent coupon");
        }
        if (user.getId() % 22 == 0) {
            System.out.println("Congratulations on getting a 2-cent coupon");
        }
        if (user.getId() % 20 == 0 && user.getId() % 22 == 0) {
            System.out.println("Congratulations on getting a avatar Pendant");
        }
    }

}

  the boss looked at the program written by Xiao Sun and was very satisfied. Turn around and torture programmer Xiao Huang: "what, you manage orders. If our new order number appears to be a multiple of 20, send a penny coupon."

  Xiao Huang was a little difficult. He found Xiao Sun: "how to do it? Do you have any written code?"

  Xiao Sun retorted, "you are the order department and I am the user department. Can the code be used indiscriminately?"

  Xiao Huang was a little angry: "you don't know the boss. It's good to keep it until tomorrow for this temporary demand!"

  Xiao Sun was a little impatient: "well, I'll extract a public tool class."

public class UtilTest {

    static class User {

        private int id;

        public int getId() {
            return id;
        }

        public User(int id) {
            this.id = id;
        }
    }

    static class DiscountCouponUtil {

        public static boolean oneCent(int number) {
            if (number % 20 == 0) {
                return true;
            }
            return false;
        }

        public static boolean twoCents(int number) {
            if (number % 22 == 0) {
                return true;
            }
            return false;
        }

        public static boolean headPendant(int number) {
            return oneCent(number) && twoCents(number);
        }
    }

    public static void main(String[] args) {
        User user = new User(220);
        int userId = user.getId();
        if (DiscountCouponUtil.oneCent(userId)) {
            System.out.println("Congratulations on getting a one cent coupon");
        }
        if (DiscountCouponUtil.twoCents(userId)) {
            System.out.println("Congratulations on getting a 2-cent coupon");
        }
        if (DiscountCouponUtil.headPendant(userId)) {
            System.out.println("Congratulations on getting a avatar Pendant");
        }
    }

}

2. Send lucky gift bag to users

    just as the code was about to go online, the boss hurried to the smashing universe software development department and shouted, "I just patted my thigh and thought of something. We'll send another lucky gift bag to people whose user id is multiple of 20 or multiple of 22. You can change it temporarily, and I'll take it right away!"

  Xiao Sun said, "what can I do? The code is all messed up! Shishan carved flowers, now!" The boss patted his chest: "I promise it's the last modification. All right, all right, do it quickly."

Make complaints about Xiao Sun's Tucao. He asked Xiao Sun what was going on. Xiao Sun told him what had happened. He smiled and said, "you can use predict to make you feel better." Xiao Sun stared: "what? What Cate?"

  Lao an calmly helped him change the code: "let's write a judgment class first."

public class DiscountCouponPredicateUtil {

    public static class DividedByTwenty implements Predicate<Integer> {

        @Override
        public boolean test(Integer integer) {
            if (integer % 20 == 0) {
                return true;
            }
            return false;
        }
    }

    public static class DividedByTwentyTwo implements Predicate<Integer> {

        @Override
        public boolean test(Integer integer) {
            if (integer % 22 == 0) {
                return true;
            }
            return false;
        }
    }

}

  then we use them in business code.

public class PredicateTest {

    static class User {

        private int id;

        public int getId() {
            return id;
        }

        public User(int id) {
            this.id = id;
        }
    }

    private static final DiscountCouponPredicateUtil.DividedByTwenty DIVIDED_BY_TWENTY_PRE = new DiscountCouponPredicateUtil.DividedByTwenty();

    private static final DiscountCouponPredicateUtil.DividedByTwentyTwo DIVIDED_BY_TWENTY_TWO_PRE = new DiscountCouponPredicateUtil.DividedByTwentyTwo();


    public static void main(String[] args) {
        User user = new User(220);
        int userId = user.getId();
        if (DIVIDED_BY_TWENTY_PRE.test(userId)) {
            System.out.println("Congratulations on getting a one cent coupon");
        }
        if (DIVIDED_BY_TWENTY_TWO_PRE.test(userId)) {
            System.out.println("Congratulations on getting a 2-cent coupon");
        }
        if (DIVIDED_BY_TWENTY_PRE.and(DIVIDED_BY_TWENTY_TWO_PRE).test(userId)) {
            System.out.println("Congratulations on getting a avatar Pendant");
        }
        if (DIVIDED_BY_TWENTY_PRE.or(DIVIDED_BY_TWENTY_TWO_PRE).test(userId)) {
            System.out.println("Congratulations on getting a lucky gift bag");
        }
    }

}

  Xiao Sun was surprised and said, "it's true that the sense of logic is better. What's the advantage of this Predicate?"

   Lao an smiled and said, "of course, you can sort out the relevant judgment logic. If it is a long-term demand, it is also easy to change it when modifying. For example..."

   as he was saying this, the boss hurried over: "I just patted my head and came up with a wonderful idea. Why don't we start a long-term activity of the lucky number? We change the rules of the lucky number every year, but we can do the lucky number activity every year, so that users can look forward to the lucky number activity like a set of five blessings."

  Lao an smiled and said, "Xiao Sun, this lucky number can be written as a Predicate, and all the places used can be quoted. When it changes the next year, it can be modified uniformly."

public class DiscountCouponPredicateUtil {

    public static class LuckyNumberPredicate implements Predicate<Integer> {
        
        @Override
        public boolean test(Integer integer) {
            if (integer % 20 == 0 && integer % 22 == 0) {
                return true;
            }
            return false;
        }
    }

}

  when the boss saw Lao an smiling, he smiled and said, "it's easy to change, isn't it? Can we go online later?"

  the office is quiet again

epilogue

   in this example, we learned how to extract the judgment logic and how to uniformly modify the logic code expressing the same business meaning in business judgment. This is a powerful tool for Java 8 to encapsulate and reuse judgment logic.

  readers, what do you want to share, a good predict application? You are welcome to discuss with other readers in the comment area!

Topics: Java