The Adapter Pattern

    어댑터 패턴은 어떤 클래스의 인터페이스 프로그래밍을 다른 클래스로 변환할 때 사용되어진다. 우리는 어댑터를 하나의 프로그램에서 함께 작업하는 클래스간에 서로 관련성이 없게 하고자 할 때 사용한다. 어댑터의 개념은 그러므로 간단하다; 우리는 요구되는 인터페이스를 갖는 클래스를 작성하고 다른 인터페이스를 갖는 클래스와 통신할 수 있게 한다. 

    어댑터를 구현하는 방법에는 상속에 의한 방법과 객체 조합에 의한 방법이 있다. 첫번째 경우에서, 우리는  상속 받는 새로운 클래스를 만들고 요구되는 인터페이스에 매치되는 상속받은 새로운 클래스를 만들기 위해 필요로 하는 메소드를 추가한다. 다른 방법은 새로운 클래스 내부에 원본의 클래스를 포함하는 방법이고 새로운 클래스 내부에서 호출되는 변환하기 위한 메소드를 생성한다. 

Adapters in Java

        자바언어에서는 많은 어댑터들이 이미 구성되어 있다. 이 경우에, 자바 어댑터는 이벤트 인터페이스가 불필요하게 복잡하지 않도록 해준다. 이러한 자바 어댑터 들 중에서 빈번하게 사용되는 예는 WindowAdapter 클래스이다.      
	
public class MainFrame extends Frame implements WindowListener {

	public MainFrame() {
		addWindowListener(this);	//frame listens
						//for window events
	}
	
	public void windowClosing(WindowEvent e) {
		System.exit(0);	//exit on System exit box clicked
	}
	
	public void windowClosed(WindowEvent e){ }
	public void windowOpened(WindowEvent e){ }
	public void windowIconified(WindowEvent e){ }
	public void windowDeiconified(WindowEvent e){ }
	public void windowActivated(WindowEvent e){ }
	public void windowDeactivated(WindowEvent e){ }
}
위에서 있는 코드는 읽기도 힘들고 작성하기도 번거로운 코드이다. 이 문제를 WindowAdapter 클래스는 간단하게 해결해 준다. 
	

//illustrates using the WindowAdapter class

public class Closer extends Frame {
	
	public Closer() {
		addWindowListener(new WinAp());
		
		//----------------------------
	}
}

class WinAp extends WidowAdapter {
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
	);
}

'Development > 패턴자료' 카테고리의 다른 글

[펌] The Builder Pattern  (0) 2011.08.13
[펌] The Bridge Pattern  (0) 2011.08.13
[펌] The Abstract Factory Pattern  (0) 2011.08.13
[펌] Summary of Structural Patterns  (0) 2011.08.13
[펌] Structural Patterns  (0) 2011.08.13
안정적인 DNS서비스 DNSEver DNS server, DNS service
Posted by 키르히아이스
,