Design Patterns - Adapter Pattern
Design Pattern adalah sebuah solusi untuk menyelesaikan masalah umum yang sering di temui dan dihadapi, yang membentuk sebuah pola (pattern) tertentu dalam membuat sebuah program. Pattern adalah bagaimana mendesain kelas dan bagaimana interaksi yang terjadi antar kelas tersebut sehingga kelas yang kita bangun bisa lebih elegan dan reusable. Ada beberapa pattern yang populer saat ini yang diciptakan oleh GoF (Gang of Four) yaitu Abstract Factory, Builder, Factory Method, Prototype, Singleton, Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy, Chain of Responbility, Command, Interpreter, Iterator, Mediator, Mementto, Observer, State, Strategy, Template Method, Visitor.Implementation
We have a MediaPlayer interface and a concrete class AudioPlayer implementing the MediaPlayer interface. AudioPlayer can play mp3 format audio files by default. We are having another interface AdvancedMediaPlayer and concrete classes implementing the AdvancedMediaPlayer interface. These classes can play vlc and mp4 format files. We want to make AudioPlayer to play other formats as well. To attain this, we have created an adapter class MediaAdapter which implements the MediaPlayer interface and uses AdvancedMediaPlayer objects to play the required format. AudioPlayer uses the adapter class MediaAdapter passing it the desired audio type without knowing the actual class which can play the desired format. AdapterPatternDemo, our demo class will use AudioPlayer class to play various formats.
source : https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
AdvancedMediaPlayer.java
VlcPlayer.java
Mp4Player.java
MediaAdapter.java
AudioPlayer.java
AdapterPatternDemo.java
No comments:
Post a Comment