What is the difference between congestion model and anemia model?

Concept:

The anemia model was first widely used from EJB2, and its strongest period was created by Spring, which separated "behavior" (also known as logic and process) and "state" (which can be understood as data, and corresponding to language is object member variable) into different objects. The object with only state is the so-called "anemia object" (often referred to as VO Value Object), The object with only behavior is the Logic/Service/Manager layer in our common N-tier structure (corresponding to the Stateless Session Bean in EJB2). (Rod Johnson, the author of Spring, once admitted that Spring is just following the "transaction script" in the era of EJB2, that is, process oriented programming)

The congestion model is actually very simple, which is the essence of object-oriented design: "an object has state and behavior". For example, a person's eyes and nose are state. People can play games or write programs, which is behavior. Why should a "human Manager" exist to help people "play games"?

Take a simple J2EE example to design a User related function. The traditional design is generally:

Class: User+UserManager

Save user call: usermanager save(User user);

The design of congestion may be:

Class: User

Save user call: user save();

——User has one behavior: save itself

In fact, they have no particular applicable direction. Individuals prefer to always use congestion model, because object-oriented programming always has richer semantics, more reasonable organization and stronger maintainability than process-oriented programming - of course, it is more difficult to master. Therefore, in the actual engineering scenario, whether to use it and how to use it also depend on the understanding and grasp of the designer and team model design, because now most J2EE developers are deeply affected by the anemia model.

In addition, the use of congestion model in actual engineering scenarios will encounter many detailed problems, among which the biggest difficulty is "how to design congestion model" or "how to separate appropriate and semantic logic from complex business into VO behavior", because in my experience, it does not specifically refer to who, Most J2EE developers haven't started object-oriented yet, but this is beyond the current problem:)

example:

Illustration:

code:

// Anemia model
// State only, not behavior
public class PeopleAnemia {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}
public class PeopleManager {
    private static Logger logger = LoggerFactory.getLogger("PeopleManager");

    // Play games
    public static void playGame(PeopleAnemia peopleAnemia) {
        String name = peopleAnemia.getName();
        Integer age = peopleAnemia.getAge();
        logger.info(name + "Apply to play games");
        if(age < 18){
            logger.info(name + "Minors are not allowed to play games");
            return;
        }
        logger.info(name + "Start playing games");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info(name + "End the game");
    }
}
// Hyperemia model
// It involves behavior
public class PeopleCongestion {
    private Logger LOGGER = LoggerFactory.getLogger("Hyperemia model");
    private String name;
    private Integer age;

    public PeopleCongestion(){

    }
    public PeopleCongestion(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    // Play games
    public void playGame() {
        LOGGER.info(name + "Apply to play games");
        if(age < 18){
            LOGGER.info(name + "Minors are not allowed to play games");
            return;
        }
        LOGGER.info(name + "Start playing games");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LOGGER.info(name + "End the game");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}
@Test
public void test03(){
    // Anemia model
    PeopleAnemia peopleAnemia = new PeopleAnemia();
    peopleAnemia.setAge(14);
    peopleAnemia.setName("bob");
    PeopleManager.playGame(peopleAnemia);
    // Hyperemia model
    PeopleCongestion peopleCongestion = new PeopleCongestion();
    peopleCongestion.setAge(19);
    peopleCongestion.setName("john");
    peopleCongestion.playGame();
}

Conclusion:

Congestion model is more convenient

Keywords: Java Spring Back-end

Added by imranlink on Thu, 03 Feb 2022 07:20:13 +0200