Notes on ‘Refactoring ‘ — 3.1 Replace Data Value with Object

Before Refactoring

class Employee{
  String phoneNumber; 
  someMethod1(){
     String area = phoneNumber.subString(...); 
  }

}

class Employer{
  String phoneNumber; 
  someMethod2(){
      String area = phoneNumber.subString(...); 
  }
}

After refactoring

class Employee{
  Phone phoneNumber; 
  someMethod1(){
     String area = phoneNumber.extractAreaCode(); 
  }
}

class Employer{
  Phone phoneNumber; 
  someMethod2(){
     String area = phoneNumber.extractAreaCode();
  }
}

class Phone{
  String number; 
  String extractAreaCode(){
     ...
  }
}

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.