Massive Parent-Child and delegate pattern

时间:2015-06-26 09:41:45

标签: ios objective-c design-patterns composite delegation

I'm facing with a complex design problem. Due to a hard designed graphic I can't use Apple navigation pattern as UINavigationController or other ones. This is the app diagram App diagram

Black arrow: protocols direction Blue arrow: parent-child pattern direction

I created this flow because I want to maintain the code inside the single ViewController clear and isolated: I thought about them as modules that they can be used somewhere in other apps. The RootViewController is the MainVCs position manager. It decides which is the current view-controller that it must be showed and it configures it based on its status (modified by delegate methods). The single MainVC doesn't know that RootVC exists and it call Root using protocols. The single ChildViewController doesn't know that its MainVC exists and it call the Main using protocols and so on.

Code is clear and much easy to understand, my purpose, but when I have to apply a modify to the skeleton (RootVC) from the ChildViewControllerN, child of the umpteenth ChildViewController, child of the umpteenth MainViewController, I have to propagate the protocol until the RootViewController.

My first question is: is this pattern correct? What do you think about it?

My second question is: is there a limit point where I haven't to use delegate pattern but something like KVO?

ADDING I read a university lecture about the composite pattern that it says:

A composite object knows its contained components, that is, its children. Should components maintain a reference to their parent component? Depends on application, but having these references supports the Chain of Responsibility pattern

So, according to the Chain, I can maintain a reference of the parent but I have to declare a custom interface for this kind of reference. However, doing this I will decrease the number of protocols, but the second question still be unanswered

1 个答案:

答案 0 :(得分:0)

观点:

当我超越单一级别的父/子关系时,我倾向于停止使用委托并转移到NSNotification。我经常直接NSNotification以减少依赖关系。我更喜欢KVO,因为它是明确的,而随着项目的进展,KVO更难调试。

(示例:如果在分配时刻和KVO传递之间在主线程上释放侦听器,则后台线程上的简单变量赋值会导致难以诊断的崩溃。)