Adopting MVC in Cocoa

7,185 views
7,094 views

Published on

A presentation I delivered to ACCU in 2009 about using MVC (model-view-controller) in Cocoa to make best use of the frameworks and maximise code reuse.

Published in: Technology, Self Improvement
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,185
On SlideShare
0
From Embeds
0
Number of Embeds
470
Actions
Shares
0
Downloads
148
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide
  • Who I am. What _are_ Objective-C and Cocoa? First, a walk down memory lane.
  • 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?
  • 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?
  • 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?
  • 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?
  • 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?
  • 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?
  • 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?
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • I could do 45 of these in one session - it’s a shame there’s no market.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
  • Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
  • Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
  • Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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?
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.



    Next: bookmarks.
  • So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.



    Next: bookmarks.
  • So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.



    Next: bookmarks.
  • So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.



    Next: bookmarks.
  • So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.



    Next: bookmarks.
  • So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.



    Next: bookmarks.
  • Add the bookmarks. No need to change the model, just bind the new UI to a collection of web addresses.
  • 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!
  • 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!
  • 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!
  • 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!
  • 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!
  • 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!
  • 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!
  • 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!
  • 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!
  • Adopting MVC in Cocoa

    1. 1. Adopting Model-View- Controller in Cocoa and Objective-C Graham Lee http://twitter.com/iamleeg
    2. 2. Memory Lane
    3. 3. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”.
    4. 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. 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. 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. 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. 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. 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. 10. What does MVC mean?
    11. 11. What does MVC mean? Display information Prepare content Provide information
    12. 12. What does MVC mean? Respond to events Display information Trigger service logic Prepare content Do the work Provide information
    13. 13. Demo - one-minute browser
    14. 14. What did I do?
    15. 15. What did I do? • Drew boxes in Interface Builder
    16. 16. What did I do? • Drew boxes in Interface Builder • Automatically created and archived View objects
    17. 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. 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
    19. 19. On syntax
    20. 20. On syntax • [anObject sendMessage: parameter];
    21. 21. On syntax • [anObject sendMessage: parameter]; • @property int count; • int x = anObject.count; • anObject.count = x * 2;
    22. 22. Demo - model the URL
    23. 23. MVC Score: D Principles not really applied
    24. 24. Delegates
    25. 25. Delegates • Customise an object’s behaviour by making decisions on its behalf
    26. 26. Delegates • Customise an object’s behaviour by making decisions on its behalf • GoF: Template Method (kindof)
    27. 27. 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
    28. 28. 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
    29. 29. Demo - fix the address bar
    30. 30. Demo - fix the address bar (oh, and add window titles)
    31. 31. Cocoa Bindings
    32. 32. Cocoa Bindings «bind»
    33. 33. Cocoa Bindings «bind» «observe»
    34. 34. MVC Score: B Good, but I want bookmarks ☺
    35. 35. Demo - bookmarks
    36. 36. MVC Score: A That’ll do for now!
    37. 37. ?

    ×