This project demonstrates the use of the Adapter Design Pattern to convert between incompatible interfaces. It includes the implementation of an AudioPlayer
class that plays audio files, and an adapter (MediaAdapter
) that allows it to play video files using an existing VideoPlayer
interface.
The Adapter Pattern allows the AudioPlayer
to work with the MediaPlayer
interface, enabling different types of media to be played in a unified way.
The Adapter Design Pattern is used to allow classes with incompatible interfaces to work together. In this case, the AudioPlayer
class is designed to play audio files, while the VideoPlayer
interface handles video files. The MediaAdapter
class is used to convert between these two incompatible interfaces, enabling the AudioPlayer
to handle both audio and video playback through a unified interface.
- MediaPlayer Interface: Defines the general contract for playing media files (audio and video).
- AudioPlayer Class: Implements the
MediaPlayer
interface and is capable of playing audio files. - VideoPlayer Interface: Defines the contract for playing video files.
- MediaAdapter Class: Implements the
MediaPlayer
interface and is used to convert calls to theVideoPlayer
interface, making it compatible with theAudioPlayer
.
When the AudioPlayer
is asked to play a video, the MediaAdapter
redirects the call to the VideoPlayer
to play the video.
-
Clone the repository
-
Navigate to the project directory
-
Compile the Java files: javac -d out/production/classes src/main/java/org/example/*.java
-
Run the application: java -cp out/production/classes org.example.Main
This will run the Main
class, which demonstrates the use of the Adapter Design Pattern.
-
MediaPlayer Interface: Defines the
play()
method that all media players (audio/video) must implement. -
AudioPlayer Class: Implements the
MediaPlayer
interface and provides functionality to play audio files. -
VideoPlayer Interface: Defines the
playVideo()
method for video playback. -
MediaAdapter Class: Implements the
MediaPlayer
interface and delegates theplay()
method to theVideoPlayer
class when playing a video. -
Main Class: The entry point of the program, where the
AudioPlayer
andMediaAdapter
are instantiated and used to play both audio and video.
When running the Main
class, the following output will be displayed:
The Adapter Design Pattern allows for the flexibility of using incompatible interfaces without changing the existing code. By using an adapter, we can extend the functionality of a class to support new types of media (audio and video) while maintaining a consistent interface. This pattern demonstrates how software components can be integrated more effectively without modifying their original design.