Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Truths Universally Acknowledged: Swift Design P...

Avatar for Anne Anne
January 21, 2016

Truths Universally Acknowledged: Swift Design Patterns As Jane Austen Heroes

Have you ever looked at your code and realized that a certain design pattern was charging in like a romantic hero, sweeping away the confusion and bringing order to chaos? Or perhaps you've encountered a pattern that you utterly hated...until it's virtues slowly grew on you as you realized that the alternative was a complete disaster? Let's imagine Ada Lovelace reading Jane Austen, and compare some of my favorite design patterns with some of my favorite Jane Austen heroes.

Note: I'll be showing Swift code, but neither knowledge of Swift nor Jane Austen are required.

Avatar for Anne

Anne

January 21, 2016

Other Decks in Technology

Transcript

  1. How To Tell You Are In A Jane Austen Novel

    1. Someone disagreeable is trying to persuade you take a trip to Bath
  2. How To Tell You Are In A Jane Austen Novel

    2. A woman who hates you is playing the pianoforte
  3. How To Tell You Are In A Jane Austen Novel

    3. A picnic has gone horribly wrong
  4. How To Tell You Are In A Jane Austen Novel

    4. You once took a walk with a cad
  5. Pride and Prejudice • Mr. Fitzwilliam Darcy arrives at Netherfield

    Hall, immediately is horrified by the neighbors and makes a terrible impression on Ms. Elizabeth Bennet • Shortly thereafter, army officer Mr. Wickham arrives in town and makes a much better impression on the local ladies. • A series of events, Darcy eventually warms to Lizzie, while she’s seeing the appeal of Mr. Wickham. • So Darcy rushes to her side to tell her that, despite her appalling family and the fact that she is so terribly beneath him, he has, despite all his better judgment, fallen in love with her.
  6. Pride and Prejudice • Meanwhile, Wickham disappears with Lizzie’s younger

    sister—without marrying her. • Everyone panics, Mr. Bennet goes looking for his lost daughter but returns without her—only to find that somehow, everything has worked out for the best. • Wickham has married Lydia in return for an annual income…paid for by Darcy! • Darcy returns, he and Lizzie take a walk in a garden and he proposes.
  7. Protocols & Delegates “A protocol defines a blueprint of methods,

    properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.”
  8. For Example public protocol wooingDelegate { var endearments: String {

    get } func proposal() -> Bool } class upstandingGentleman: wooingDelegate { var income: Int = 10_000 var endearments = "You must allow me to tell you how ardently I admire and love you." func proposal() -> Bool { happilyEverAfter() return true } func happilyEverAfter() { // lives happily ever after } } class scoundrel: wooingDelegate { var debt: Int = -600 var endearments = "I know it's not funny, but your perfume smells like your daddy's got money" func proposal() -> Bool { getSlapped() return false } func getSlapped() { // gets slapped in the face } }
  9. Protocols & Delegates • Kind of smug • Inscrutable •

    Will save the day, behind the scenes
  10. How To Tell You Are In A Jane Austen Novel

    5. You develop a resentment at public dance
  11. Persuasion • 7 years ago, Anne Elliot broke an engagement

    to a young naval officer, Frederick Wentworth. • He was confident and ambitious, but not of a particularly noteworthy family—her father and sisters convinced her that she would be marrying beneath her station • But time passes, and Freddie Wentworth becomes the decorated Capt. Wentworth, but as for Anne, “her bloom had vanished early…she was faded and thin.” • And the proud Elliot family has fallen into debt and been reduced to renting out their manor to another naval officer
  12. Persuasion • Anne goes with some friends and Capt. Wentworth

    to Lyme to visit still more naval officers. One of the friends falls and is terribly injured, but Anne handles things and is generally competent • The entire party decamps to Bath, because in Austen novels you always end up in Bath? • In Bath, Anne has a conversation with a gentlemen over wether women or men fall out of love quicker • Capt. Wentworth overhears and hides a letter to Anne declaring his love.
  13. If Statement if setting == "Netherfield Hall" { cell?.tintColor =

    .blueColor() if suitor.name == "Wickham" { cell?.textLabel?.text = "Immoral con-man" } else if suitor.name == "Darcy" { cell?.textLabel?.text = "Insufferable, but maybe not?" } } else if setting == "Cleveland Park" { cell?.tintColor = .yellowColor() if suitor.name == "Willoughby" { cell?.textLabel?.text = "Charming, but a jerk" } else if suitor.name == "Brandon" { cell?.textLabel?.text = "Surprisingly sweet" } } else if setting == "Kellynch Hall" { cell?.tintColor = .greenColor() if suitor.name == "Elliot" { cell?.textLabel?.text = "Probably shady" } else if suitor.name == "Wentworth" { cell?.textLabel?.text = "The one that got away, almost" } }
  14. Switch statement switch setting { case "Netherfield Hall": cell?.tintColor =

    .blueColor() switch suitor.name { case "Wickham": cell?.textLabel?.text = "Immoral con-man" case "Darcy": cell?.textLabel?.text = "Insufferable, but maybe not?" } case "Cleveland Park": cell?.tintColor = .yellowColor() switch suitor.name { case "Willoughby": cell?.textLabel?.text = "Charming, but a jerk" case "Brandon": cell?.textLabel?.text = "Surprisingly sweet" } case "Kellynch Hall": cell?.tintColor = .greenColor() switch suitor.name { case "Elliot": cell?.textLabel?.text = "Probably shady" case "Wentworth": cell?.textLabel?.text = "The one that got away, almost" } }
  15. How To Tell You Are In A Jane Austen Novel

    6. You have five hundred a year. From who? Five hundred what? No one knows. No one cares. You have it. It’s yours. All five hundred of it.
  16. Sense and Sensibility • Marianne Dashwood meets the charming Mr.

    Willoughby when she sprains her ankle on a walk and he gallantly carries her home • The two are inseparable and deeply in love, but suddenly Mr. Willoughby departs for London and stops answering Marianne’s letters • Meanwhile, Col. Brandon shows up and is obviously taken with Marianne. • But! Col. Brandon has a scandalous past and possibly a bastard daughter!
  17. Sense and Sensibility • Marianne is invited to London, where

    she hopes to meet Mr. Willoughby again • And she does, at dance where he is incredibly rude to her—and announces his engagement to a wealthy heiress • Distraught, Marianne falls ill and Col. Brandon helps care for her, reading her sonnets • Eventually we learn that the alleged bastard daughter is his niece, who he cared for after she was orphaned • And Col. Brandon and Marianne get married
  18. Guard Statements “A guard statement is used to transfer program

    control out of a scope if one or more conditions aren’t met”
  19. Willoughby Code func acceptsProposal(suitor: gentleman) -> Bool { if !suitor.hasHat

    || suitor.income < 1200 { return false } else { return true } }
  20. Brandon Code func acceptsProposal(suitor: gentleman) -> Bool { guard suitor.hasHat

    else { return false } guard suitor.income > 12_000 else { return false } return true }
  21. How To Tell You Are In A Jane Austen Novel

    7. You are in a garden, and you are astonished.