SlideShare une entreprise Scribd logo
1  sur  47
Adopting Model-View-
 Controller in Cocoa
  and Objective-C
             Graham Lee
     http://twitter.com/iamleeg
Memory Lane
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with Classes”.
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with Classes”.

•   1986: Cox authors Object Oriented Programming: an
    evolutionary approach, ObjC is born
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with Classes”.

•   1986: Cox authors Object Oriented Programming: an
    evolutionary approach, ObjC is born

•   1988: NeXT licence ObjC from StepStone
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with Classes”.

•   1986: Cox authors Object Oriented Programming: an
    evolutionary approach, ObjC is born

•   1988: NeXT licence ObjC from StepStone

•   1993: GCC runtime for ObjC on !NeXT OS
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with Classes”.

•   1986: Cox authors Object Oriented Programming: an
    evolutionary approach, ObjC is born

•   1988: NeXT licence ObjC from StepStone

•   1993: GCC runtime for ObjC on !NeXT OS

•   1995: NeXT purchase all ObjC rights
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with Classes”.

•   1986: Cox authors Object Oriented Programming: an
    evolutionary approach, ObjC is born

•   1988: NeXT licence ObjC from StepStone

•   1993: GCC runtime for ObjC on !NeXT OS

•   1995: NeXT purchase all ObjC rights

•   1996: Apple (Computer, back then) acquire NeXT
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with Classes”.

•   1986: Cox authors Object Oriented Programming: an
    evolutionary approach, ObjC is born

•   1988: NeXT licence ObjC from StepStone

•   1993: GCC runtime for ObjC on !NeXT OS

•   1995: NeXT purchase all ObjC rights

•   1996: Apple (Computer, back then) acquire NeXT

•   2001: Mac OS X released
What does MVC mean?
What does MVC mean?
          Display information




          Prepare content




                  Provide information
What does MVC mean?
   Respond to events      Display information




  Trigger service logic   Prepare content




 Do the work                      Provide information
Demo - one-minute
    browser
What did I do?
What did I do?
• Drew boxes in Interface Builder
What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
  objects
What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
  objects
• Automatically un-archived and displayed by
  my app
What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
  objects
• Automatically un-archived and displayed by
  my app
• MVC score: F    Should really try harder
On syntax
On syntax

• [anObject   sendMessage: parameter];
On syntax

• [anObject sendMessage: parameter];
• @property int count;
• int x = anObject.count;
• anObject.count = x * 2;
Demo - model the URL
MVC Score: D Principles not really applied
Delegates
Delegates
• Customise an object’s behaviour by making
  decisions on its behalf
Delegates
• Customise an object’s behaviour by making
  decisions on its behalf
• GoF: Template Method (kindof)
Delegates
• Customise an object’s behaviour by making
  decisions on its behalf
• GoF: Template Method (kindof)
• Implement a subset of delegate methods,
  runtime introspection avoids exceptions
Delegates
• Customise an object’s behaviour by making
  decisions on its behalf
• GoF: Template Method (kindof)
• Implement a subset of delegate methods,
  runtime introspection avoids exceptions
• We’ll use WebFrameLoadDelegate
Demo - fix the address
        bar
Demo - fix the address
        bar
     (oh, and add window titles)
Cocoa Bindings
Cocoa Bindings


  «bind»
Cocoa Bindings


  «bind»   «observe»
MVC Score: B Good, but I want bookmarks ☺
Demo - bookmarks
MVC Score: A That’ll do for now!
?

Contenu connexe

Plus de Graham Lee

Plus de Graham Lee (8)

Beyond build and analyze
Beyond build and analyzeBeyond build and analyze
Beyond build and analyze
 
Sign your code
Sign your codeSign your code
Sign your code
 
Unit testing for Cocoa developers
Unit testing for Cocoa developersUnit testing for Cocoa developers
Unit testing for Cocoa developers
 
Security and Encryption on iOS
Security and Encryption on iOSSecurity and Encryption on iOS
Security and Encryption on iOS
 
Dial M For Mitigation
Dial M For MitigationDial M For Mitigation
Dial M For Mitigation
 
Presentations and Podcasts - OxMug July 2009
Presentations and Podcasts - OxMug July 2009Presentations and Podcasts - OxMug July 2009
Presentations and Podcasts - OxMug July 2009
 
Intel Briefing Notes
Intel Briefing NotesIntel Briefing Notes
Intel Briefing Notes
 
Designing a Secure Cocoa App
Designing a Secure Cocoa AppDesigning a Secure Cocoa App
Designing a Secure Cocoa App
 

Dernier

Dernier (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Adopting MVC in Cocoa

  • 1. Adopting Model-View- Controller in Cocoa and Objective-C Graham Lee http://twitter.com/iamleeg
  • 3. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”.
  • 4. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”. • 1986: Cox authors Object Oriented Programming: an evolutionary approach, ObjC is born
  • 5. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”. • 1986: Cox authors Object Oriented Programming: an evolutionary approach, ObjC is born • 1988: NeXT licence ObjC from StepStone
  • 6. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”. • 1986: Cox authors Object Oriented Programming: an evolutionary approach, ObjC is born • 1988: NeXT licence ObjC from StepStone • 1993: GCC runtime for ObjC on !NeXT OS
  • 7. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”. • 1986: Cox authors Object Oriented Programming: an evolutionary approach, ObjC is born • 1988: NeXT licence ObjC from StepStone • 1993: GCC runtime for ObjC on !NeXT OS • 1995: NeXT purchase all ObjC rights
  • 8. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”. • 1986: Cox authors Object Oriented Programming: an evolutionary approach, ObjC is born • 1988: NeXT licence ObjC from StepStone • 1993: GCC runtime for ObjC on !NeXT OS • 1995: NeXT purchase all ObjC rights • 1996: Apple (Computer, back then) acquire NeXT
  • 9. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”. • 1986: Cox authors Object Oriented Programming: an evolutionary approach, ObjC is born • 1988: NeXT licence ObjC from StepStone • 1993: GCC runtime for ObjC on !NeXT OS • 1995: NeXT purchase all ObjC rights • 1996: Apple (Computer, back then) acquire NeXT • 2001: Mac OS X released
  • 10. What does MVC mean?
  • 11. What does MVC mean? Display information Prepare content Provide information
  • 12. What does MVC mean? Respond to events Display information Trigger service logic Prepare content Do the work Provide information
  • 13. Demo - one-minute browser
  • 14. What did I do?
  • 15. What did I do? • Drew boxes in Interface Builder
  • 16. What did I do? • Drew boxes in Interface Builder • Automatically created and archived View objects
  • 17. What did I do? • Drew boxes in Interface Builder • Automatically created and archived View objects • Automatically un-archived and displayed by my app
  • 18. What did I do? • Drew boxes in Interface Builder • Automatically created and archived View objects • Automatically un-archived and displayed by my app • MVC score: F Should really try harder
  • 20. On syntax • [anObject sendMessage: parameter];
  • 21. On syntax • [anObject sendMessage: parameter]; • @property int count; • int x = anObject.count; • anObject.count = x * 2;
  • 22. Demo - model the URL
  • 23.
  • 24.
  • 25. MVC Score: D Principles not really applied
  • 27. Delegates • Customise an object’s behaviour by making decisions on its behalf
  • 28. Delegates • Customise an object’s behaviour by making decisions on its behalf • GoF: Template Method (kindof)
  • 29. Delegates • Customise an object’s behaviour by making decisions on its behalf • GoF: Template Method (kindof) • Implement a subset of delegate methods, runtime introspection avoids exceptions
  • 30. Delegates • Customise an object’s behaviour by making decisions on its behalf • GoF: Template Method (kindof) • Implement a subset of delegate methods, runtime introspection avoids exceptions • We’ll use WebFrameLoadDelegate
  • 31. Demo - fix the address bar
  • 32. Demo - fix the address bar (oh, and add window titles)
  • 34. Cocoa Bindings «bind»
  • 35. Cocoa Bindings «bind» «observe»
  • 36.
  • 37.
  • 38.
  • 39. MVC Score: B Good, but I want bookmarks ☺
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46. MVC Score: A That’ll do for now!
  • 47. ?

Notes de l'éditeur

  1. Who I am. What _are_ Objective-C and Cocoa? First, a walk down memory lane.
  2. Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up. Next: what exactly is MVC?
  3. Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up. Next: what exactly is MVC?
  4. Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up. Next: what exactly is MVC?
  5. Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up. Next: what exactly is MVC?
  6. Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up. Next: what exactly is MVC?
  7. Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up. Next: what exactly is MVC?
  8. Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up. Next: what exactly is MVC?
  9. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  10. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  11. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  12. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  13. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  14. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  15. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  16. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  17. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  18. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  19. Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do? Next: into the sample.
  20. I could do 45 of these in one session - it’s a shame there’s no market.
  21. So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL. Next: see what does really count as a model.
  22. So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL. Next: see what does really count as a model.
  23. So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL. Next: see what does really count as a model.
  24. So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL. Next: see what does really count as a model.
  25. So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL. Next: see what does really count as a model.
  26. Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
  27. Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
  28. Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
  29. Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
  30. The only model object we really have is the URL. Let’s see how we can factor that out into an MVC design. Next: class view.
  31. So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty. Next: move onto fixing the URL field, but discuss delegates first.
  32. So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty. Next: move onto fixing the URL field, but discuss delegates first.
  33. So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty. Next: move onto fixing the URL field, but discuss delegates first.
  34. So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty. Next: move onto fixing the URL field, but discuss delegates first.
  35. So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty. Next: move onto fixing the URL field, but discuss delegates first.
  36. Some objects implement a basic behaviour then allow another object to interpose at specific points in the process. This is called delegation in Cocoa. It’s a bit like a Template Method, except that the delegate doesn’t encapsulate the original object. ObjC messaging means you can implement a subset of the delegate methods.
  37. Some objects implement a basic behaviour then allow another object to interpose at specific points in the process. This is called delegation in Cocoa. It’s a bit like a Template Method, except that the delegate doesn’t encapsulate the original object. ObjC messaging means you can implement a subset of the delegate methods.
  38. Some objects implement a basic behaviour then allow another object to interpose at specific points in the process. This is called delegation in Cocoa. It’s a bit like a Template Method, except that the delegate doesn’t encapsulate the original object. ObjC messaging means you can implement a subset of the delegate methods.
  39. Some objects implement a basic behaviour then allow another object to interpose at specific points in the process. This is called delegation in Cocoa. It’s a bit like a Template Method, except that the delegate doesn’t encapsulate the original object. ObjC messaging means you can implement a subset of the delegate methods.
  40. We can use a delegation point in the loading of a web page in order to get the address that the web view has really gone to. Oh, where’s the code to update the text field, though?
  41. If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested. Next: how that works in the browser app.
  42. If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested. Next: how that works in the browser app.
  43. If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested. Next: how that works in the browser app.
  44. If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested. Next: how that works in the browser app.
  45. If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested. Next: how that works in the browser app.
  46. If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested. Next: how that works in the browser app.
  47. So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility. Next: bookmarks.
  48. So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility. Next: bookmarks.
  49. So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility. Next: bookmarks.
  50. So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility. Next: bookmarks.
  51. So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility. Next: bookmarks.
  52. So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility. Next: bookmarks.
  53. Add the bookmarks. No need to change the model, just bind the new UI to a collection of web addresses.
  54. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!
  55. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!
  56. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!
  57. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!
  58. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!
  59. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!
  60. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!
  61. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!
  62. First: NSArrayController manages its content Second: Add bookmark, note how information only passed over the controller layer Third: Bookmark this page, again information flow over controller layer only Next: Qs!