Can you explain how to use a Mediator pattern with a Navigation Controller?
Hi Carlo,
I just bought your book ( as an ebook from Amazon ) yesterday and have been working my way through it having already read the Design patterns book by the “four” I was looking for examples in Objective-C, so great and thanks
I am a little confused with the Mediator pattern ( it might be the wrong pattern to use )
Our scenario is our application has the following flow:
Account Screen -> TableView -> TableView (multiple drill down of a hierarchical tree) -> image picker -> Special Table view
Currently we are using a navigation controller, however I would like to refactor our App to be more “OO” and after reading about the Mediator pattern it sounds like a good idea, however I cannot get my head around how a UINavigation would work with the Mediator?
If you can shine any light on this it would be much appreciated!
Cheers,
Chris


Chris,
Mediator is a right pattern for that situation. One approach is to hide your active navigation controller in your mediator. Your other views just need to tell your mediator to change states then your mediator will push an appropriate view controller to the active navigation controller. In most situations, you don’t need to worry about the pop (when the user hits the “back” button) unless you have some non-linear view flows in your application. If you have many view controllers to be managed by your mediator, then you may consider using a state machine to return an appropriate view controller one at a time. In that case, you have three different kinds of entities: regular controllers/user controls (colleagues), a mediator with a navigation controller, and a state machine that manages different view controllers for each state. They are all reusable and scalable independently.
Hope it helps.
Cheers,
Carlo
Was this answer helpful?
LikeDislikeHi Carlo,
Thank you for the really quick reply, much appreciated
I managed to get the Mediator working in the way you described above with the Navigation Controller inside the mediator, so it just pushes the appropriate views onto the Navigation Controller and as you mentioned, the back button just works
As our application is likely to become more complex I can see the Mediator’s main method becoming a monster, one thing that was missing in your book was the state machine, you mentioned it somewhere else on the site that you were going to write an article about it, have you done so as I would really like to understand if this would be an even better way to handle the interaction between the objects.
Best regards,
Chris
Was this answer helpful?
LikeDislikeChris,
I think I still have the first draft for the State pattern. Once I found it I’ll post it here in a format of blog posts.
Cheers,
Carlo
Was this answer helpful?
LikeDislikeHi Carlo,
Is there any chance you could post the first draft of the State Pattern as promised? Even just code and UML would be interesting. I tried asking before but my posts seem to vanish?
Great Book by the way, very useful.
Thanks.
Andy.
Was this answer helpful?
LikeDislikeSorry – missed your original reply. Thanks.
Hey Andy,
Thanks for the comment. In fact the code and UML are quite interesting in that chapter but it’ll be much better if there is at least some text in between. I’ll see if I can squeeze out some time to break it down into multiple parts and post one part at a time. Hopefully I can do it in the next week or so. Stay tuned.
- Carlo
Was this answer helpful?
LikeDislikeI found it but in fact only code and UML diagrams are there. I’ll post some diagrams with text a bit later.
- Carlo
Was this answer helpful?
LikeDislikeHi Carlo,
Thanks that would be great
Cheers,
Chris
Was this answer helpful?
LikeDislikeHi Carlo, can you explain the drawing data model in your book – whether this approach is best practice to store all vertex for strokes in drawing app and then draw them into canvas or you use this model only for explaining design patterns?
Sorry for my English ))
Was this answer helpful?
LikeDislikeHi Tort,
Like I mentioned in the book that the example drawing mechanism is not optimized. A better approach is to just let a view draw a very particular spot (i.e. just a new stroke). Also you can just draw dots/strokes as your finger goes without storing all the vertices in a model. We store them when undo/redo is desired. I hope this answers your question.
Cheers,
Carlo
Was this answer helpful?
LikeDislikeThank you Carlo for answer!
I missed one important thing in my question – of course I meant using drawing model in context of undo/redo mechanism. I try to find best practice for drawing app architecture, but all examples in the web are very simple. Maybe you can give me a link on some example of “big” and “serious” applications where considers approaches
I hope that I lay out my questions are clearly.
Was this answer helpful?
LikeDislikeTort,
I don’t know any other “big” and “serious” drawing apps out there for which you can download source code for free. Also there is no such “best practices” per se, as every application is like a solution for a particular problem. Therefore, there is no silver bullet for everything. A software architect needs to figure out good and better solutions for different software problems. The sample drawing app from the book is complex and big enough for anyone to start with. All you need to do is customize it to fit your own needs.
Hope it helps.
Carlo
Was this answer helpful?
LikeDislike