RSS Feed for This PostCurrent Article

Writing Easily Maintainable Code ?

Download Source Code

This is the design pattern I came across while browsing through my RSS feed reader. Not very confirmed now from where I read about this. It is quite hard to apply this to the data access layer since I use ORM tools but I am not sure how this can be applied to the business logic layer.

The concept of this pattern is simple. In order to make your code easily maintainable, do not use primitive data types, e.g. String, Integer in your class properties since primitive data types carry no meaning at all.

E.g. for the following Student class

   1: import java.util.List;
   2:  
   3: public class Student {
   4:     private Name name;
   5:     private Email email;
   6:     private List<Course> courses;
   7:  
   8:     public Student() {
   9:     }
  10:  
  11:     public Student(Name name, Email email, List<Course> courses) {
  12:         this.name = name;
  13:         this.email = email;
  14:         this.courses = courses;
  15:     }
  16:  
  17:     public Name getName() {
  18:         return name;
  19:     }
  20:  
  21:     public Student setName(Name name) {
  22:         this.name = name;
  23:         return this;
  24:     }
  25:  
  26:     public Email getEmail() {
  27:         return email;
  28:     }
  29:  
  30:     public Student setEmail(Email emal) {
  31:         this.email = emal;
  32:         return this;
  33:     }
  34:  
  35:     public List<Course> getCourses() {
  36:         return courses;
  37:     }
  38:  
  39:     public Student setCourses(List<Course> courses) {
  40:         this.courses = courses;
  41:         return this;
  42:     }
  43: }
  44:  

As you can see, all the properties are classes, as shown below.

   1: public class Name {
   2:     private String name;
   3:  
   4:     public Name() {
   5:     }
   6:  
   7:     public Name(String name) {
   8:         this.name = name;
   9:     }
  10:  
  11:     public static Name name(String value) {
  12:         return new Name(value);    
  13:     }
  14:  
  15:     public String getName() {
  16:         return name;
  17:     }
  18:  
  19:     public void setName(String name) {
  20:         this.name = name;
  21:     }
  22: }
   1: public class Email {
   2:     private String email;
   3:  
   4:     public Email() {
   5:     }
   6:  
   7:     public Email(String email) {
   8:         this.email = email;
   9:     }
  10:  
  11:     public static Email email(String value){
  12:         return new Email(value);
  13:     }
  14:  
  15:     public String getEmail() {
  16:         return email;
  17:     }
  18:  
  19:     public void setEmail(String email) {
  20:         this.email = email;
  21:     }
  22: }
   1: import java.util.Arrays;
   2: import java.util.List;
   3:  
   4: public class Course {
   5:     private String name;
   6:  
   7:     public Course() {
   8:     }
   9:  
  10:     public Course(String name) {
  11:         this.name = name;
  12:     }
  13:  
  14:  
  15:     public static Course course(String name) {
  16:         return new Course(name);
  17:     }
  18:  
  19:     public static List<Course> courses(Course... values) {
  20:         return Arrays.asList(values);
  21:     }
  22:  
  23:     public String getName() {
  24:         return name;
  25:     }
  26:  
  27:     public void setName(String name) {
  28:         this.name = name;
  29:     }
  30: }

Each of each class, Name, Email and Course define static methods which are used to instantiate object of its own.

Since student can have many courses, the method courses in Course accept Varargs as parameter and return a List.

To use it, see below

   1: import static Name.name;
   2: import static Email.email;
   3: import static Course.*;
   4:  
   5: public class TestClass {
   6:  
   7:     public static void main(String[] args) {
   8:         Student student = new Student(name("jason"), email("[email protected]"),
   9:                 courses(course("Programming in Java"), course("Computer Algorithms"), course("Computer Concepts")));
  10:  
  11:         // To change any information
  12:         student.setName(name("alice"));
  13:         student.setEmail(email("[email protected]"));
  14:  
  15:     }
  16: }

Is this code more maintainable ? I am not sure…


Trackback URL


RSS Feed for This Post1 Comment(s)

  1. zersromadeefe | Nov 24, 2009 | Reply

    Kick-ass post, good looking weblog, added it to my favs!

Sorry, comments for this entry are closed at this time.