Extract Interface Refactoring  

 

�� ������ Extract Interface ï¿½ï¿½ï¿½ï¿½ï¿½ä¸µ ������ ��ſ��� �������� overview�� how ï¿½ï¿½ �����丵�� IntelliJ IDEA���� ���� �� �ִ°��� �����մϴ�.

����


Extract Interface ï¿½ï¿½ï¿½ï¿½ï¿½ä¸µï¿½ï¿½ ���Ҿ� ����� �� �ɼ��� �����ϴ�.  Ã¹ ��°(Extract Interface) ï¿½ï¿½ Ŭ������ �޼ҵ带 ���ʷ� �ϴ� �������̽��� �����ϵ��� ����մϴ�.

 

�� ��° (Rename original class and use interface where possible) ï¿½ï¿½ ������ Ŭ������ �̸��� �����ϰ� ���Ӱ� ������ �������̽��� �����մϴ�. ï¿½×·ï¿½ ���, �����ϸ� IDEA�� �׶� �������̽��� ����ϱ� ���� ��� ������ Ŭ���� ����� �ٲߴϴ� (�׸��� �װ��� ���� ���� ������ �װ͵��� �������� ���� ���Դϴ�).

 

����, ������ Ŭ�������� ����� ������ �������� �ʵ�� �������̽��� �̵��� �� �ֽ��ϴ�.  ï¿½ï¿½ ���, �������̽��� Ư���� �޼ҵ�� �ʵ带 �����ϸ鼭 ������ ���Դϴ�.  ï¿½ï¿½ ������, ���ǵ� Ŭ���� �޼ҵ�� �����ϴ� �������̽� �޼ҵ��� ������ �˴ϴ�.

 

Extract Interface ï¿½É¼ï¿½ï¿½ï¿½ ���� ���� �ڵ�

�츮�� ������ Ŭ������ �����ϴ�:

 
class AClass {
    public static final double CONSTANT=3.14;
    public void publicMethod() {//some code here}
    public void secretMethod() {//some code here}
}

�����丵 ����, ���ο� �������̽��� ������ ���Դϴ�:

public interface AnInterface {
    double CONSTANT=3.14;
    void publicMethod();
}


�ҽ� Ŭ������ �������̽��� ������ ���Դϴ�:

 
class AClass implements AnInterface {
    public void publicMethod() {//some code here}
    public void secretMethod() {//some code here}
}
     

���� Rename original class and use interface where possible  �ɼ��� ���õǸ� �ڵ带 �����Ͻʽÿ�:

public class FormerAClass implements AClass {
    public void publicMethod() {//some code here }
    public void secretMethod() {//some code here}
}
 
�׸��� ������ Ŭ������ �̸��� ������ ���Ӱ� ������ �������̽�:  
 
public interface AClass {
    double CONSTANT=3.14;
 
    void publicMethod();
}
 

����� �� �ٸ� �������̽��� �̹� �����ϴ� Ŭ�����κ��� �������̽��� ������ �� �ֽ��ϴ�. AnInterface�� �����ϴ� Ŭ�����κ��� extract interface�� �մϴ�. �츮�� AnotherInterface(����� �޼ҵ�)�� AnInterface (������ ��)�� Ȯ��DZ⸦ ���ϴ��� �Ǵ� �׵��� ������ �ҽ� AClass�� �츮�� ���ϴ����� ���� �츮�� ������ �ڵ带 ��� �� ���Դϴ�.


Extracted Interface extends the existing one Source class:

 
class AClass implements AnotherInterface {
    public void publicMethod() { //some code here }
    public void secretMethod() { //some code here }
}
 

Extracted Interface: 

 
public interface AnotherInterface extends AnInterface {
}
 

Source class implements both interfaces  

Source class: 

 
 class AClass implements AnInterface, AnotherInterface {
    public void publicMethod() { //some code here }
    public void secretMethod() { //some code here }
}

Extracted Interface: 

public interface AnotherInterface {
}

�������̽��� ����Ǿ������, IDEA�� Ŭ������ ����� �˻��ϰ�, �������̽��� ������� �׵��� �����ϸ� ��ü�� ���� ������ ���Դϴ�.  ï¿½ï¿½ï¿½ï¿½ï¿½ IDEA�� �� ������ �ڵ������� �����ϰ� �� �� �ְų�, ����� �߰ߵǰ�, �׵��� ��ü�� �����ϴ� ���� ������ ��� ���� �� �ֽ��ϴ�.

���� �ڵ�:

class AClass implements AnInterface {
    public void publicMethod() {//some code here}
    public void secretMethod() {//some code here}
 
}
    ...
    ...
 
//This usage can be modified by changing the
//parameter type from 'AClass' to 'AnInterface'
 
 void foo (AClass aClass){
    aClass.publicMethod();
}
 
...

  
...

  
 
//This usage will not be suggested for 
//refactoring because it is not specified in the interface
 
void bar (AClass aClass){
    aClass.secretMethod();
}
 

�������̽� ����


Ŭ�����κ��� �������̽��� �����ϱ� ���� ������ �մϴ�:

 1.   Project ���⿡�� Ŭ������ �����ϰų� ���� �����⿡�� Ŭ���� ������ ij���� ��� ���� ��ġ�Ͻʽÿ�, �׸��� Extract Interface ï¿½ï¿½ �˾� �޴��κ��� �Ǵ� Refactor �޴��κ��� �����ʽÿ�.


����� ���� Ŭ������ Structure, Commander, Source ��� ���� �ٸ� ���⿡�� ���� �� �ֽ��ϴ�.



 2.   ï¿½ï¿½ �Ŀ�, ���� ������� �����丵 �� ������ �����ϱ� ���� ��Ÿ�� ���Դϴ�:



ù ��°��, �� �ɼ�(Extract Interface  ï¿½Ç´ï¿½ Rename original class and use interface where possible ) ��ư ���� �� ���� �����Ͻʽÿ�. �׸��� ���� Interface name  ï¿½Ç´ï¿½ Rename original class to ï¿½Ø½ï¿½Æ® �ʵ忡�� �������̽��� ���� �̸� �Ǵ� ���� Ŭ������ ���� ���ο� �̸��� �����Ͻʽÿ�

Members to Form Interface ï¿½Ð³Î¿ï¿½ï¿½ï¿½, ����� �������̽��� ���ԵDZ⸦ ���ϴ� �޼ҵ�� �ʵ带 �����Ͻʽÿ�.


����� Ŭ������ �� �ٸ� �������̽��� �̹� �����߽��ϴ�.  ï¿½ï¿½ ���, �� �������̽��� Members to Form Interface ��Ͽ��� ��Ÿ�� ���Դϴ�.  ï¿½ï¿½ï¿½ï¿½ ����� ������ �������̽��� ���� �ϳ��� Ȯ���ϱ⸦ ���ϸ�, ����� ��Ͽ��� �װ��� �����ؾ߸� �մϴ�.  ï¿½ï¿½ï¿½ï¿½ �׷��� �ʴٸ�, �� �������̽��� ���õ��� �ʰ� �Ͻʽÿ�. �׷��� Ŭ������ �����ϰ� ���� �������̽��� ������ ���Դϴ�.



JavaDoc �׷쿡��, ���ο� �������̽��� ������ �� Javadoc�� �ٷ�����߸� �ϴ� �ɼ� ��ư�� �����Ͻʽÿ�.

As is  

??? 

Copy  

??? 

Move  

??? 



 3.   OK ï¿½ï¿½ Ŭ���Ͻʽÿ�, �׸��� ���ο� �������̽��� ������ ���Դϴ�.

 4.   ï¿½ï¿½ ��, ���� ����� Extract Interface  �ɼ� ��ư�� �����ϸ�, ���� �����찡 ��Ÿ�� ���Դϴ�:



���� ����� IDEA�� Ŭ������ ����� �м��ϰ�, �����ϸ� �������̽��� ������� �׵��� ��ü�ϱ� �ٶ��, Yes ï¿½ï¿½ Ŭ���Ͻʽÿ�, ï¿½×·ï¿½ï¿½ï¿½ �ʴٸ� No�� Ŭ���Ͻʽÿ�.  ï¿½ï¿½ï¿½ï¿½ ���� �̸� ���⸦ ���ʽÿ�.

Preview usages to be changed üũ �ڽ��� üũ�ϸ� ��ſ��� ����� �߰ߵǰ�, �׵��� ��ü�� �����ϴ� ������ ��� ��� ���� ����մϴ�.  Ã¼Å©ï¿½ï¿½ï¿½ï¿½ ���� ��, IDEA�� ��ü�� �ڵ������� �����մϴ�.


������ üũ �ڽ��� Ȯ�ε��� �ʾ��� ����, ���� �߰ߵ� ����� � �б� ���� ���Ͽ��� �����ϸ� ��ſ��� ��� ��ü�� Ȯ���ϱ� ���� ������Ʈ �� �� �Դϴ�.