Adapter Pattern Adapter pattern works as a bridge
Adapter Pattern
• Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces. • This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. • A real life example could be a case of card reader which acts as an adapter between memory card and a laptop. You plugin the memory card into card reader and card reader into the laptop so that memory card can be read via laptop. We are demonstrating use of Adapter pattern via following example in which an audio player device can play mp 3 files only and wants to use an advanced audio player capable of playing vlc and mp 4 files.
• Intent • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. • Also Known As • Wrapper
• Applicability Use the Adapter pattern when • you want to use an existing class, and its interface does not match the one you need. • you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces. • (object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
• Participants • Target (Shape) o defines the domain-specific interface that Client uses. • Client (Drawing. Editor) o collaborates with objects conforming to the Target interface. • Adaptee (Text. View) o defines an existing interface that needs adapting. • Adapter (Text. Shape) interface. o adapts the interface of Adaptee to the Target
Collaborations • Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee operations that carry out the request.
Implementation • We have a Media. Player interface and a concrete class Audio. Player implementing the. Media. Player interface. Audio. Player can play mp 3 format audio files by default. We are having another interface Advanced. Media. Player and concrete classes implementing the Advanced. Media. Player interface. These classes can play vlc and mp 4 format files. We want to make Audio. Player to play other formats as well. To attain this, we have created an adapter class Media. Adapter which implements the Media. Player interface and uses Advanced. Media. Player objects to play the required format. Audio. Player uses the adapter class Media. Adapter passing it the desired audio type without knowing the actual class which can play the desired format. Adapter. Pattern. Demo, our demo class, will use Audio. Player class to play various formats.
Step 1 - Create interfaces for Media Player and Advanced Media Player.
Step 2 - Create concrete classes implementing the Advanced. Media. Player interface.
Step 3 - Create adapter class implementing the Media. Player interface.
Step 4 - Create concrete class implementing the Media. Player interface.
Step 5 - Use the Audio. Player to play different types of audio formats.
Step 6 - Verify Output
- Slides: 14