Code optimization-polymorphism instead of IF conditional judgment

Scene description

In development scenarios, discounted business needs are often encountered, and each user's level of discount is different.For example, 9% discount for ordinary members, 8.5% discount for bronze members, 8% discount for gold members, etc.In general development, the easiest thing is to determine the user's level and then discount the order accordingly.

Scene example

Write a simple small example, as follows:

//1 for student 2 for teacher 3 for principal
int type = 1;
if (1 == type) {
    System.out.println("Student giggle talk");
} else if (2 == type) {
    System.out.println("Teacher's happy talk");
} else {
    System.out.println("The headmaster's serious remark");
}

The code above is what we often do. When there is less code, it looks very clear, but when there is more code or more judgment, the code above will be more confusing. If there are changes, change this part of the code every time.

Solution

You can change the code above to polymorphic and create three classes, Student, Teacher, HeadMater, and Person, all of which implement the parent's method say, as follows:

Person.class

package me.xueyao.service;

/**
 * @author Simon.Xue
 * @date 2019-12-01 14:31
 **/
public interface Person {
    void say();
}

Student.class

package me.xueyao.service.impl;

import me.xueyao.service.Person;
import org.springframework.stereotype.Service;

/**
 * @author Simon.Xue
 * @date 2019-12-01 14:34
 **/
@Service
public class Student implements Person {
    @Override
    public void say() {
        System.out.println("Student giggle talk");
    }
}

Teacher.class

package me.xueyao.service.impl;

import me.xueyao.service.Person;
import org.springframework.stereotype.Service;

/**
 * @author Simon.Xue
 * @date 2019-12-01 14:37
 **/
@Service
public class Teacher implements Person {
    @Override
    public void say() {
        System.out.println("Teacher's happy talk");
    }
}

HeadMaster.class

package me.xueyao.service.impl;

import me.xueyao.service.Person;
import org.springframework.stereotype.Service;

/**
 * @author Simon.Xue
 * @date 2019-12-01 14:41
 **/
@Service
public class HeadMaster implements Person {

    @Override
    public void say() {
        System.out.println("The headmaster's serious remark");
    }
}

test method

@Test
public void testSay() {
    Person student = new Student();
    student.say();

    Person teacher = new Teacher();
    teacher.say();

    Person headMaster = new HeadMaster();
    headMaster.say();
}

optimization

This is basically an optimization, but we'll also find the problem that each time we create an object.There are three classes above, so we need to create three objects. Can we optimize them again?

Now that the project uses the Sping framework, injection can be used to complete the optimization.

First, create a Person enumeration class, as follows:

package me.xueyao.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import me.xueyao.service.impl.HeadMaster;
import me.xueyao.service.impl.Student;
import me.xueyao.service.impl.Teacher;

/**
 * @author Simon.Xue
 * @date 2019-12-01 15:55
 **/
@AllArgsConstructor
@Getter
public enum  PersonEnums {
    STUDENT(1, "Student", Student.class),
    TEACHER(2, "Teacher", Teacher.class),
    HEADMASTER(3, "Principal", HeadMaster.class);

    Integer code;
    String msg;
    Class clazz;

    /**
     * Get the name of the class because when Spring is automatically injected, the default name is the class name (lowercase first letter)
     * @param code
     * @return
     */
    public static String className(Integer code) {
        for (PersonEnums value : values()) {
            if (value.getCode().equals(code)) {
                String simpleName = value.getClazz().getSimpleName();
                simpleName.substring(1);
                return String.valueOf(simpleName.charAt(0)).toLowerCase() + simpleName.substring(1);
            }
        }
        return "";
    }

}

How to use:

@Autowired
private Map<String, Person> personMap = new HashMap<>();
@Test
public void testSay() {
    personMap.get(PersonEnums.className(2)).say();
}

Tags: Java Lombok less Spring

Posted on Mon, 02 Dec 2019 06:33:42 -0800 by serial