Data clump là gì?

Noun Programming

Đôi khi các phần khác nhau của code chứa các nhóm biến giống hệt nhau (chẳng hạn như các tham số để kết nối với cơ sở dữ liệu). Nhóm các biến này được gọi là data clump. Nhóm các biến này nên được chuyển thành các lớp (class) riêng của chúng. Ví dụ trong đoạn code Java bên dưới:


public static void main(String args[]) {
    String firstName = args[0];
    String lastName = args[1];
    Integer age = new Integer(args[2]);
    String gender = args[3];
    String occupation = args[4];
    String city = args[5];
    welcomeNew(firstName, lastName, age, gender, occupation, city);
}

public static void welcomeNew(String firstName, String lastName, Integer age, String gender, String occupation, String city) {
    System.out.printf("Welcome %s %s, a %d-year-old %s from %s who works as a%s\n",firstName, lastName, age, gender, city, occupation);
}

Ta thấy data clump là nhóm các biến firstName, lastName, age, gender, occupation và city. Tất cả các biến có thể được đóng gói thành một đối tượng "Person" duy nhất do đó ta sẽ tạo ra lớp riêng cho chúng:


public static void main(String args[]) {
    String firstName = args[0];
    String lastName = args[1];
    Integer age = new Integer(args[2]);
    String gender = args[3];
    String occupation = args[4];
    String city = args[5];

    Person joe = new Person(firstName, lastName, age, gender, occupation, city);
    joe.welcomeNew();
    joe.work();
}

private static class Person {
    /* All parameters have been moved to the new Person class where they are properly grouped and encapsulated */
    String firstName;
    String lastName;
    Integer age;
    String gender;
    String occupation;
    String city;
    
    public Person(String firstName, String lastName, Integer age, String gender, String occupation, String city) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.gender = gender;
        this.occupation = occupation;
        this.city = city;
    }
    
    /* Existing functionality relating to the data can also be incorporated into the new class, reducing the risk of scope collision */
    public void welcomeNew() {
        System.out.printf("Welcome %s %s, a %d-year-old %s from %s who works as a%s\n", firstName, lastName, age, gender, city, occupation);
    }
    /* Additionally, the new class may be an opportunity for new functionality to be added */
    public void work() {
        System.out.printf("This is %s working hard on %s in %s", firstName, occupation, city);
    }
}

Learning English Everyday