SlideShare una empresa de Scribd logo
1 de 17
Adapter 패턴
D-2
Adapter 패턴의 의도
• 한 클래스의 인터페이스를 클라이언트에서 사용하고자
하는 다른 인터페이스로 변환한다.
• 어댑터를 이용하면 인터페이스의 호환성 문제 때문에 같
이 쓸 수 없는 클래스를 연결 해 쓸 수 있다.
D-3
실생활에서의 예제
여행용 멀티 어댑터
D-4
Adapter 패턴의 해결
• 전제 조건
– 이미 제공되고 있는 인터페이스 혹은 클래스가 있다.
– 그것을 현재 그대로 사용할 수 없다.
• 해결법
– 그 인터페이스를 자신이 사용할 수 있도록 기존의 인터페이스를
상속받아 알맞게 변환해주는 클래스를 만든다. (상속)
– 그 클래스를 위임하는 Adapter 클래스를 만든다. (위임)
Adpater 패턴 예제 - 상속
• 구성
– 이미 제공되고 있는 Banner 클래스
– 내가 사용하려는 print 인터페이스
– 글자를 표시하고 출력할 Main 클래스
• 해결방안
– Banner 클래스와 print 인터페이스를 상속한다.
– 그리고 print 인터페이스를 Banner의 메소드를 이용해 구현한다.
– 이 파일을 PrintBanner 클래스라고 한다.
B-5
예제 소스 분석
기존에 제공하는 Banner 클래스.
public class Banner {
private String string;
//글자를 초기화
public Banner(String string) {
this.string = string;
}
//글자를 출력하는 메소드들
public void showWithParen() {
System.out.println("(" + string + ")");
}
public void showWithAster() {
System.out.println("*" + string + "*");
}
}
B-6
예제 소스 분석
사용하려는 Print 인터페이스
public interface Print {
//약하게 출력하는 메소드
public abstract void printWeak();
//강하게 출력하는 메소드
public abstract void printStrong();
}
B-7
예제 소스 분석
변환을 위해 만든 어댑터 클래스
// 변환을 위해 Banner와 Print를 상속받는다.
public class PrintBanner extends Banner implements Print {
//출력을 원하는 문자열을 초기화.
public PrintBanner(String string) {
super(string);
//super 메소드를 통해 Banner가 초기화됨.
}
//문자를 약하게 출력
public void printWeak() {
showWithParen();
//Banner 클래스의 메소드를 통해 출력
}
//문자를 강하게 출력
public void printStrong() {
showWithAster();
//Banner 클래스의 메소드를 통해 출력
}
}
B-8
예제 소스 분석
Main 클래스
public class Main {
public static void main(String[] args) {
//내가 사용하길 원하는 Print 객체 선언 후 어댑터를 통해 초기화
Print p = new PrintBanner("Hello");
//어댑터의 객체를 통해 출력
p.printWeak();
p.printStrong();
}
}
B-9
예제 소스 분석
Main 클래스
public class Main {
public static void main(String[] args) {
//내가 사용하길 원하는 Print 객체 선언 후 어댑터를 통해 초기화
Print p = new PrintBanner("Hello");
//어댑터의 객체를 통해 출력
p.printWeak();
p.printStrong();
}
}
B-10
상속 패턴 클래스 다이어그램
B-11
Adpater 패턴 예제 - 위임
• 구성
– 이미 제공되고 있는 Banner 클래스
– 내가 사용하려는 print 클래스
– 글자를 표시하고 출력할 Main 클래스
• 해결방안
– print 클래스의 기능을 위임하는 클래스를 생성한다.
– Banner 인스턴스를 선언하고 print의 메소드를 구현한다.
– 이 파일을 PrintBanner 클래스라고 한다.
B-12
예제 소스 분석
기존에 제공하는 Banner 클래스.
public class Banner {
private String string;
//글자를 초기화
public Banner(String string) {
this.string = string;
}
//글자를 출력하는 메소드들
public void showWithParen() {
System.out.println("(" + string + ")");
}
public void showWithAster() {
System.out.println("*" + string + "*");
}
}
B-13
예제 소스 분석
사용하려는 Print 인터페이스
public abstract Print {
//약하게 출력하는 메소드
public abstract void printWeak();
//강하게 출력하는 메소드
public abstract void printStrong();
}
B-14
예제 소스 분석
변환을 위해 만든 어댑터 클래스
// 변환을 위해 Banner와 Print를 상속받는다.
public class PrintBanner extends Print {
//Banner 인스턴스를 생성
private Banner banner;
//banner 인스턴스를 통해 Banner가 초기화됨.
public PrintBanner(String string) {
this.banner = new Banner(string);
}
//Banner 인스턴스의 메소드를 통해 문자를 약하게 출력
public void printWeak() {
banner.showWithParen();
}
//Banner 인스턴스의 메소드를 통해 문자를 강하게 출력
public void printStrong() {
banner.showWithAster();
}
}
B-15
예제 소스 분석
Main 클래스
public class Main {
public static void main(String[] args) {
//내가 사용하길 원하는 Print 객체 선언 후 어댑터를 통해 초기화
Print p = new PrintBanner("Hello");
//어댑터의 객체를 통해 출력
p.printWeak();
p.printStrong();
}
}
B-16
위임 패턴 클래스 다이어그램
B-17

Más contenido relacionado

Destacado

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destacado (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Adapter Pattern

  • 2. D-2 Adapter 패턴의 의도 • 한 클래스의 인터페이스를 클라이언트에서 사용하고자 하는 다른 인터페이스로 변환한다. • 어댑터를 이용하면 인터페이스의 호환성 문제 때문에 같 이 쓸 수 없는 클래스를 연결 해 쓸 수 있다.
  • 4. D-4 Adapter 패턴의 해결 • 전제 조건 – 이미 제공되고 있는 인터페이스 혹은 클래스가 있다. – 그것을 현재 그대로 사용할 수 없다. • 해결법 – 그 인터페이스를 자신이 사용할 수 있도록 기존의 인터페이스를 상속받아 알맞게 변환해주는 클래스를 만든다. (상속) – 그 클래스를 위임하는 Adapter 클래스를 만든다. (위임)
  • 5. Adpater 패턴 예제 - 상속 • 구성 – 이미 제공되고 있는 Banner 클래스 – 내가 사용하려는 print 인터페이스 – 글자를 표시하고 출력할 Main 클래스 • 해결방안 – Banner 클래스와 print 인터페이스를 상속한다. – 그리고 print 인터페이스를 Banner의 메소드를 이용해 구현한다. – 이 파일을 PrintBanner 클래스라고 한다. B-5
  • 6. 예제 소스 분석 기존에 제공하는 Banner 클래스. public class Banner { private String string; //글자를 초기화 public Banner(String string) { this.string = string; } //글자를 출력하는 메소드들 public void showWithParen() { System.out.println("(" + string + ")"); } public void showWithAster() { System.out.println("*" + string + "*"); } } B-6
  • 7. 예제 소스 분석 사용하려는 Print 인터페이스 public interface Print { //약하게 출력하는 메소드 public abstract void printWeak(); //강하게 출력하는 메소드 public abstract void printStrong(); } B-7
  • 8. 예제 소스 분석 변환을 위해 만든 어댑터 클래스 // 변환을 위해 Banner와 Print를 상속받는다. public class PrintBanner extends Banner implements Print { //출력을 원하는 문자열을 초기화. public PrintBanner(String string) { super(string); //super 메소드를 통해 Banner가 초기화됨. } //문자를 약하게 출력 public void printWeak() { showWithParen(); //Banner 클래스의 메소드를 통해 출력 } //문자를 강하게 출력 public void printStrong() { showWithAster(); //Banner 클래스의 메소드를 통해 출력 } } B-8
  • 9. 예제 소스 분석 Main 클래스 public class Main { public static void main(String[] args) { //내가 사용하길 원하는 Print 객체 선언 후 어댑터를 통해 초기화 Print p = new PrintBanner("Hello"); //어댑터의 객체를 통해 출력 p.printWeak(); p.printStrong(); } } B-9
  • 10. 예제 소스 분석 Main 클래스 public class Main { public static void main(String[] args) { //내가 사용하길 원하는 Print 객체 선언 후 어댑터를 통해 초기화 Print p = new PrintBanner("Hello"); //어댑터의 객체를 통해 출력 p.printWeak(); p.printStrong(); } } B-10
  • 11. 상속 패턴 클래스 다이어그램 B-11
  • 12. Adpater 패턴 예제 - 위임 • 구성 – 이미 제공되고 있는 Banner 클래스 – 내가 사용하려는 print 클래스 – 글자를 표시하고 출력할 Main 클래스 • 해결방안 – print 클래스의 기능을 위임하는 클래스를 생성한다. – Banner 인스턴스를 선언하고 print의 메소드를 구현한다. – 이 파일을 PrintBanner 클래스라고 한다. B-12
  • 13. 예제 소스 분석 기존에 제공하는 Banner 클래스. public class Banner { private String string; //글자를 초기화 public Banner(String string) { this.string = string; } //글자를 출력하는 메소드들 public void showWithParen() { System.out.println("(" + string + ")"); } public void showWithAster() { System.out.println("*" + string + "*"); } } B-13
  • 14. 예제 소스 분석 사용하려는 Print 인터페이스 public abstract Print { //약하게 출력하는 메소드 public abstract void printWeak(); //강하게 출력하는 메소드 public abstract void printStrong(); } B-14
  • 15. 예제 소스 분석 변환을 위해 만든 어댑터 클래스 // 변환을 위해 Banner와 Print를 상속받는다. public class PrintBanner extends Print { //Banner 인스턴스를 생성 private Banner banner; //banner 인스턴스를 통해 Banner가 초기화됨. public PrintBanner(String string) { this.banner = new Banner(string); } //Banner 인스턴스의 메소드를 통해 문자를 약하게 출력 public void printWeak() { banner.showWithParen(); } //Banner 인스턴스의 메소드를 통해 문자를 강하게 출력 public void printStrong() { banner.showWithAster(); } } B-15
  • 16. 예제 소스 분석 Main 클래스 public class Main { public static void main(String[] args) { //내가 사용하길 원하는 Print 객체 선언 후 어댑터를 통해 초기화 Print p = new PrintBanner("Hello"); //어댑터의 객체를 통해 출력 p.printWeak(); p.printStrong(); } } B-16
  • 17. 위임 패턴 클래스 다이어그램 B-17