Design Patterns Adapter Pattern - Design Patterns

What is Design Pattern Adapter Pattern?

Adapter pattern works as a connection between two incompatible interfaces. This type of design pattern comes under structural pattern as this model combines the ability of two dependent interfaces.

This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. A real life instance could be a case of card reader which acts as an adapter between memory card and a laptop. You plug-in the memory cards into card reader and card reader into the laptop so that memory card can be study via laptop.

We are demonstrating use of Adapter pattern via following instance in which an audio player device can play mp3 files only and wants to apply an advanced audio player able of playing vice and mp4 files.

Implementation

We contain a Media Player interface and a valid class AudioPlayerimplementing the Media Player interface. Audio Player can play mp3 format audio files by payment.

We are having a different interface AdvancedMediaPlayer and real classes implementing the AdvancedMediaPlayer interface. These classes can play vice and mp4 arrangement files.

We want to create Audio Player to play other formats as well. To attain this, we have formed an adapter class Media Adapter which equipment the Media Player interface and uses AdvancedMediaPlayer substance to play the essential format.

Audio Player uses the adapter class Media Adapter momentary it the desired audio kind without knowing the actual class which can play the wanted format. AdapterPatternDemo, our presentation class will apply Audio Player class to play various formats.
adapter_pattern_uml_diagram

Step 1

Create interfaces for Media Player and basic Media Player.

MediaPlayer.java

AdvancedMediaPlayer.java

Step 2

Create real classes implementing the AdvancedMediaPlayer interface.

VlcPlayer.java

Mp4Player.java

Step 3

Create adapter class implement the Media Player line.

MediaAdapter.java

Step 4

Construct concrete class implementing the Media Player boundary.

AudioPlayer.java

Step 5

Apply the Audio performer to play dissimilar type of audio format.

AdapterPatternDemo.java

Step 6

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics