Adapter modeli

A3qlA0BWz5jGyId2tZ6y_18.png

Adapter modeli ikkita o’zaro mos kelmaydigan interfeyslar o’rtasida ko’prik vazifasini o’taydi. Bu model tuzilmali model turiga kiradi. Chunki u ikkita mustaqil interfeyslarning imkoniyarlarini birlashtiradi.

Bu model o’z ichiga o’zaro mos kelmaydigan yoki mustaqil interfeyslarning funksiyasalarini birlashtiruvchi yagona klassni oladi. Hayotiy misol sifatida xotira kartasi va noutbuk o’rtasida ko’prik bo’lib qiladigan karta o’quvchi qurilmani keltirish mumkin. Xotira kartasini karta o’quvchi qurilma ichiga, qurilmani esa noutbukka kiritganingizda xotira kartasidagi ma’lumotlarni noutbuk orqali o’qishingiz mumkin bo’ladi.

Adapter modelini quyidagi misol bilan tushuntirmoqchiqmiz: audio player qurilmasi faqat mp3 formatli musiqalarni o’qiy oladi. Biz esa uni vlc va mp4 formatlarni ham o’qiydigan qilmoqchimiz.

Ishlatilishi.

Bizda MediaPlayer interfeysi va undan foydalanuvchi AudioPlayer xususiy hol klasi bor. Odatiy holda AudioPlayer mp3 formatni o’qiydi.

Yana bizda AdvancedMediaPlayer interfeysini hamda bu interfeysni ishlatuvchi xususiy hol klasi ham mavjud. Bu klasslar vlc va mp4 formatlarni o’qiy oladi.

Shu bilan birga AudioPlayer klassini ham boshqa formatlarni o’qiydigan qilmoqchimiz. Buni amalga oshirish uchun MediaPlayer interfeysini ishlatadigan va so’ralgan formatlarni o’qish uchun AdvancedMediaPlayer obyektilaridan foydalanadigan MediaAdapter adapter klassini yaratamiz.

AudioPlayer MediaAdapter adapter klassidan foydalanadi. Kerakli formatni o’qiydigan haqiqiy klassni bilmay turib AudioPlayer klassi adapter klassga o’zi o’qishni xohlagan formatni uzatadi. AdapterPatternDemo klassi har xil ko’rinishdagi formatlarni o’qish uchun AudioPlayer klassidan foydalanadi.

1-qadam.

MediaPlayer va AdvancedMediaPlayer interfeyslarini yaratamiz

MediaPlayer.java

public interface MediaPlayer {
   public void play(String audioType, String fileName);
}

AdvancedMediaPlayer.java

public interface AdvancedMediaPlayer {         public void playVlc(String fileName);    public void playMp4(String fileName); }

2-qadam.

AdvancedMediaPlayer interfeysini ishlatadigan xususiy hol klasslarini yaratamiz.

VlcPlayer.java

public class VlcPlayer implements AdvancedMediaPlayer{
   @Override
   public void playVlc(String fileName) {
      System.out.println("Playing vlc file. Name: "+ fileName);                
   }

 
   @Override
   public void playMp4(String fileName) {
      // hech narsa qilmaydi
   }
}

Mp4Player.java

public class Mp4Player implements AdvancedMediaPlayer{

 
   @Override
   public void playVlc(String fileName) {
      //hech narsa qilmaydi
   }

 
   @Override
   public void playMp4(String fileName) {
      System.out.println("Playing mp4 file. Name: "+ fileName);                
   }
}

3-qadam.

MediaPlayer interfeysini ishlatuvchi adapter klas yaratamiz

MediaAdapter.java

public class MediaAdapter implements MediaPlayer {

 
   AdvancedMediaPlayer advancedMusicPlayer;

 
   public MediaAdapter(String audioType){
   
      if(audioType.equalsIgnoreCase("vlc") ){
         advancedMusicPlayer = new VlcPlayer();                       
         
      }else if (audioType.equalsIgnoreCase("mp4")){
         advancedMusicPlayer = new Mp4Player();
      }  
   }

 
   @Override
   public void play(String audioType, String fileName) {
   
      if(audioType.equalsIgnoreCase("vlc")){
         advancedMusicPlayer.playVlc(fileName);
      }
      else if(audioType.equalsIgnoreCase("mp4")){
         advancedMusicPlayer.playMp4(fileName);
      }
   }
}

4-qadam.

MediaPlayer interfeysini ishlatuvchi xususiy hol klassini yaratamiz.

AudioPlayer.java


public class AudioPlayer implements MediaPlayer {

   MediaAdapter mediaAdapter;
   @Override

   public void play(String audioType, String fileName) {             
      //inbuilt mp3 formatidagi faylni o’qishni ta’minlaydi

      if(audioType.equalsIgnoreCase("mp3")){

         System.out.println("Playing mp3 file. Name: " + fileName);                   

      }
      //mediaAdapter boshqa formatlarni o’qishni ta’minlaydi

      else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){

         mediaAdapter = new MediaAdapter(audioType);

         mediaAdapter.play(audioType, fileName);

      }
      else{
         System.out.println("Invalid media. " + audioType + " format not supported");
      }
   }  
}

5-qadam.

Turli xildagi audio formatlarni o’qish uchun AudioPlayerdan foydalanamiz

AdapterPatternDemo.java

public class AdapterPatternDemo {

   public static void main(String[] args) {

      AudioPlayer audioPlayer = new AudioPlayer();

      audioPlayer.play("mp3", "beyond the horizon.mp3");

      audioPlayer.play("mp4", "alone.mp4");

      audioPlayer.play("vlc", "far far away.vlc");

      audioPlayer.play("avi", "mind me.avi");

   }

}

6-qadam. Natijani ko’ramiz.


Playing mp3 file. Name: beyond the horizon.mp3

Playing mp4 file. Name: alone.mp4

Playing vlc file. Name: far far away.vlc

Invalid media. avi format not supported