10 More Days to Become a Better Developer by Daniel Scheufler
Author:Daniel Scheufler [Daniel Scheufler]
Language: eng
Format: epub, pdf
Publisher: leanpub.com
Published: 2022-11-19T00:00:00+00:00
You can read more about the Factory Design Pattern here or return to Day 2
Software Pattern - Adapter
The Adapter pattern is a structural pattern. It deals with the structure of objects in code. The Adapter acts as passthru layer adapting two different interfaces. Think of it like a AC-USB adapter for charging your devices. The Adapterâs job is to allow different interfaces to interoperate. Being a structural pattern, you should favor keeping the implementation of the adapter pure with minimum additional logic to enable the adaptation. You can see a simplistic example below, where we will convert a third party libraryâs interface to one our system can use:
1 // Our Target Interface 2 interface IAcmeCoMediaPlayer { 3 function play(): Stream; // starts playback of Media,\ 4 into result Stream 5 function seek(timeInSec: Number): boolean; // moves c\ 6 ursor to indicated point in time of Media, returns true i\ 7 f successful 8 function stop(): void; // stops playback of Media int\ 9 o existing Streams if any. 10 } 11 12 // Support Classes for Third Party Media Reader 13 type MediaResult = { 14 wasSuccessful: boolean; 15 errorMsg?: string; 16 } 17 18 type ThirdPartyStream = {} 19 20 class ThirdPartyMediaPlayer { 21 construtor(inputPath: string){ 22 // do something wtih inputPath 23 } 24 25 function start(): ThirdPartyStream { 26 // starts playing inputPath content into ThirdPar\ 27 tyStream object 28 } 29 30 function goTo(min: Number, sec: Numnber): MediaResult{ 31 // seeks position in inputPath 32 } 33 34 function stop(): MediaResult { 35 // stops the playback if possible, returns error \ 36 is not already playing. 37 } 38 } 39 40 class MediaPlayerAdapter implements IAcmeCoMediaPlayer { 41 42 _wrappedPlayer: ThirdPartyMediaPlayer; 43 44 constructor(inputPath: string)}{ 45 _wrappedPlayer - new ThirdParthMediaPlayer(inputP\ 46 ath); 47 } 48 49 function convertedCommonStreamOf(thirdPartyStream: Th\ 50 irdPartyStream): Stream {} 51 function play(): Stream{ 52 return convertedCommonStreamOf(_wrappedPlayer.sta\ 53 rt()); 54 } 55 56 57 function seek(timeInSec: Number): boolean { 58 // need to convert arguments 59 const inputMin = floor(timeInSec / 60); 60 const remainingSec = timeInSec % 60; 61 62 const thirdPartySeekResult = _wrappedPlayer.goTo(\ 63 inputMin, remainingSec); 64 // might console log the error message here if ne\ 65 cessary 66 return thirdPartySeekResult.wasSuccessful; 67 } 68 69 function stop(): void { 70 _wrappedPlayer.stop(); 71 } 72 }
Download
10 More Days to Become a Better Developer by Daniel Scheufler.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26275)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(18806)
Shot Through the Heart by Mercy Celeste(18688)
The Fifty Shades Trilogy & Grey by E L James(18583)
The Subtle Art of Not Giving a F*ck by Mark Manson(13902)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(13841)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(12772)
Scorched Earth by Nick Kyme(12510)
Drei Generationen auf dem Jakobsweg by Stein Pia(10739)
Suna by Ziefle Pia(10673)
Scythe by Neal Shusterman(10023)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9281)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9096)
This is Going to Hurt by Adam Kay(8729)
Dirty Filthy Fix: A Fixed Trilogy Novella by Laurelin Paige(7345)
How to Make Love to a Negro Without Getting Tired by Dany LaFerrière(6749)
He Loves Me...KNOT by RC Boldt(6631)
Unleashing the Power of UX Analytics: Proven techniques and strategies for uncovering user insights [Team-IRA] [True PDF] by Jeff Hendrickson(6309)
Interdimensional Brothel by F4U(6087)
