* 클래스 추출(Extract Class) 리팩토링
- 기존의 클래스에서 필드와 메서드를 추출해서 새로운 클래스로 옮기는 것.
* 클래스 추출 리팩토링 순서
1) 클래스의 책임을 어떻게 추출할지 결정
2) 추출한 책임을 담당할 새로운 클래스 작성
3) 새로운 클래스에서 새로운 클래스로 링크 작성
4) 기존 클래스에서 새로운 클래스로 필요한 필드 이동
5) 이동 할때마다 컴파일해서 테스트
6) 기존 클래스에서 새로운 클래스로 필요한 메스드 이동
* 리팩토링 전 코드
package ExtractClass;
public class Book {
private String _title;
private String _isbn;
private String _price;
private String _authorName;
private String _authorMail;
public Book(String title, String isbn, String price, String authorName, String authorMail) {
_title = title;
_isbn = isbn;
_price = price;
_authorName = authorName;
_authorMail = authorMail;
}
public String get_title() {
return _title;
}
public void set_title(String _title) {
this._title = _title;
}
public String get_isbn() {
return _isbn;
}
public void set_isbn(String _isbn) {
this._isbn = _isbn;
}
public String get_price() {
return _price;
}
public void set_price(String _price) {
this._price = _price;
}
public String get_authorName() {
return _authorName;
}
public void set_authorName(String _authorName) {
this._authorName = _authorName;
}
public String get_authorMail() {
return _authorMail;
}
public void set_authorMail(String _authorMail) {
this._authorMail = _authorMail;
}
public String toXml() {
String author = tag("author", tag("name",_authorName) + tag("mail", _authorMail));
String book = tag("book", tag("title", _title) + tag("isbn", _isbn) + tag("price", _price) + author);
return book;
}
private String tag(String element, String content) {
return "<" + element + ">" + content + "</" + element + ">";
}
}
package ExtractClass;
public class Main {
public static void main(String[] args) {
Book refactoring = new Book(
"Refactoring: improving the design of existing code",
"ISBN0201485672",
"44.95$",
"Martin Fowler",
"fowler@acm.org"
);
Book math = new Book("프로그래머 수학", "ISBN1234", "20000원", "유키 히로시", "hyuki@hyuki.com");
System.out.println("refactoring:");
System.out.println(refactoring.toXml());
System.out.println("math:");
System.out.println(math.toXml());
}
}
* 리팩토링 후 코드
package ExtractClass;
public class Book {
private String _title;
private String _isbn;
private String _price;
// private String _authorName;
// private String _authorMail;
private Author _author;
public Book(String title, String isbn, String price, String authorName, String authorMail) {
_title = title;
_isbn = isbn;
_price = price;
// _authorName = authorName;
// _authorMail = authorMail;
_author = new Author(authorName, authorMail);
}
public String get_title() {
return _title;
}
public void set_title(String _title) {
this._title = _title;
}
public String get_isbn() {
return _isbn;
}
public void set_isbn(String _isbn) {
this._isbn = _isbn;
}
public String get_price() {
return _price;
}
public void set_price(String _price) {
this._price = _price;
}
public String getAuthorName() {
return _author.getName();
}
public void setAuthorName(String name) {
_author.setName(name);
}
public String getAuthorMail() {
return _author.getMail();
}
public void setAuthorMail(String mail) {
_author.setMail(mail);
}
public String toXml() {
String author = tag("author", tag("name", _author.getName()) + tag("mail", _author.getMail()));
String book = tag("book", tag("title", _title) + tag("isbn", _isbn) + tag("price", _price) + author);
return book;
}
private String tag(String element, String content) {
return "<" + element + ">" + content + "</" + element + ">";
}
class Author {
private String _name;
private String _mail;
public Author(String name, String mail) {
_name = name;
_mail = mail;
}
public String getName() {
return _name;
}
public String getMail() {
return _mail;
}
public void setName(String name) {
_name = name;
}
public void setMail(String mail) {
_mail = mail;
}
}
}
package ExtractClass;
public class Main {
public static void main(String[] args) {
Book refactoring = new Book(
"Refactoring: improving the design of existing code",
"ISBN0201485672",
"44.95$",
"Martin Fowler",
"fowler@acm.org"
);
Book math = new Book("프로그래머 수학", "ISBN1234", "20000원", "유키 히로시", "hyuki@hyuki.com");
System.out.println("refactoring:");
System.out.println(refactoring.toXml());
System.out.println("math:");
System.out.println(math.toXml());
}
}
위의 코드에서 Book 클래스에서 Author 클래스로의 링크 이외에 역방향 링크를 추가하여 양방향 링크, 즉 Author클래스에서 Book클래스로의
링크를 생성하게 되면 초기화나 관리가 복잡하고 필드나 메서드를 정리하기 힘들기 때문에 양방향 링크를 될 수 있는한 생성하지 않는다.
( 양방향 링크를 피할 수 없는 경우도 있음 )
양방향 링크를 단방향으로 수정하는 '양방향 관련을 단방향으로 변경'이라는 리팩토링도 있음.
분류 코드를 하위 클래스로 치환 (Replace Type Code With Subclass) (0) | 2020.02.03 |
---|---|
분류 코드를 클래스로 치환( Replace Type Code with Class) 리팩토링 (0) | 2020.01.28 |
메서드 추출(Extract Method) 리팩토링 (0) | 2020.01.20 |
널 객체 도입 리팩토링 (0) | 2020.01.18 |
어서션(assert) 리팩토링 (0) | 2020.01.18 |
댓글 영역