Scene description

In the development scenario, it is common to encounter discounted business requirements, and each user level, their discount situation is also different. For example, regular members get 10% off, bronze members get 15% off, gold members get 20% off and so on. In the general development of the simplest is to judge the user level, and then the order for the corresponding discount processing.

Sample scenario

A simple small example is written as follows:

//1 is student 2 teacher 3 principal
int type = 1;
if (1 == type) {
    System.out.println("The student speaks with a smile.");
} else if (2 == type) {
    System.out.println("The teacher speaks happily.");
} else {
    System.out.println("Serious words from the Principal.");
}
Copy the code

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

The solution

Student, Teacher, Teacher, HeadMater, Person, Student, Teacher, HeadMater, Person, Student, Teacher, HeadMater

Person.class

package me.xueyao.service;

/ * * *@author Simon.Xue
 * @dateThe 2019-12-01 o * * /
public interface Person {
    void say(a);
}
Copy the code

Student.class

package me.xueyao.service.impl;

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

/ * * *@author Simon.Xue
 * @dateThe number 2019-12-01 * * /
@Service
public class Student implements Person {
    @Override
    public void say(a) {
        System.out.println("The student speaks with a smile."); }}Copy the code

Teacher.class

package me.xueyao.service.impl;

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

/ * * *@author Simon.Xue
 * @dateIn the 2019-12-01 s and he * * /
@Service
public class Teacher implements Person {
    @Override
    public void say(a) {
        System.out.println("The teacher speaks happily."); }}Copy the code

HeadMaster.class

package me.xueyao.service.impl;

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

/ * * *@author Simon.Xue
 * @dateIn the 2019-12-01 s he cometh * * /
@Service
public class HeadMaster implements Person {

    @Override
    public void say(a) {
        System.out.println("Serious words from the Principal."); }}Copy the code

The test method

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

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

    Person headMaster = new HeadMaster();
    headMaster.say();
}
Copy the code

To optimize the

This is basically optimization done, but there is a problem that we still have to create the corresponding object each time. We have three classes up here, we’re going to create three objects, can we optimize that again?

Because the project now uses the Sping framework, it can be optimized with injection.

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
 * @dateThe 2019-12-01 15:55 * * /
@AllArgsConstructor
@Getter
public enum  PersonEnums {
    STUDENT(1."Students", Student.class),
    TEACHER(2."Teacher", Teacher.class),
    HEADMASTER(3."The principal", HeadMaster.class);

    Integer code;
    String msg;
    Class clazz;

    /** * Gets the name of the class, because Spring automatically injects the default class name (lowercase) *@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 ""; }}Copy the code

Usage:

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