1 / 8

מנשקים

מנשקים. מנשק ( interface ) הוא מבנה תחבירי ב Java המאפשר לחסוך בקוד לקוח קוד אשר משתמש במנשק יוכל בזמן ריצה לעבוד עם מגוון מחלקות המממשות את המנשק הזה (ללא צורך בשכפול הקוד עבור כל מחלקה) דוגמא: נגן מוזיקה אשר מותאם לעבוד עם קובצי מוזיקה ( mp3 ) ועם קובצי וידאו ( mp4 ). Playing Mp3.

edie
Télécharger la présentation

מנשקים

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. מנשקים • מנשק (interface) הוא מבנה תחבירי ב Java המאפשר לחסוך בקוד לקוח • קוד אשר משתמש במנשק יוכל בזמן ריצה לעבוד עם מגוון מחלקות המממשות את המנשק הזה (ללא צורך בשכפול הקוד עבור כל מחלקה) • דוגמא: נגן מוזיקה אשר מותאם לעבוד עם קובצי מוזיקה (mp3) ועם קובצי וידאו (mp4)

  2. Playing Mp3 publicclass MP3Song { publicvoid play(){ // audio codec calculations, // play the song... { // does complicated stuff // related to MP3 format... { publicclass Player { privatebooleanrepeat; privatebooleanshuffle; publicvoidplaySongs(MP3Song[] songs) { do{ if(shuffle) Collections.shuffle(Arrays.asList(songs)); for(MP3Song song : songs) song.play(); } while (repeat); } {

  3. Playing VideoClips publicclassVideoClip { publicvoid play(){ // audio codec calculations, // play the song... { // does complicated stuff // related to MP3 format... { publicclass Player { // same as before... publicvoidplayVideos(VideoClip[] clips) { do{ if(shuffle) Collections.shuffle(Arrays.asList(clips)); for(VideoClipvideoClip : clips) videoClip.play(); } while (repeat); { {

  4. שכפול קוד publicvoidplaySongs(MP3Song[] songs) { do { if (shuffle) Collections.shuffle(Arrays.asList(songs)); for (MP3Song song : songs) song.play(); } while (repeat); } publicvoidplayVideos(VideoClip[] clips) { do { if (shuffle) Collections.shuffle(Arrays.asList(clips)); for (VideoClipvideoClip : clips) videoClip.play(); } while (repeat); } למרות ששני השרותים נקראים play() אלו פונקציות שונות! נרצה למזג את שני קטעי הקוד

  5. שימוש במנשק public void play (Playable [] items) { do { if (shuffle) Collections.shuffle(Arrays.asList(items)); for (Playable item : items) item.play(); } while (repeat); } public interfacePlayable { public void play(); }

  6. מימוש המנשק ע"י הספקים • public class VideoClipimplementsPlayable { • @Override • public void play() { • // render video, play the song on screen... • } • // does complicated stuff related to video formats... • } • public class MP3Song implementsPlayable { • @Override • public void play(){ • // audio codec calculations, play the song... • } • // does complicated stuff related to MP3 format... • }

  7. מערכים פולימורפים Playable [] songsAndClips = new Playable[3]; songsAndClips[0] = new MP3Song(); songsAndClips[1] = newVideoClip(); songsAndClips[2] = new MP4Song(); // new Playable class Player player = new Player(); // init player... player.play(songsAndClips); public void play (Playable [] items) { do { if (shuffle) Collections.shuffle(Arrays.asList(items)); for (Playable item : items) item.play(); } while (repeat); } עבור כל איבר במערך יקרא ה play() המתאים

More Related