본문 바로가기

Software Architect

[Design Pattern] Adapter Pattern

Adapter Pattern

개요

  • 기존 사용하던 Library 교체가 필요한 경우 기존 코드 수정을 최소화 하기 위해 사용
    > 새로운 Library API에 맞도록 Wrapper 생성
    > 변환기로 사용
  • Facade와 비교 (용도가 다름)
Facade Adapter
둘다 Wrapper
쉬운 Class 사용을 위한 단순 Wrapper 모듈 교체용 Wrapper

 

코드 구현 - 1

(변경 전)

public class main {
	// [1] Test가 완료된 모듈 -> 크게 안 건드리고 싶음
	public static void run(Azure az) {  
		az.ms_connect();
		az.ms_login("ID", "1221");
		az.ms_sendData();
		az.ms_receiveData();
		az.ms_disconnect();		
	}
	
	public static void main(String[] args) {
		run(new Azure());		
	}
}

// Library AZURE 
class Azure {
	public void ms_connect    () { System.out.println("Azure connect");	};
	public void ms_login(String ID, String PASS) { System.out.println("Azure Login "+ ID +"/"+ PASS);	};
	public void ms_sendData   () { System.out.println("Azure send data");	};
	public void ms_receiveData() { System.out.println("Azure receive data");	};
	public void ms_disconnect () { System.out.println("Azure disconnect");	};
		
}

class AWS {
	public void aws_connect(String ID, String PASS) { System.out.println("AWS Conn "+ ID +"/"+ PASS);	};
	public void aws_setData() { System.out.println("AWS setData");	};
	public void aws_getData() { System.out.println("AWS getData");	};
	public void aws_bye    () { System.out.println("AWS bye");	};
	
}
  • 현재는 Azure Library를 사용하는 상황
  • [1] Test가 완료되어 크게 건드리고 싶지 않은 부분

 

(변경 후)

public class main {
	// Test가 완료된 모듈 -> 안 건드리고 싶음
	public static void run(Adapter az) {  
		az.ms_connect();
		az.ms_login("ID", "1221");
		az.ms_sendData();
		az.ms_receiveData();
		az.ms_disconnect();		
	}
	
	public static void main(String[] args) {
		run(Adapter.getInst());		
	}
}

class Adapter extends Azure {
	private static AWS aws;

	public static Adapter getInst() {
		aws = new AWS();
		return new Adapter();
	}
	
	public void ms_connect    () {  }
	public void ms_login(String ID, String PASS) { aws.aws_connect(ID,  PASS);} 
	public void ms_sendData   () { aws.aws_setData(); }
	public void ms_receiveData() { aws.aws_getData(); }
	public void ms_disconnect () { aws.aws_bye();     }
}

// Library AZURE 
class Azure {
}

class AWS {
	public void aws_connect(String ID, String PASS) { System.out.println("AWS Conn "+ ID +"/"+ PASS);	};
	public void aws_setData() { System.out.println("AWS setData");	};
	public void aws_getData() { System.out.println("AWS getData");	};
	public void aws_bye    () { System.out.println("AWS bye");	};
}
  • [1] 기존 모듈에서 Adapter를 가져오는 것으로만 변경
  • [2] Adapter 적용 = Azure를 받아서 AWS로 변환

 

 

코드구현 - 2 (interface 활용)

(변경전)

public class main {

	public static void main(String[] args) {
		CassettePlayer cp = new MYMY();
		cp.insertCassette();
		cp.pushPlay();
		cp.pushStop();
	}
}

interface CassettePlayer {
	void insertCassette();
	void pushPlay();
	void pushStop();
}

class MYMY implements CassettePlayer {

	@Override
	public void insertCassette() { System.out.println("Insert Cassette"); }

	@Override
	public void pushPlay() { System.out.println("Push Play"); }

	@Override
	public void pushStop() { System.out.println("Push Stop"); }
}

interface MP3Player {
	void downloadMusic();
	void mp3_play();
	void mp3_stop();
}
class EYE_REAVER implements MP3Player {
	@Override
	public void downloadMusic() { System.out.println("Download Music");	}
	@Override
	public void mp3_play() { System.out.println("MP3 Play"); }
	@Override
	public void mp3_stop() { System.out.println("MP3 Stop"); }
	
}

 

(변경후)

public class main {
	static void run(CassettePlayer cp) {
		cp.insertCassette();
		cp.pushPlay();
		cp.pushStop();
	}
	
	public static void main(String[] args) {
		// run(new MYMY());
		run(new Adapter(new EYE_REAVER()));
	}
}
class Adapter implements CassettePlayer {
	EYE_REAVER er;
	
	public Adapter(EYE_REAVER er) {
		this.er = er;
	}
	
	@Override
	public void insertCassette() { er.downloadMusic(); }

	@Override
	public void pushPlay() { er.mp3_play(); }

	@Override
	public void pushStop() { er.mp3_stop(); }
}

interface CassettePlayer {
	void insertCassette();
	void pushPlay();
	void pushStop();
}
//class MYMY implements CassettePlayer {
//	@Override
//	public void insertCassette() { System.out.println("Insert Cassette"); }
//
//	@Override
//	public void pushPlay() { System.out.println("Push Play"); }
//
//	@Override
//	public void pushStop() { System.out.println("Push Stop"); }
//}

interface MP3Player {
	void downloadMusic();
	void mp3_play();
	void mp3_stop();
}
class EYE_REAVER implements MP3Player {
	@Override
	public void downloadMusic() { System.out.println("Download Music");	}
	@Override
	public void mp3_play() { System.out.println("MP3 Play"); }
	@Override
	public void mp3_stop() { System.out.println("MP3 Stop"); }
	
}

'Software Architect' 카테고리의 다른 글

[Design Pattern] Observer Pattern  (0) 2022.03.25
[Design Pattern] Decorator Pattern  (0) 2022.03.24
[Design Pattern] Facade Pattern  (0) 2022.03.24
[Design Pattern] Singleton Pattern  (0) 2022.03.24
[Design Pattern] Builder Pattern  (0) 2022.03.24