Skip to content

Latest commit

Β 

History

History
202 lines (138 loc) Β· 4.67 KB

adapter.md

File metadata and controls

202 lines (138 loc) Β· 4.67 KB

Adapter(μ–΄λŒ‘ν„°) λ””μžμΈ νŒ¨ν„΄

μ–΄λŒ‘ν„° νŒ¨ν„΄μ΄λž€ ν˜Έν™˜μ„±μ΄ μ—†λŠ” μΈν„°νŽ˜μ΄μŠ€ λ•Œλ¬Έμ— ν•¨κ»˜ λ™μž‘ν•  수 μ—†λŠ” ν΄λž˜μŠ€λ“€μ΄ ν•¨κ»˜ μž‘λ™ν•  수 μžˆλ„λ‘ ν•΄μ£ΌλŠ” νŒ¨ν„΄μ΄λ‹€.

μ–΄λŒ‘ν„°νŒ¨ν„΄

Adapter λ””μžμΈ νŒ¨ν„΄μ˜ μž₯점

  • 관계가 μ—†λŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό 같이 μ‚¬μš© κ°€λŠ₯
  • ν”„λ‘œκ·Έλž¨μ„ ν…ŒμŠ€νŠΈ ν•˜λŠ”λ° 용이
  • 클래슀 μž¬ν™œμš©μ„±μ΄ 높아짐

Adapter λ””μžμΈ νŒ¨ν„΄μ˜ μ’…λ₯˜

  • 객체 μ–΄λŒ‘ν„° νŒ¨ν„΄ - μœ„μž„μ„ μ΄μš©ν•œ μ–΄λŒ‘ν„° νŒ¨ν„΄

  • 클래슀 μ–΄λŒ‘ν„° νŒ¨ν„΄ - 상속을 μ΄μš©ν•œ μ–΄λŒ‘ν„° νŒ¨ν„΄

상속(Inheritance)κ³Ό μœ„μž„(Delegation)μ—μ„œ μœ„μž„μ— λŒ€ν•΄ μ΅μˆ™ν•˜μ§€ μ•Šμ„ 수 μžˆλŠ”λ° μœ„μž„μ΄λž€ λ‹€λ₯Έ 클래슀의 객체λ₯Ό 멀버 λ³€μˆ˜λ‘œ κ°–λŠ” ν˜•νƒœμ˜ ν΄λž˜μŠ€μ΄λ‹€.

Adapter λ””μžμΈ νŒ¨ν„΄ 예제

μ‹œλ‚˜λ¦¬μ˜€

μ›λž˜λŠ” μ΄λ―Έμ§€λ§Œ 보여쀄 수 μžˆλŠ” λ·°μ–΄κ°€ μžˆλ‹€. μ—¬κΈ°μ„œ μš”κ΅¬μ‚¬ν•­μ— μ˜ν•΄ 이미지가 μ•„λ‹Œ λ™μ˜μƒλ„ 보여 μ£Όκ³  μ‹Άλ‹€.

  1. Image ν΄λž˜μŠ€λŠ” Media μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ†ν•œλ‹€.
  2. Video ν΄λž˜μŠ€λŠ” AdvancedMedia μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ†ν•œλ‹€.
  3. ν˜„μž¬ 개발된 μ½”λ“œλ₯Ό μˆ˜μ • ν•  수 μ—†λŠ” 상황이닀.
  4. μƒˆλ‘œμš΄ Viewerλ₯Ό λ§Œλ“€ 수 μ—†λŠ” 상황이닀.

μ–΄λ–€ νŒŒμΌλ“€μ„ μΆ”κ°€ν•˜κ³  main()에 μ–΄λ–€ μ½”λ“œλ“€μ΄ μΆ”κ°€ 되면 쒋을 지 μƒκ°ν•΄λ³΄μž.

μš”κ΅¬μ‚¬ν•­ 반영 μ „

Main.java

public class Main {
    public static void main(String[] args) {
        Media media = new Image("Monalisa");
        Viewer viewer = new Viewer();
        viewer.view(media);
    }
}

Viewer.java

public class Viewer { // media의 show()λ₯Ό ν˜ΈμΆœν•˜λŠ” μ—­ν• 
    public void view(Media media){
        media.show();
    }
}

Media.java( Media interface )

public interface Media {
    public void show();
}

Image.java

public class Image implements Media {
    public String name;

    public Image(String name) {
        this.name = name;
    }

    @Override
    public void show() {
        System.out.println("Image :" + name);
    }
}

μš”κ΅¬μ‚¬ν•­ 반영 ν›„

  1. Video 클래슀, AdvancedMedia μΈν„°νŽ˜μ΄μŠ€μ— λŒ€ν•œ νŒŒμΌμ„ μΆ”κ°€ν•œλ‹€.
  2. Image와 VideoλŠ” interfaceκ°€ λ‹€λ₯΄κΈ° λ•Œλ¬Έμ— Videoλ₯Ό λ°”λ‘œ Viewerμ—μ„œ μ‚¬μš©ν•  수 μ—†κΈ° λ•Œλ¬Έμ— Adapterλ₯Ό μΆ”κ°€ν•΄μ€€λ‹€.

AdvancedMedia.java( AdvancedMedia interface )

public interface AdvancedMedia {
    public void play();
}

Video.java

public class Video implements AdvancedMedia {
    public String name;

    public Video(String name) {
        this.name = name;
    }

    @Override
    public void play() {
        System.out.println("Video :" + name);
    }
}

객체 μ–΄λŒ‘ν„° νŒ¨ν„΄(μœ„μž„)

MediaAdapter.java

public class MediaAdapter implements Media {
    public AdvancedMedia media; // μœ„μž„ 
    public MediaAdapter(AdvancedMedia media) {
        this.media = media;
    }

    @Override
    public void show() {
        media.play();
    }
}

Main.java

public class Main {
    public static void main(String[] args) {

        Media media = new Image("Monalisa");
        AdvancedMedia advancedMedia = new Video("IZONE M/V"); // μœ„μž„ν•  객체 생성

        MediaAdapter mediaAdapter = new MediaAdapter(advancedMedia); // MediaAdapter에 advancedMedia μœ„μž„
      
        Viewer viewer = new Viewer();
        viewer.view(media);
        viewer.view(mediaAdapter);
    }
}

클래슀 μ–΄λŒ‘ν„° νŒ¨ν„΄

MediaAdapter.java

public class MediaAdapter extends Video implements Media { // 상속
    public MediaAdapter(String name) {
        super(name); // μƒμ†ν•œ Video 클래슀 init
    }

    @Override
    public void show() {
        super.play(); // μƒμ†ν•œ Video 클래슀의 play() μ‹€ν–‰
    }
}

Main.java

public class Main {
    public static void main(String[] args) {

        Media media = new Image("Monalisa");
        MediaAdapter mediaAdapter = new MediaAdapter("IZONE M/V");
      
        Viewer viewer = new Viewer();
        viewer.view(media);
        viewer.view(mediaAdapter);
    }
}

κ²°κ³Ό

μ–΄λ–€ Adapterλ₯Ό μ μš©ν•΄λ„ κ²°κ³ΌλŠ” κ°™κ²Œ λ‚˜μ˜¨λ‹€.

image

마무리

μ΄λ ‡κ²Œ μ–΄λŒ‘ν„° νŒ¨ν„΄μ„ μ‚¬μš©ν•˜λ©΄ κΈ°μ‘΄ μ†ŒμŠ€μ½”λ“œλ₯Ό μˆ˜μ •ν•˜μ§€ μ•Šκ³  μƒˆλ‘œμš΄ λ˜λŠ” λ§žμΆ”κ³ μž ν•˜λŠ” μΈν„°νŽ˜μ΄μŠ€μ— λ§žμΆ°μ„œ λ™μž‘κ°€λŠ₯ν•˜κ²Œ ν•œλ‹€. κ·Έλ ‡κΈ° λ•Œλ¬Έμ— μ†ŒμŠ€μ½”λ“œκ°€ μ’€ 더 간결해지고 μœ μ§€λ³΄μˆ˜ λ˜ν•œ μ›ν™œν•΄μ§„λ‹€.