Podcast: Play in new window | Download
Subscribe: Apple Podcasts | Spotify | TuneIn | RSS
Part 4 of our design patterns series, this time up it’s Adapters, Facades, and Mementos. Oh, and which tech luminary would make the best head of state!
Episode 30 Show Notes and Survey
News & Updates
- We failed the Algorithm course, but @iamwillmadison is the man
- Lots of React.js meetups!
- Whitebox vs blackbox testing
- Meteor in Gaming coming up Wed 7/29 in ATL
- @theallenu started playing with Meteor and React
- Why Meteor? This post grabbed his interest:
http://wiki.dandascalescu.com/essays/why_meteor
- Why Meteor? This post grabbed his interest:
- Foundation vs Bootstrap
- Thanks for the reviews! AswinR, Fleckism
- Special thanks to Luke Warren!
- Topic suggestions from Adam and Andre Young
- Remind @iamwaltuo about Connect.js!
- Episode 29’s poll – the yes’s have it
- Atom vs Code – Send us your thoughts
- President? Steve Jobs? Bill Gates? Linus Torvalds? Rms?
Adapter Pattern
- What is it and what does it solve?
- Imagine you have a laptop that has mini-SD slot on it. You just bought a camera that takes microsd…and you have some sony memory sticks as well. How do you get it to where you can read all those memory cards using your laptop?
- You create an interface that your application uses. If you bring in code – perhaps a 3rd party library that doesn’t have the same methods as your application expects, you create an adapter that will sit in between your application code and the 3rd party library. This way your underlying code doesn’t change – it functionally stays the same, but you create the adapter that will hook into the new library using your same set of methods.
- TutorialsPoint had one of the better code examples of an adapter using Java. Concept is the same regardless of the programming language…
- Audio player example –
rather than muddy up your play method with a bunch of if else or switch case statements, you can keep that clean and move the code for handling that into a MediaAdapter class
Facade Pattern
- An object that provides a simplified interface to a larger body of code.
- The intention is to provide a simplified interface. Or to provide a unified interface to a set of interfaces (i.e. a wrapper).
- When to use:
You need/want to provide a simpler interface to a complex system or set of calls…more often than not - Remember the olden days of APIs, wrap that crap in a class
- Facade vs Adapter
- Adapter uses an existing interface or signature(s). Facade defines a new interface.
- Adapter makes one or more existing things look similar
- Both are wrappers. Facade often wraps multiple objects into a simpler call. Adapter wraps a single object.
- Adapter aims to solve the problem of making non-compatible interfaces compatible
- Uses in .NET: System.Net.WebClient,
Maybe JSON.NET? - Uses outside of .NET: React.js?
- If the facade pattern aims to make complex subsystems or interfaces simpler by wrapping them in an easier interface, are all NuGet packages, JavaScript frameworks, etc. facade patterns?
Memento Pattern
- Aimed at rolling back changes to an object
- Snapshot the internal state, and restore your object to that internal state by passing that data back
- Like taking a backup of your db before applying scary changes to it
- NOT a good way of implementing undo/redo. Command pattern is much better for that.
- It’s heavy handed, but it’s guarunteed to get the job done
- So, how it works
- Originator is real object
- Memento represents the state, should be immutable!
- Caretaker is in charge of requesting the memento, and getting it done
- Originator is the star
- Two methods “saveToMemento” and “restoreToMenento” (returns void)
- Having the originator know how to get/set is a violation of the SRP (if it’s not a DTO) – but the trade-off is that no class has to know about the internals of another, a clear violation of Encapsulation! – and that’s why i think studying Design Patterns is important!
- Pattern doesn’t say much about the caretaker. You gots to do what’s right for you
-
- Weirdo twists:
- Undo- Undo history!
- Clone objects! (Similar to prototype pattern)
- Send between different systems!
- Generics make it easy to re-use the same code in multiple classes: saveToMemento() restoreToMemento(T state)
- Saving files – Megaman
-
- Why it’s not used more often…
- Transactions are better implemented by the persisters
- Command is awesomer
Resources We Like
Tips of the Week
- CodeFormatter in Sublime
- Slack > Google Hangouts
- Atom / Code – keyboard shortcut cheat sheet