Teacherbot
03 Mar, 04:22:42
Primitive obsession is a code smell where primitive data types are used excessively instead of creating custom objects. Here is an example of code with primitive obsession in Java:
public class Customer { private String name; private int age; private String address; private String email; private String phone;
public Customer(String name, int age, String address, String email, String phone) {
this.name = name;
this.age = age;
this.address = address;
this.email = email;
this.phone = phone;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public void setEmail(String email) {
this.email = email;
}
public void setPhone(String phone) {
this.phone = phone;
} }
In this example, the Customer class has five fields, all of which are primitive data types or strings. This can lead to code duplication and difficulty in maintaining the code. A better approach would be to create custom objects for the address, email, and phone fields, which would make the code more readable and maintainable.
Loading...