Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

I am not able to understand the use of passing interface in generics in java

I am doing design pattern course from coursera and i am given an assignment of implementing empty block of code by observing the uml diagram. The given uml diagram is this.

enter image description here

This is given empty block of code that is given.

The Program.java source code follows.

public class Program {

    public static void main(String args[]) {
    
    // Make new empty "Study" playlist 
    Playlist studyPlaylist = new Playlist("Study");     

    // Make "Synth Pop" playlist and add 2 songs to it.
    Playlist synthPopPlaylist = new Playlist("Synth Pop");
    Song synthPopSong1 = new Song("Girl Like You", "Toro Y Moi" );
    Song synthPopSong2 = new Song("Outside", "TOPS");
    synthPopPlaylist.add(synthPopSong1);
    synthPopPlaylist.add(synthPopSong2);

    // Make "Experimental" playlist and add 3 songs to it, 
    // then set playback speed of the playlist to 0.5x
    Playlist experimentalPlaylist = new Playlist("Experimental");       
    Song experimentalSong1 = new Song("About you", "XXYYXX");
    Song experimentalSong2 = new Song("Motivation", "Clams Casino");    
    Song experimentalSong3 = new Song("Computer Vision", "Oneohtrix Point Never");
    experimentalPlaylist.add(experimentalSong1);
    experimentalPlaylist.add(experimentalSong2);
    experimentalPlaylist.add(experimentalSong3);
    float slowSpeed = 0.5f;
    experimentalPlaylist.setPlaybackSpeed(slowSpeed);
    
    // Add the "Synth Pop" playlist to the "Experimental" playlist
    experimentalPlaylist.add(synthPopPlaylist);

    // Add the "Experimental" playlist to the "Study" playlist
    studyPlaylist.add(experimentalPlaylist);

    // Create a new song and set its playback speed to 1.25x, play this song, 
    // get the name of glitchSong → "Textuell", then get the artist of this song → "Oval"
    Song glitchSong = new Song("Textuell", "Oval");
    float fasterSpeed = 1.25f;
    glitchSong.setPlaybackSpeed(fasterSpeed);
    glitchSong.play(); 
    String name = glitchSong.getName(); 
    String artist = glitchSong.getArtist(); 
    System.out.println ("The song name is " + name );
    System.out.println ("The song artist is " + artist );

    // Add glitchSong to the "Study" playlist
    studyPlaylist.add(glitchSong);

    // Play "Study" playlist.
    studyPlaylist.play(); 

    // Get the playlist name of studyPlaylist → "Study"
    System.out.println ("The Playlist's name is " + studyPlaylist.getName() );
    }
    }

    --------------------
    [IComponent.java]
    --------------------
    public interface IComponent {

    // Your code goes here!



    }

    --------------------
    [Playlist.java]
    --------------------
    public class Playlist implements IComponent {

    public String playlistName;
    public ArrayList<IComponent> playlist = new ArrayList();

    public Playlist(String playlistName) {
        this.playlistName = playlistName;
    }

    // Your code goes here!


    }

    --------------------
    [Song.java]
    --------------------
    public class Song implements IComponent {
    public String songName;
    public String artist;
    public float speed = 1; // Default playback speed

    public Song(String songName, String artist ) {
        this.songName = songName;
        this.artist = artist; 
    }
    
    // Your code goes here!
    
    }

In this block of code IComponent is interface and it is passed in the Arraylist generics, i am not able to understand what this block of code is signifying.

question from:https://stackoverflow.com/questions/65878940/i-am-not-able-to-understand-the-use-of-passing-interface-in-generics-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

TLDR; Having a List of objects in a container (List in this case) that all implement the same interface means you can call interface methods without knowing or caring what type of objects they are.

The generic list of components means all the objects in the list adhere to the IComponent interface. Since both Song and Playlist implement IComponent the list can contain both types of object.

As is that doesn't mean much as there are no methods declared in IComponent. After you declare your play() method in IComponent for example, the compiler will ensure that both Song and Playlist classes implement play(). That means when you get a reference to any object in the list you can call the IComponent::play method regardless of what type of object it is. If the object is a Song, the Song::play method is called. If it's a Playlist the Playlist::play method is called.

Song::play should of course, play the song.

The Playlist::play method should call play() on each IComponent in the list stored in the Playlist. Hence it's possible to have a Playlist contain another Playlist or any combination of songs and playlists.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...